代码之家  ›  专栏  ›  技术社区  ›  Ram on Rails

`公寓::租户。切换!`在“bin/rails console”期间使用“pry”`

  •  0
  • Ram on Rails  · 技术社区  · 7 年前
    • 什么时候 console 已启动
    • 安慰 促使

    在这里查看输出。简单快捷的方法。 T.me T.names (DB中的租户)。。。

    启动,请求租户选择,设置

    $ bin/rails c
    Running via Spring preloader in process 11233
    Loading development environment (Rails 5.1.5)
       (1.9ms)  SELECT "public"."tenants"."subdomain" FROM "public"."tenants" WHERE "public"."tenants"."deleted_at" IS NULL ORDER BY "public"."tenants"."created_at" DESC
    Available tenants: {0=>"public", 1=>"local"}
    Select tenant: 1
    You are now Tenant 'local'
    
    Frame number: 0/24
    

    交换机租户

    [1] [my-project][development] pry(main)> T.ask
    Available tenants: {0=>"public", 1=>"local"}
    Select tenant: 0
    You are now Tenant 'public'
    => nil
    

    [2] [my-project][development] pry(main)> T.ask
    Available tenants: {0=>"public", 1=>"local"}
    Select tenant: 1
    You are now Tenant 'local'
    => nil
    

    当前租户

    [3] [my-project][development] pry(main)> T.me
    => "local"
    

    我们可以快速切换到

    [4] [my-project][development] pry(main)> T.hash
    => {0=>"public", 1=>"local"}
    

    [5] [my-project][development] pry(main)> T.names
    => ["local"]
    

    abc

    [6] [my-project][development] pry(main)> T.exists? 'abc'
    => false
    

    local

    [7] [my-project][development] pry(main)> T.exists? 'local'
    => true
    

    注意:这不是完全测试。使用前请先测试。这段代码只是给你一些想法,我是如何使用这些小的快捷方式来节省开发时间的。谢谢你的阅读。

    1 回复  |  直到 6 年前
        1
  •  3
  •   Ram on Rails    7 年前

    把它放进去 <project-root>/.pryrc

    # What is it?
    #   => Helper methods for Apartment::Tenant gem
    # How does it work?
    #   * bin/rails console => auto-loads and asks to switch tenant
    #   * T.ask             => anytime in console, to switch tenant from a list
    #   * T.me              => same as Apartment::Tenant.current
    #   * T.hash            => hash of tenants. Example: { 0 => "public", 1 => "tenant-a" }
    #   * T.names           => array with all existing tenant names from DB
    #   * T.exists?(arg)    => returns true/false if `arg` exists as tenant in DB
    #   * T.switch!(arg)    => same as Apartment::Tenant.switch!
    require "rubygems"
    
    # convenience class
    class T
      class << self
        # ['tenant1', 'tenant2', ...]
        def names
          @@names ||= Apartment.tenant_names.sort
        end
    
        # { 0 => 'public', 1 => 'tenant1', ...}
        def hash
          @@hash ||= { 0 => 'public' }.merge(
            (1..(T.names.length)).to_a
            .product(T.names)
            .to_h
          )
        end
    
        def switch! arg
          Apartment::Tenant.switch!(arg) if T.hash.value?(arg)
        end
    
        # current tenant
        def me
          Apartment::Tenant.current
        end
    
        def exists? arg
          T.names.include? arg
        end
    
        # ask to switch the tenant
        def ask
          WelcomeClass.select_tenant
        end
      end
    end
    
    # select tenant when entering console
    class WelcomeClass
      def self.select_tenant
        puts "Available tenants: #{T.hash}"
    
        print "Select tenant: "
        tenant = gets.strip # ask which one?
    
        unless tenant.empty?
          # by name
          if T.exists?(tenant)
            T.switch!(tenant)
    
          # by index position
          # string has digit + tenant index present
          elsif tenant[/\d/].present? && T.hash.key?(tenant.to_i)
            T.switch!(T.hash[tenant.to_i])
    
          # not found = no action
          else
            puts "Tenant not found in list '#{tenant}'"
          end
        end
    
        # announce current tenant
        puts "You are now Tenant '#{T.me}'"
      end
    end
    
    # run the code at `bin/rails console`
    Pry.config.exec_string = WelcomeClass.select_tenant
    
        2
  •  2
  •   bazfer    6 年前

    接受的答案需要更新:T'hash'方法正在创建具有正确键数的哈希,但所有键的值都与最后一个租户名称(0=>)重复公共',1=>'测试',2=>'测试'。。x=>'测试”)。下面是一个有效的“哈希”方法:

    def hash
      @@hash ||= Hash[(0..T.names.size - 1).zip T.names]
    end
    
        3
  •  0
  •   lcchatter    5 年前

    def hash
          @@hash ||= { 0 => 'public' }.merge(Hash[(1..T.names.size).zip T.names])
    end
    

    请添加到bazfer答案和已接受答案

    推荐文章