module DecentExposure::Behavior
Public Instance Methods
Public: Builds a new object on the passed-in scope.
params - A Hash of attributes for the object to-be built. scope - The collection that will be searched.
Returns the new object.
# File lib/decent_exposure/behavior.rb, line 63 def build(params, scope) scope.new(params) end
Public: Get all the parameters of the current request.
Returns the controller's parameters for the current request.
# File lib/decent_exposure/behavior.rb, line 78 def build_params if controller.respond_to?(params_method_name, true) && !get_request? controller.send(params_method_name) else {} end end
Public: Returns a decorated object. This method is designed to be overridden.
Returns the decorated object.
# File lib/decent_exposure/behavior.rb, line 71 def decorate(instance) instance end
Public: Fetches a scope.
Finds an object. If it isn't found, the object gets instantiated.
Returns the decorated object.
# File lib/decent_exposure/behavior.rb, line 8 def fetch instance = id ? find(id, computed_scope) : build(build_params, computed_scope) decorate(instance) end
Public: Find an object on the supplied scope.
id - The Integer id attribute of the desired object scope - The collection that will be searched.
Returns the found object.
# File lib/decent_exposure/behavior.rb, line 53 def find(id, scope) scope.find(id) end
Public: Checks a params hash for an id attribute.
Checks a hash of parameters for keys that represent an object's id.
Returns the value of the id parameter, if it exists. Otherwise nil.
# File lib/decent_exposure/behavior.rb, line 18 def id params_id_key_candidates.each do |key| value = params[key] return value if value.present? end nil end
Public: Converts a name into a standard Class name.
Examples
'egg_and_hams'.model # => EggAndHam
Returns a standard Class name.
# File lib/decent_exposure/behavior.rb, line 43 def model name.to_s.classify.constantize end
Public: An object query. Essentially, this method is designed to be overridden.
model - The Class to be scoped or queried.
Returns the object scope.
# File lib/decent_exposure/behavior.rb, line 33 def scope(model) model end
Protected Instance Methods
# File lib/decent_exposure/behavior.rb, line 96 def computed_scope scope(model) end
# File lib/decent_exposure/behavior.rb, line 92 def model_param_key model.name.underscore end
# File lib/decent_exposure/behavior.rb, line 88 def params_id_key_candidates ["#{model_param_key}_id", "#{name}_id", "id"].uniq end