Module: ResponderController::InstanceMethods
- Defined in:
- lib/responder_controller/instance_methods.rb
Overview
Instance methods that support the Actions module.
Instance Method Summary collapse
-
#find_model ⇒ Object
Find a particular model.
-
#find_models ⇒ Object
Find all models in #scope.
-
#model ⇒ Object
Retrive #model_ivar.
-
#model=(model) ⇒ Object
Assign #model_ivar.
-
#model_ivar ⇒ Object
The name of the instance variable holding a single model instance.
-
#model_slug ⇒ Object
The underscored model class name, as a symbol.
-
#models ⇒ Object
Retrive #models_ivar.
-
#models=(models) ⇒ Object
Assign #models_ivar.
-
#models_ivar ⇒ Object
The name of the instance variable holding a collection of models.
-
#models_slug ⇒ Object
Like #model_slug, but plural.
-
#respond_with_contextual(model) ⇒ Object
Pass
model
through InstanceMethods#responder_context, and pass that to #respond_with. -
#responder_context(model) ⇒ Object
Apply ClassMethods#responds_within to the given model (or symbol.).
-
#scope(query) ⇒ Object
Apply scopes to the given query.
Instance Method Details
#find_model ⇒ Object
Find a particular model.
#find_models is asked to find a model with params[:id]
. This ensures that class-level scopes are enforced (potentially for security.)
60 61 62 63 64 |
# File 'lib/responder_controller/instance_methods.rb', line 60 def find_model models = find_models finder = models.respond_to?(:from_param) ? :from_param : :find models.send finder, params[:id] end |
#find_models ⇒ Object
Find all models in #scope.
The initial query is ClassMethods#model_class.scoped
.
52 53 54 |
# File 'lib/responder_controller/instance_methods.rb', line 52 def find_models scope model_class.scoped end |
#model ⇒ Object
Retrive #model_ivar
99 100 101 |
# File 'lib/responder_controller/instance_methods.rb', line 99 def model instance_variable_get model_ivar end |
#model=(model) ⇒ Object
Assign #model_ivar
104 105 106 |
# File 'lib/responder_controller/instance_methods.rb', line 104 def model=(model) instance_variable_set model_ivar, model end |
#model_ivar ⇒ Object
The name of the instance variable holding a single model instance.
79 80 81 |
# File 'lib/responder_controller/instance_methods.rb', line 79 def model_ivar "@#{model_slug}" end |
#model_slug ⇒ Object
The underscored model class name, as a symbol.
Model modules are omitted.
69 70 71 |
# File 'lib/responder_controller/instance_methods.rb', line 69 def model_slug model_class_name.split('/').last.to_sym end |
#models ⇒ Object
Retrive #models_ivar
89 90 91 |
# File 'lib/responder_controller/instance_methods.rb', line 89 def models instance_variable_get models_ivar end |
#models=(models) ⇒ Object
Assign #models_ivar
94 95 96 |
# File 'lib/responder_controller/instance_methods.rb', line 94 def models=(models) instance_variable_set models_ivar, models end |
#models_ivar ⇒ Object
The name of the instance variable holding a collection of models.
84 85 86 |
# File 'lib/responder_controller/instance_methods.rb', line 84 def models_ivar model_ivar.pluralize end |
#models_slug ⇒ Object
Like #model_slug, but plural.
74 75 76 |
# File 'lib/responder_controller/instance_methods.rb', line 74 def models_slug model_slug.to_s.pluralize.to_sym end |
#respond_with_contextual(model) ⇒ Object
Pass model
through InstanceMethods#responder_context, and pass that to #respond_with.
125 126 127 |
# File 'lib/responder_controller/instance_methods.rb', line 125 def respond_with_contextual(model) respond_with *responder_context(model) end |
#responder_context(model) ⇒ Object
Apply ClassMethods#responds_within to the given model (or symbol.)
“Apply” just means turning responds_within
into an array and appending model
to the end. If responds_within
is an array, it used directly.
If it is a proc, it is called with instance_exec
, passing model
in. It should return an array, which model
will be appended to. (So, don’t include it in the return value.)
116 117 118 119 120 121 |
# File 'lib/responder_controller/instance_methods.rb', line 116 def responder_context(model) context = responds_within.collect do |o| o = instance_exec model, &o if o.is_a? Proc o end.flatten + [model] end |
#scope(query) ⇒ Object
Apply scopes to the given query.
Applicable scopes come from two places. They are either declared at the class level with ClassMethods#scope
, or named in the request itself. The former is good for defining topics or enforcing security, while the latter is free slicing and dicing for clients.
Class-level scopes are applied first. Request scopes come after, and are discovered by examining params
. If any params
key matches a name found in ClassMethods#model_class.scopes.keys
, then it is taken to be a scope and is applied. The values under that params
key are passed along as arguments.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/responder_controller/instance_methods.rb', line 21 def scope(query) query = (scopes || []).inject(query) do |query, scope| if Symbol === scope and model_class.scopes.key? scope query.send scope elsif Proc === scope instance_exec query, &scope else raise ArgumentError.new "Unknown scope #{model_class}.#{scope}" end end requested = model_class.scopes.keys & params.keys.collect(&:to_sym) if serves_scopes[:only] && (requested - serves_scopes[:only]).any? raise ForbiddenScope end if serves_scopes[:except] && (requested & serves_scopes[:except]).any? raise ForbiddenScope end query = requested.inject(query) do |query, scope| query.send scope, *params[scope.to_s] rescue raise BadScope end query end |