Class: Custom::MultiCompany

Inherits:
Object
  • Object
show all
Defined in:
lib/custom/multi_company.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, company_class, company_name_matcher) ⇒ MultiCompany

Returns a new instance of MultiCompany.



8
9
10
11
12
13
14
# File 'lib/custom/multi_company.rb', line 8

def initialize(app, company_class, company_name_matcher)
  @app = app
  @company_class_name = company_class.to_s.split('_').collect!{ |w| w.capitalize }.join
  @company = Module.const_get(@company_class_name).find_by_company_name('DEFAULT') if db_configured
  @company_name_matcher = Module.const_get(
    company_name_matcher.to_s.split('_').collect!{ |w| w.capitalize }.join)
end

Instance Attribute Details

#company_class_nameObject (readonly)

Returns the value of attribute company_class_name.



6
7
8
# File 'lib/custom/multi_company.rb', line 6

def company_class_name
  @company_class_name
end

Instance Method Details

#call(env) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/custom/multi_company.rb', line 16

def call(env)
  request = Rack::Request.new(env)

  # - the matcher is an injected class with a to_domain method that makes it easy to replace
  # - with a different scheme of obtaining the company name from Request/Url.
  domain = @company_name_matcher.to_domain(request)

  # insert the company into ENV - for the controller helper_method 'current_company'
  retrieve_company_from_subdomain(domain.upcase)
  env['COMPANY_ID'] = @company.id unless @company.nil?
  @app.call(env)
end