`Apartment::Tenant.switch!` during `bin/rails console` using `pry`

走远了吗. 提交于 2019-12-25 03:26:16

问题


  • when console is launched
  • while at console prompt

How it should work?

See the output here. Simple, quick methods. T.me (current tenant), T.names (tenants in the DB), ...

Launch, ask for tenant selection, set

$ 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

Switch tenant

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

Switch again

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

Current tenant

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

Tenant we can quickly switch to

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

Tenant names

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

Is abc a tenant?

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

Is local a tenant?

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

Note: This is not tested thoroughly. Please test before using. This code just gives you some idea, how I have been using these small shortcuts to save time during development. Thank you for reading.


回答1:


Put it inside <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:


An update is needed for the accepted answer: the T 'hash' method is creating a hash with the right number of keys but the values for all keys are duplicated with the last tenant name (0 => 'public', 1 => 'test', 2 => 'test' .. x => 'test'). Here's a working 'hash' method:

def hash
  @@hash ||= Hash[(0..T.names.size - 1).zip T.names]
end


来源:https://stackoverflow.com/questions/53251640/apartmenttenant-switch-during-bin-rails-console-using-pry

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!