`Apartment::Tenant.switch!` 在 `bin/rails console` 期间使用 `pry`
`Apartment::Tenant.switch!` during `bin/rails console` using `pry`
console
启动时
- 在
console
提示时
它应该如何运作?
在此处查看输出。简单、快速的方法。 T.me
(当前租户),T.names
(数据库中的租户),...
启动,请求租户选择,设置
$ 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
注意:这没有经过彻底测试。请在使用前进行测试。这段代码只是给你一些想法,我是如何使用这些小快捷方式来节省开发时间的。感谢阅读。
放在里面<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
已接受的答案需要更新:T 'hash' 方法正在使用正确数量的键创建散列,但所有键的值都与最后一个租户名称重复(0 => 'public', 1 => 'test', 2 => 'test' .. x => 'test')。这是一个有效的 'hash' 方法:
def hash
@@hash ||= Hash[(0..T.names.size - 1).zip T.names]
end
bazfer回答部分正确,忘记了public租客
def hash
@@hash ||= { 0 => 'public' }.merge(Hash[(1..T.names.size).zip T.names])
end
请添加到 bazfer 答案和已接受的答案
console
启动时- 在
console
提示时
它应该如何运作?
在此处查看输出。简单、快速的方法。 T.me
(当前租户),T.names
(数据库中的租户),...
启动,请求租户选择,设置
$ 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
注意:这没有经过彻底测试。请在使用前进行测试。这段代码只是给你一些想法,我是如何使用这些小快捷方式来节省开发时间的。感谢阅读。
放在里面<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
已接受的答案需要更新:T 'hash' 方法正在使用正确数量的键创建散列,但所有键的值都与最后一个租户名称重复(0 => 'public', 1 => 'test', 2 => 'test' .. x => 'test')。这是一个有效的 'hash' 方法:
def hash
@@hash ||= Hash[(0..T.names.size - 1).zip T.names]
end
bazfer回答部分正确,忘记了public租客
def hash
@@hash ||= { 0 => 'public' }.merge(Hash[(1..T.names.size).zip T.names])
end
请添加到 bazfer 答案和已接受的答案