Module: ActionController::Dependencies::ClassMethods

Defined in:
lib/action_controller/dependencies.rb

Overview

Dependencies control what classes are needed for the controller to run its course. This is an alternative to doing explicit require statements that bring a number of benefits. It’s more succinct, communicates what type of dependency we’re talking about, can trigger special behavior (as in the case of observer), and enables Rails to be clever about reloading in cached environments like FCGI. Example:

class ApplicationController < ActionController::Base
  model    :account, :company, :person, :project, :category
  helper   :access_control
  service  :notifications, :billings
  observer :project_change_observer
end

Please note that a controller like ApplicationController will automatically attempt to require_dependency on a model of its singuralized name and a helper of its name. If nothing is found, no error is raised. This is especially useful for concrete controllers like PostController:

class PostController < ApplicationController
  # model  :post (already required)
  # helper :post (already required)
end

Instance Method Summary collapse

Instance Method Details

#depend_on(layer, dependencies) ⇒ Object

:nodoc:



77
78
79
# File 'lib/action_controller/dependencies.rb', line 77

def depend_on(layer, dependencies) #:nodoc:
  write_inheritable_array("#{layer}_dependencies", dependencies)
end

#dependencies_on(layer) ⇒ Object

Returns an array of symbols that specify the dependencies on a given layer. For the example at the top, calling ApplicationController.dependencies_on(:model) would return [:account, :company, :person, :project, :category]



73
74
75
# File 'lib/action_controller/dependencies.rb', line 73

def dependencies_on(layer)
  read_inheritable_attribute("#{layer}_dependencies")
end

#model(*models) ⇒ Object

Specifies a variable number of models that this controller depends on. Models are normally Active Record classes or a similar backend for modelling entity classes.



51
52
53
54
# File 'lib/action_controller/dependencies.rb', line 51

def model(*models)
  require_dependencies(:model, models)
  depend_on(:model, models)
end

#observer(*observers) ⇒ Object

Specifies a variable number of observers that are to govern when this controller is handling actions. The observers will automatically have .instance called on them to make them active on assignment.



65
66
67
68
69
# File 'lib/action_controller/dependencies.rb', line 65

def observer(*observers)
  require_dependencies(:observer, observers)
  depend_on(:observer, observers)
  instantiate_observers(observers)
end

#require_dependency(file_name) ⇒ Object

Loads the file_name if reload_dependencies is true or requires if it’s false.



45
46
47
# File 'lib/action_controller/dependencies.rb', line 45

def require_dependency(file_name)
  reload_dependencies ? silence_warnings { load("#{file_name}.rb") } : require(file_name)
end

#service(*services) ⇒ Object

Specifies a variable number of services that this controller depends on. Services are normally singletons or factories, like Action Mailer service or a Payment Gateway service.



58
59
60
61
# File 'lib/action_controller/dependencies.rb', line 58

def service(*services)
  require_dependencies(:service, services)
  depend_on(:service, services)
end