Method: ResourcesController#resources_controller_for

Defined in:
lib/resources_controller.rb

#resources_controller_for(name, options = {}, &block) ⇒ Object

Specifies that this controller is a REST style controller for the named resource

Enclosing resources are loaded automatically by default, you can turn this off with :load_enclosing (see options below)

resources_controller_for <name>, <options>, <&block>

Options:

  • :singleton: (default false) set this to true if the resource is a Singleton

  • :find: (default null) set this to a symbol or Proc to specify how to find the resource. Use this if the resource is found in an unconventional way. Passing a block has the same effect as setting :find => a Proc

  • :in: specify the enclosing resources, by name. ClassMethods#nested_in can be used to specify this more fully.

  • :load_enclosing: (default true) loads enclosing resources automatically.

  • :actions: (default nil) set this to false if you don’t want the default RC actions. Set this to a module to use that module for your own actions.

  • :only: only include the specified actions.

  • :except: include all actions except the specified actions.

Options for unconvential use

(otherwise these are all inferred from the name)

  • :route: the route name (without name_prefix) if it can’t be inferred from name. For a collection resource this should be plural, for a singleton it should be singular.

  • :source: a string or symbol (e.g. :users, or :user). This is used to find the class or association name

  • :class: a Class. This is the class of the resource (if it can’t be inferred from name or :source)

  • :segment: (e.g. ‘users’) the segment name in the route that is matched

The :in option

The default behavior is to set up before filters that load the enclosing resource, and to use associations on that model to find and create the resources. See ClassMethods#nested_in for more details on this, and customising the default behaviour.

load_enclosing_resources

By default, a before_action is added by resources_controller called :load_enclosing_resources - which does all the work of loading the enclosing resources. You can use ActionControllers standard filter mechanisms to control when this filter is invoked. For example - you can choose not to load resources on an action

resources_controller_for :foos
skip_before_action :load_enclosing_resources, :only => :static_page

Or, you can change the order of when the filter is invoked by adding the filter call yourself (rc will only add the filter if it doesn’t exist)

before_action :do_something
prepend_before_action :load_enclosing_resources
resources_controller_for :foos
before_action :do_something_else     # chain => [:load_enclosing_resources, :do_something, :do_something_else]

Default actions module

If you have your own actions module you prefer to use other than the standard resources_controller ones you can set ResourcesController.actions to that module to have this be included by default

ResourcesController.actions = MyAwesomeActions
ResourcesController.singleton_actions = MyAweseomeSingletonActions

class AwesomenessController < ApplicationController
  resources_controller_for :awesomenesses # includes MyAwesomeActions by default
end
[View source] [View on GitHub]

453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
# File 'lib/resources_controller.rb', line 453

def resources_controller_for(name, options = {}, &block)
  options.assert_valid_keys(:class, :source, :singleton, :actions, :in, :find, :load_enclosing, :route, :segment, :as, :only, :except, :resource_methods)
  when_options = {:only => options.delete(:only), :except => options.delete(:except)}

  unless included_modules.include? ResourcesController::InstanceMethods
    class_attribute :specifications, :route_name
    extend  ResourcesController::ClassMethods
    helper  ResourcesController::Helper
    include ResourcesController::InstanceMethods, ResourcesController::NamedRouteHelper
    include ResourcesController::ResourceMethods unless options.delete(:resource_methods) == false || included_modules.include?(ResourcesController::ResourceMethods)
  end

  before_action(:load_enclosing_resources, when_options.dup) unless load_enclosing_resources_filter_exists?

  self.specifications = []
  specifications << '*' unless options.delete(:load_enclosing) == false

  unless (actions = options.delete(:actions)) == false
    actions ||= options[:singleton] ? ResourcesController.singleton_actions : ResourcesController.actions
    include_actions actions, when_options
  end

  route = (options.delete(:route) || name).to_s
  name = options[:singleton] ? name.to_s : name.to_s.singularize
  self.route_name = options[:singleton] ? route : route.singularize

  nested_in(*options.delete(:in)) if options[:in]

  class_attribute :resource_specification, :instance_writer => false
  self.resource_specification = Specification.new(name, options, &block)
end