Class: SolidusActsAsTenant::Utils::TenantSelector

Inherits:
Object
  • Object
show all
Defined in:
lib/solidus_acts_as_tenant/utils/tenant_selector.rb

Constant Summary collapse

MESSAGES =
{
  available: "Available tenants: %<tenants>s",
  prompt: "Select tenant: ",
  not_found: "Tenant not found in list '%<tenants>s'",
  current: "You are now Tenant '%<name>s'"
}.freeze

Instance Method Summary collapse

Instance Method Details

#askObject



13
14
15
16
17
18
19
20
21
# File 'lib/solidus_acts_as_tenant/utils/tenant_selector.rb', line 13

def ask
  return if tenants.empty?

  puts format(MESSAGES[:available], tenants: tenants) # rubocop:disable Rails/Output
  print MESSAGES[:prompt] # rubocop:disable Rails/Output

  tenant_name = ENV["DEFAULT_TENANT"] || gets.strip
  switch_tenant!(tenant_name) unless tenant_name.empty?
end

#currentObject



41
42
43
# File 'lib/solidus_acts_as_tenant/utils/tenant_selector.rb', line 41

def current
  ActsAsTenant.current_tenant&.name
end

#switch_tenant!(tenant_name) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/solidus_acts_as_tenant/utils/tenant_selector.rb', line 27

def switch_tenant!(tenant_name)
  if exists?(tenant_name)
    switch(tenant_name)
  elsif numeric_tenant?(tenant_name)
    switch(tenants[tenant_name.to_i])
  else
    puts format(MESSAGES[:not_found], tenants: tenants) # rubocop:disable Rails/Output
    return false
  end

  puts format(MESSAGES[:current], name: current) # rubocop:disable Rails/Output
  true
end

#tenantsObject



23
24
25
# File 'lib/solidus_acts_as_tenant/utils/tenant_selector.rb', line 23

def tenants
  @tenants ||= fetch_tenants
end