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]) rescue nil }
end
The find can also fall back to using params when :find_on_id is specified. The following are equivalent:
class WidgetController < ActionController::Base
assume :widget, :find_on_id => true
end
class WidgetController < ActionController::Base
assume(:widget) { Widget.find(params[:widget_id] || params[:id]) rescue nil }
end
The find will, by default, swallow errors encountered when finding. This can be overridden by setting :raise_error.
class WidgetController < ActionController::Base
assume :widget, :raise_error => true
end
class WidgetController < ActionController::Base
assume(:widget) { Widget.find(params[:widget_id]) }
end
Both of these settings can be turned on by default via configuration options, such as:
conf.active_record.raise_error = true
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.
58 59 60 61 62 63 |
# File 'lib/basic_assumption/default_assumption/rails.rb', line 58 def block klass = self.class Proc.new do |name, context| klass.new(name, context, params).result end end |
#result ⇒ Object
:nodoc:
65 66 67 68 69 70 71 72 |
# File 'lib/basic_assumption/default_assumption/rails.rb', line 65 def result #:nodoc: begin model_class.find(lookup_id) rescue raise if settings[:raise_error] nil end end |