module Restful::Base::InstanceMethods

Instance helper methods for a Restful controller

Public Instance Methods

collection() click to toggle source

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
collection_path() click to toggle source

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
collection_url() click to toggle source

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
edit_resource_path(object) click to toggle source

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
edit_resource_url(object) click to toggle source

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
new_resource_path() click to toggle source

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
new_resource_url() click to toggle source

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
resource() click to toggle source

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
resource_class() click to toggle source

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

build_resource() click to toggle source

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
collection_ivar() click to toggle source

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
create_resource() click to toggle source

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_and_update_resource() click to toggle source

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_resource() click to toggle source

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_collection_ivar() click to toggle source

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_resource_ivar() click to toggle source

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
get_secure_params() click to toggle source

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
resource_ivar() click to toggle source

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
respond_with_dual(object, options, &block) click to toggle source

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
route_prefix_to_method_name(method) click to toggle source

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_collection_ivar(objects) click to toggle source

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
set_flash(options = {}) click to toggle source

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
set_resource_ivar(object) click to toggle source

Set the instance variable for a single object with an object.

Params

  • object: The object to be stored in the instance variable.

# File lib/restful/base.rb, line 123
def set_resource_ivar(object) # rubocop:disable Naming/AccessorMethodName
  instance_variable_set resource_ivar, object
end