在bin/rails console期间使用`pry`的`Apartment :: Tenant.switch!` [英] `Apartment::Tenant.switch!` during `bin/rails console` using `pry`

查看:119
本文介绍了在bin/rails console期间使用`pry`的`Apartment :: Tenant.switch!`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  • 启动console
  • console提示符下
  • when console is launched
  • while at console prompt

在此处查看输出.简单,快捷的方法. T.me(当前租户),T.names(数据库中的租户),...

See the output here. Simple, quick methods. T.me (current tenant), T.names (tenants in the 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是房客吗?

Is abc a tenant?

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

local是房客吗?

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.

推荐答案

将其放入<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

这篇关于在bin/rails console期间使用`pry`的`Apartment :: Tenant.switch!`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆