Module: Oasis::Dependencies

Included in:
ActiveSupport::Dependencies
Defined in:
lib/oasis/dependencies.rb

Instance Method Summary collapse

Instance Method Details

#require_or_load(file, const_path = nil) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/oasis/dependencies.rb', line 8

def require_or_load(file, const_path=nil)
  super(file, const_path) unless defined? RAILS_ROOT
  file_loaded = false

  plugin_file_name, app_file_name = "", ""      
  # get out the peices we want for them requirement.
  file_name, base_name = extract_file_name(file), extract_base_name(file)
  
  # start, by looking for the files as helpers or controllers
  # if they are suffixed with helper or controller.
  if file_name =~ /_(helper|controller)?$/
    type = file_name.split("_").last
    Oasis::Debug.log "searching for #{type} named '#{file_name}'"
    Oasis.apps.each do |app|
      plugin_file_name = File.expand_path(File.join(app.directory, "app", "#{type}s", base_name))
      Oasis::Debug.log "checking engine '#{app.name}' for '#{file_name}' (#{plugin_file_name})"
      break if File.file? "#{plugin_file_name}.rb"
    end
    
  else
    
    # if we didn't find it in helpers or controllers, lets try looking in models.
    Oasis::Debug.log "searching for models named '#{file_name}'"
    Oasis.apps.each do |app|
      plugin_file_name = File.expand_path(File.join(app.directory, "app", "models", base_name))
      Oasis::Debug.log "checking engine '#{app.name}' for '#{file_name}' (#{plugin_file_name})"
      break if File.file? "#{plugin_file_name}.rb"
    end
    
  end
  
  # success! we've found code in the engine, lets load it.
  if File.file?("#{plugin_file_name}.rb")
    Oasis::Debug.log "loading '#{file_name}' from an engine."
    file_loaded = true if super(plugin_file_name, const_path)
  end
  
  app_file_name = File.join(RAILS_ROOT, "app", "#{type||"model"}s", base_name)
  if File.file? "#{app_file_name}.rb"
    Oasis::Debug.log "loading from application: #{file_name}" 
    file_loaded = true if super(app_file_name, const_path)
  else
    Oasis::Debug.log "file '#{file_name}' not found in application"
  end
  
  # if we managed to load a file, return true. If not, default to the original method.
  # Note that this relies on the RHS of a boolean || not to be evaluated if the LHS is true.
  file_loaded || super(file, const_path)
end