Module: AdequateExposure::Behavior

Defined in:
lib/adequate_exposure/behavior.rb

Instance Method Summary collapse

Instance Method Details

#build(params, scope) ⇒ Object

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.



63
64
65
# File 'lib/adequate_exposure/behavior.rb', line 63

def build(params, scope)
  scope.new(params)
end

#build_paramsObject

Public: Get all the parameters of the current request.

Returns the controller’s parameters for the current request.



78
79
80
81
82
83
84
# 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) ⇒ Object

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

Returns the decorated object.



71
72
73
# File 'lib/adequate_exposure/behavior.rb', line 71

def decorate(instance)
  instance
end

#fetchObject

Public: Fetches a scope.

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

Returns the decorated object.



8
9
10
11
# 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) ⇒ Object

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.



53
54
55
# File 'lib/adequate_exposure/behavior.rb', line 53

def find(id, scope)
  scope.find(id)
end

#idObject

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.



18
19
20
21
22
23
24
25
# 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

#modelObject

Public: Converts a name into a standard Class name.

Examples

'egg_and_hams'.model # => EggAndHam

Returns a standard Class name.



43
44
45
# File 'lib/adequate_exposure/behavior.rb', line 43

def model
  name.to_s.classify.constantize
end

#scope(model) ⇒ Object

Public: An object query. Essentially, this method is designed to be overridden.

model - The Class to be scoped or queried.

Returns the object scope.



33
34
35
# File 'lib/adequate_exposure/behavior.rb', line 33

def scope(model)
  model
end