Skip to content

Commit

Permalink
[Resolves #14] Adds custom console methods
Browse files Browse the repository at this point in the history
  • Loading branch information
rpbaltazar committed Mar 23, 2020
1 parent aef2461 commit 085e8e5
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 19 deletions.
31 changes: 12 additions & 19 deletions lib/apartment/console.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,18 @@ def reload!(print = true)
true
end


module Apartment
module CustomConsole
def st(schema_name = nil)
if schema_name.nil?
tenant_list.each { |t| puts t }
else
Apartment::Tenant.switch!(schema_name) if Apartment::Tenant.include? schema_name
end
end

def tenant_list
tenant_list = ['public']
Apartment::Tenant.each do |t|
tenant_list << t
end
tenant_list
end
def st(schema_name = nil)
if schema_name.nil?
tenant_list.each { |t| puts t }
else
Apartment::Tenant.switch!(schema_name) if tenant_list.include? schema_name
end
end

include Apartment::CustomConsole
def tenant_list
tenant_list = [Apartment.default_tenant]
Apartment::Tenant.each do |t|
tenant_list << t
end
tenant_list.uniq
end
24 changes: 24 additions & 0 deletions lib/apartment/custom_console.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

module Apartment
module CustomConsole
begin
require 'pry-rails'
rescue LoadError
puts '[Failed to load pry-rails] If you want to use Apartment custom prompt you need to add pry-rails to your gemfile'
end

if Pry::Prompt.respond_to?(:add)
desc = "Includes the current Rails environment and project folder name.\n" \
'[1] [project_name][Rails.env][Apartment::Tenant.current] pry(main)>'

Pry::Prompt.add 'ros', desc, %w[> *] do |target_self, nest_level, pry, sep|
"[#{pry.input_ring.size}] [#{PryRails::Prompt.formatted_env}][#{Apartment::Tenant.current}] " \
"#{pry.config.prompt_name}(#{Pry.view_clip(target_self)})" \
"#{":#{nest_level}" unless nest_level.zero?}#{sep} "
end

Pry.config.prompt = Pry::Prompt[:ros][:value]
end
end
end

0 comments on commit 085e8e5

Please sign in to comment.