module AdequateExposure::Behavior

Public Instance Methods

build(params, scope) click to toggle source

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/adequate_exposure/behavior.rb, line 63
def build(params, scope)
  scope.new(params)
end
build_params() click to toggle source

Public: Get all the parameters of the current request.

Returns the controller's parameters for the current request.

# File lib/adequate_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
decorate(instance) click to toggle source

Public: Returns a decorated object. This method is designed to be overridden.

Returns the decorated object.

# File lib/adequate_exposure/behavior.rb, line 71
def decorate(instance)
  instance
end
fetch() click to toggle source

Public: Fetches a scope.

Finds an object. If it isn't found, the object gets instantiated.

Returns the decorated object.

# File lib/adequate_exposure/behavior.rb, line 8
def fetch
  instance = id ? find(id, computed_scope) : build(build_params, computed_scope)
  decorate(instance)
end
find(id, scope) click to toggle source

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/adequate_exposure/behavior.rb, line 53
def find(id, scope)
  scope.find(id)
end
id() click to toggle source

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/adequate_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
model() click to toggle source

Public: Converts a name into a standard Class name.

Examples

'egg_and_hams'.model # => EggAndHam

Returns a standard Class name.

# File lib/adequate_exposure/behavior.rb, line 43
def model
  name.to_s.classify.constantize
end
scope(model) click to toggle source

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/adequate_exposure/behavior.rb, line 33
def scope(model)
  model
end

Protected Instance Methods

computed_scope() click to toggle source
# File lib/adequate_exposure/behavior.rb, line 96
def computed_scope
  scope(model)
end
model_param_key() click to toggle source
# File lib/adequate_exposure/behavior.rb, line 92
def model_param_key
  model.name.underscore
end
params_id_key_candidates() click to toggle source
# File lib/adequate_exposure/behavior.rb, line 88
def params_id_key_candidates
  [ "#{model_param_key}_id", "#{name}_id", "id" ].uniq
end