Module: Dependencies

Defined in:
lib/productize.rb

Overview

Load site-specific controllers and models and let them re-open their corresponding class

Instance Method Summary collapse

Instance Method Details

#require_or_load(file_name) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/productize.rb', line 5

def require_or_load(file_name)
  file_name = "#{file_name}.rb" unless ! load? || file_name[-3..-1] == '.rb'
  begin
    load? ? load(file_name) : require(file_name)
  rescue LoadError => e
    # raise an error if this isn't a site-specific model
    raise unless File.exist?(File.join(SITE_ROOT, 'app', 'models', File.basename(file_name)))
  end
  # try loading site-specific controller
  if file_name.include? 'controller'
    file_name = File.join(SITE_ROOT, 'app', 'controllers', File.basename(file_name))
    if File.exist? file_name
      load? ? load(file_name) : require(file_name)
    end
  end
  # try loading site-specific model
  file_name = File.join(SITE_ROOT, 'app', 'models', File.basename(file_name))
  if File.exist? file_name
    load? ? load(file_name) : require(file_name)
  end
end