module ControllerResources

A mixin for ActionController for easily and consistently finding resource objects to be used in the controller and view layer.

Constants

VERSION

Current version of this gem.

Public Instance Methods

resource(name, ancestor: nil, finder: :find, searcher: :where, param: :id, class_name: nil) click to toggle source

Define the resource this controller will be manipulating, and call the :find_resource callback before most actions in order to set up the resource automatically.

@param [Symbol] name - Lowercased name of the resource. @option [Symbol] :ancestor - Optional ancestor ivar name.

# File lib/controller_resources.rb, line 27
def resource(name, ancestor: nil, finder: :find, searcher: :where, param: :id, class_name: nil)
  self.resource_name = name.to_s
  self.resource_ancestor_name = ancestor.to_s if ancestor.present?
  self.resource_search_method = searcher
  self.resource_finder_method = finder
  self.resource_id_param = param
  self.resource_class_name = class_name || resource_name.classify

  before_action :find_resource, except: [:new, :create]
end

Protected Instance Methods

ancestor() click to toggle source

Find the ancestor of this model if it's defined. Used in the model_class to use as the base object by which we derive models in this controller.

@protected @return [Object] or nil if the ancestor was not configured.

# File lib/controller_resources.rb, line 83
def ancestor
  return unless resource_ancestor_name.present?
  instance_variable_get "@#{resource_ancestor_name}"
end
collection() click to toggle source

Find the collection by its search params.

@protected @return [Object]

# File lib/controller_resources.rb, line 65
def collection
  model_class.send resource_search_method, search_params
end
find_resource() click to toggle source

Runs before every action to determine its resource in an instance variable.

@protected

# File lib/controller_resources.rb, line 53
def find_resource
  if collection?
    instance_variable_set "@#{plural_resource_name}", collection
  else
    instance_variable_set "@#{resource_name}", model
  end
end
model() click to toggle source

Find the model by its ID, or return a new model.

@protected @return [Object]

# File lib/controller_resources.rb, line 73
def model
  model_class.send resource_finder_method, resource_id
end
plural_resource_name() click to toggle source

Pluralized name of the resource.

@protected @return [String]

# File lib/controller_resources.rb, line 45
def plural_resource_name
  resource_name.pluralize
end

Private Instance Methods

ancestor_resource() click to toggle source
# File lib/controller_resources.rb, line 114
def ancestor_resource
  return unless ancestor.present?
  return unless ancestor.respond_to? plural_resource_name
  ancestor.send plural_resource_name
end
collection?() click to toggle source

Test whether current action_name is in the collection_actions Array.

@return [Boolean] true if action needs a collection as its resource.

# File lib/controller_resources.rb, line 94
def collection?
  collection_actions.include? action_name.to_sym
end
model_class() click to toggle source

@private @return [Class] Class constant for the given resource derived from resource_name.

# File lib/controller_resources.rb, line 110
def model_class
  ancestor_resource || resource_class_name.constantize
end
resource_id() click to toggle source

Override to provide your own finder param.

@private @return [String] Param used to find a model, by default params[:id]

# File lib/controller_resources.rb, line 124
def resource_id
  params[resource_id_param]
end
search_params() click to toggle source

Override this method to provide your own search params.

@private @return [ActionController::Parameters] Params given to the search method.

# File lib/controller_resources.rb, line 103
def search_params
  params.permit!.except(:controller, :action, :format)
end