module Restful::Base::InstanceMethods
Instance helper methods for a Restful
controller
Public Instance Methods
Helper method that allows you to refer to the instance variable that represents the collection of objects from the model name defined in the restful macro.
# File lib/restful/base.rb, line 32 def collection get_collection_ivar || begin set_collection_ivar class_name.all end end
This is a helper method to get the object collection path.
# File lib/restful/base.rb, line 72 def collection_path send route_prefix_to_method_name("#{class_name.model_name.route_key}_path") end
This is a helper method to get the object collection url.
# File lib/restful/base.rb, line 78 def collection_url send route_prefix_to_method_name("#{class_name.model_name.route_key}_url") end
Generic route path helper method to edit a model.
# File lib/restful/base.rb, line 46 def edit_resource_path(object) send route_prefix_to_method_name("edit_#{class_name.model_name.singular_route_key}_path"), object end
Generic route url helper method to edit a model.
# File lib/restful/base.rb, line 53 def edit_resource_url(object) send route_prefix_to_method_name("edit_#{class_name.model_name.singular_route_key}_url"), object end
Generic route path helper method for new model.
# File lib/restful/base.rb, line 60 def new_resource_path send route_prefix_to_method_name("new_#{class_name.model_name.singular_route_key}_path") end
Generic route url helper method for new model.
# File lib/restful/base.rb, line 66 def new_resource_url send route_prefix_to_method_name("new_#{class_name.model_name.singular_route_key}_url") end
Helper method that allows you to refer to the instance variable that represent a single object from the model name defined in the restful macro.
# File lib/restful/base.rb, line 24 def resource get_resource_ivar end
Helper method that gives you access to the class of your model.
# File lib/restful/base.rb, line 40 def resource_class class_name end
Protected Instance Methods
Get an object from instance variable if it exists, if not then build a new object.
# File lib/restful/base.rb, line 146 def build_resource get_resource_ivar || begin set_resource_ivar class_name.new end end
Return the instance variable name for a collection of objects based on the model name defined in the restful macro, example:
@documents
# File lib/restful/base.rb, line 108 def collection_ivar "@#{class_name.model_name.plural}" end
Creates a new object using request params, tries to save the object and set it to the object's instance variable.
# File lib/restful/base.rb, line 155 def create_resource class_name.new(get_secure_params).tap do |model| model.save set_resource_ivar model end end
Find an object, update attributes from params and set instance variable.
# File lib/restful/base.rb, line 235 def find_and_update_resource model = class_name.find(params[:id]) model.tap do |m| m.update get_secure_params set_resource_ivar m end end
Find an object using the param id and set the object to the instance variable.
# File lib/restful/base.rb, line 228 def find_resource set_resource_ivar class_name.find(params[:id]) end
Get the collection of objects from an instance variable.
# File lib/restful/base.rb, line 129 def get_collection_ivar # rubocop:disable Naming/AccessorMethodName instance_variable_get collection_ivar end
Get the object from a single object instance variable.
# File lib/restful/base.rb, line 114 def get_resource_ivar # rubocop:disable Naming/AccessorMethodName instance_variable_get resource_ivar end
Calls template methods for getting params curated by strong params This method calls to a 3 different hook methods in the controller. First depending on the action :create or :update the controller is tested to have a create_secure_params or update_secure_params method, if method is not present then it tries with a generic method secure_params.
If no template method is implemented in the controller then a NotImplementedError exception is raised.
# File lib/restful/base.rb, line 172 def get_secure_params # rubocop:disable Naming/AccessorMethodName params_method = "#{action_name}_secure_params".to_sym filterd_params = (send(params_method) if respond_to?(params_method, true)) || (send(:secure_params) if respond_to?(:secure_params, true)) unless filterd_params raise NotImplementedError, "You need to define template methods for strong params" end filterd_params end
Return the instance variable name for a single object based on the model name defined in the restful macro, example:
@document
# File lib/restful/base.rb, line 99 def resource_ivar "@#{class_name.model_name.singular}" end
This is a special method used by create and update actions that allows these methods to receive two blocks, success and failure blocks.
# File lib/restful/base.rb, line 190 def respond_with_dual(object, options, &block) args = [object, options] set_flash options case block.try(:arity) when 2 respond_with(*args) do |responder| dummy_responder = Restful::DummyResponder.new if get_resource_ivar.errors.empty? block.call responder, dummy_responder else block.call dummy_responder, responder end end when 1 respond_with(*args, &block) else options[:location] = block.call if block respond_with(*args) end end
Return a url helper method name with additional route prefix if set. If route_prefix param is set to `admin` then the method name will be:
edit_resource_path => admin_edit_resource_path
# File lib/restful/base.rb, line 90 def route_prefix_to_method_name(method) "#{route_prefix + "_" if route_prefix}#{method}" end
Set the instance variable for a collection of objects
Params¶ ↑
-
objects: The objects collections to be stored in the instance
variable.
# File lib/restful/base.rb, line 139 def set_collection_ivar(objects) # rubocop:disable Naming/AccessorMethodName instance_variable_set collection_ivar, objects end
This method set the flash messages if they are passed within the action
Params¶ ↑
-
options: a hash with :notice or :alert messages
# File lib/restful/base.rb, line 217 def set_flash(options = {}) # rubocop:disable Naming/AccessorMethodName if options.key?(:notice) flash[:notice] = options[:notice] elsif options.key?(:alert) flash[:alert] = options[:alert] end end