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











up vote
0
down vote

favorite













  • 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









share|improve this question


























    up vote
    0
    down vote

    favorite













    • 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









    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite












      • 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









      share|improve this question














      • 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






      ruby-on-rails console multi-tenant pry apartment-gem






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 11 at 18:04









      ramonrails

      1,0051018




      1,0051018
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          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





          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53251640%2fapartmenttenant-switch-during-bin-rails-console-using-pry%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote



            accepted










            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





            share|improve this answer



























              up vote
              0
              down vote



              accepted










              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





              share|improve this answer

























                up vote
                0
                down vote



                accepted







                up vote
                0
                down vote



                accepted






                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





                share|improve this answer














                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






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 12 at 7:03

























                answered Nov 11 at 18:04









                ramonrails

                1,0051018




                1,0051018






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53251640%2fapartmenttenant-switch-during-bin-rails-console-using-pry%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Bressuire

                    Vorschmack

                    Quarantine