module InheritedResources::BaseHelpers
Base helpers for InheritedResource work. Some methods here can be overwritten and you will need to do that to customize your controllers from time to time.
Protected Instance Methods
Returns the association chain, with all parents (does not include the current resource).
# File lib/inherited_resources/base_helpers.rb, line 130 def association_chain @association_chain ||= symbols_for_association_chain.inject([begin_of_association_chain]) do |chain, symbol| chain << evaluate_parent(symbol, resources_configuration[symbol], chain.last) end.compact.freeze end
This class allows you to set a instance variable to begin your association chain. For example, usually your projects belongs to users and that means that they belong to the current logged in user. So you could do this:
def begin_of_association_chain @current_user end
So every time we instantiate a project, we will do:
@current_user.projects.build(params[:project]) @current_user.projects.find(params[:id])
The variable set in begin_of_association_chain
is not sent when building urls, so this is never going to happen when calling resource_url:
project_url(@current_user, @project)
If the user actually scopes the url, you should use belongs_to method and declare that projects belong to user.
# File lib/inherited_resources/base_helpers.rb, line 116 def begin_of_association_chain nil end
This method is responsible for building the object on :new and :create methods. If you overwrite it, don’t forget to cache the result in an instance variable.
# File lib/inherited_resources/base_helpers.rb, line 53 def build_resource get_resource_ivar || set_resource_ivar(end_of_association_chain.send(method_for_build, *resource_params)) end
This is how the collection is loaded.
You might want to overwrite this method if you want to add pagination for example. When you do that, don’t forget to cache the result in an instance_variable:
def collection @projects ||= end_of_association_chain.paginate(params[:page]).all end
# File lib/inherited_resources/base_helpers.rb, line 24 def collection get_collection_ivar || begin c = end_of_association_chain set_collection_ivar(c.respond_to?(:scoped) ? c.scoped : c.all) end end
Responsible for saving the resource on :create method. Overwriting this allow you to control the way resource is saved. Let’s say you have a PasswordsController who is responsible for finding an user by email and sent password instructions for him. Instead of overwriting the entire :create method, you could do something:
def create_resource(object) object.send_instructions_by_email end
# File lib/inherited_resources/base_helpers.rb, line 67 def create_resource(object) object.save end
Handle the :destroy method for the resource. Overwrite it to call your own method for destroying the resource, as:
def destroy_resource(object) object.cancel end
# File lib/inherited_resources/base_helpers.rb, line 90 def destroy_resource(object) object.destroy end
Returns if the controller has a parent. When only base helpers are loaded, it’s always false and should not be overwritten.
# File lib/inherited_resources/base_helpers.rb, line 123 def parent? false end
This is how the resource is loaded.
You might want to overwrite this method when you are using permalink. When you do that, don’t forget to cache the result in an instance_variable:
def resource @project ||= end_of_association_chain.find_by_permalink!(params[:id]) end
You also might want to add the exclamation mark at the end of the method because it will raise a 404 if nothing can be found. Otherwise it will probably render a 500 error message.
# File lib/inherited_resources/base_helpers.rb, line 45 def resource get_resource_ivar || set_resource_ivar(end_of_association_chain.send(method_for_find, params[:id])) end
Responsible for updating the resource in :update method. This allow you to handle how the resource is going to be updated, let’s say in a different way than the usual :update:
def update_resource(object, attributes) object.reset_password!(attributes) end
# File lib/inherited_resources/base_helpers.rb, line 79 def update_resource(object, attributes) object.update(*attributes) end
Private Instance Methods
getting role for mass-assignment
# File lib/inherited_resources/base_helpers.rb, line 374 def as_role { as: self.resources_configuration[:self][:role] } end
extract attributes from params
# File lib/inherited_resources/base_helpers.rb, line 356 def build_resource_params parameters = permitted_params || params rparams = [parameters[resource_request_name] || parameters[resource_instance_name] || {}] if without_protection_given? rparams << without_protection else rparams << as_role if role_given? end rparams end
Returns the name of the method used for building the resource in cases where we have a parent. This is overwritten in singleton scenarios.
# File lib/inherited_resources/base_helpers.rb, line 199 def method_for_association_build :build end
Returns finder method for instantiate resource by params
# File lib/inherited_resources/base_helpers.rb, line 212 def method_for_find resources_configuration[:self][:finder] || :find end
Returns hash of sanitized params in a form like ‘{:project => {:project_attribute => ’value’}}‘
This method makes use of ‘project_params` (or `smth_else_params`) which is a default Rails
controller method for strong parameters definition.
‘permitted_params` is usually fired by method :new, :create, :update actions. Action :new usually has no parameters so strong parameters `require` directive raises a ActionController::ParameterMissing
exception. `#permitted_params` rescues such exceptions in :new and returns an empty hash of parameters (which is reasonable default). If for any reasons you need something more specific, you can redefine this method in a way previous `inherited_resources` versions did:
# Unnecessary redefinition def permitted_params params.permit(:project => [:project_attribute]) end
# File lib/inherited_resources/base_helpers.rb, line 343 def permitted_params return nil unless respond_to?(resource_params_method_name, true) {resource_request_name => send(resource_params_method_name)} rescue ActionController::ParameterMissing # typically :new action if params[:action].to_s == 'new' {resource_request_name => {}} else raise end end
memoize the extraction of attributes from params
# File lib/inherited_resources/base_helpers.rb, line 316 def resource_params @resource_params ||= build_resource_params end
# File lib/inherited_resources/base_helpers.rb, line 320 def resource_params_method_name "#{resource_instance_name}_params" end
# File lib/inherited_resources/base_helpers.rb, line 168 def resource_request_name self.resources_configuration[:self][:request_name] end
checking if role given
# File lib/inherited_resources/base_helpers.rb, line 369 def role_given? self.resources_configuration[:self][:role].present? end
URL to redirect to when redirect implies collection url.
# File lib/inherited_resources/base_helpers.rb, line 304 def smart_collection_url url = nil if respond_to? :index url ||= collection_url rescue nil end if respond_to? :parent, true url ||= parent_url rescue nil end url ||= root_url rescue nil end
URL to redirect to when redirect implies resource url.
# File lib/inherited_resources/base_helpers.rb, line 295 def smart_resource_url url = nil if respond_to? :show url = resource_url rescue nil end url ||= smart_collection_url end
Adds the given object to association chain.
# File lib/inherited_resources/base_helpers.rb, line 152 def with_chain(object) association_chain + [ object ] end
# File lib/inherited_resources/base_helpers.rb, line 382 def without_protection { without_protection: self.resources_configuration[:self][:without_protection] } end
# File lib/inherited_resources/base_helpers.rb, line 378 def without_protection_given? self.resources_configuration[:self][:without_protection].present? end