ruby-on-railsconsolemulti-tenantpryapartment-gem

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


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.


Solution

  • 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