Class: BasicAssumption::DefaultAssumption::Rails
- Defined in:
- lib/basic_assumption/default_assumption/rails.rb
Overview
Custom default behavior in the context of Rails.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
:nodoc:.
-
#name ⇒ Object
readonly
:nodoc:.
-
#params ⇒ Object
readonly
:nodoc:.
Instance Method Summary collapse
-
#block ⇒ Object
Returns a block that will attempt to find an instance of an ActiveRecord model based on the name that was given to BasicAssumption#assume and an id value in the parameters.
-
#initialize(name = nil, context = {}, params = {}) ⇒ Rails
constructor
:nodoc:.
-
#result ⇒ Object
:nodoc:.
Constructor Details
#initialize(name = nil, context = {}, params = {}) ⇒ Rails
:nodoc:
7 8 9 |
# File 'lib/basic_assumption/default_assumption/rails.rb', line 7 def initialize(name=nil, context={}, params={}) #:nodoc: @name, @context, @params = name.to_s, context, params end |
Instance Attribute Details
#context ⇒ Object (readonly)
:nodoc:
5 6 7 |
# File 'lib/basic_assumption/default_assumption/rails.rb', line 5 def context @context end |
#name ⇒ Object (readonly)
:nodoc:
5 6 7 |
# File 'lib/basic_assumption/default_assumption/rails.rb', line 5 def name @name end |
#params ⇒ Object (readonly)
:nodoc:
5 6 7 |
# File 'lib/basic_assumption/default_assumption/rails.rb', line 5 def params @params end |
Instance Method Details
#block ⇒ Object
Returns a block that will attempt to find an instance of an ActiveRecord model based on the name that was given to BasicAssumption#assume and an id value in the parameters. The following two examples would be equivalent:
class WidgetController < ActionController::Base
assume :widget
end
class WidgetController < ActionController::Base
assume(:widget) { Widget.find(params[:widget_id] || params[:id]) }
end
It is possible to specify an alternative model name:
class WidgetController < ApplicationController
assume :sprocket, :as => :widget
end
This will create a sprocket method in your actions and view that will use the Widget model for its lookup.
31 32 33 34 35 36 |
# File 'lib/basic_assumption/default_assumption/rails.rb', line 31 def block klass = self.class Proc.new do |name, context| klass.new(name, context, params).result end end |
#result ⇒ Object
:nodoc:
38 39 40 |
# File 'lib/basic_assumption/default_assumption/rails.rb', line 38 def result #:nodoc: model_class.find(lookup_id) end |