Class: BasicAssumption::DefaultAssumption::Rails
- Inherits:
-
Object
- Object
- BasicAssumption::DefaultAssumption::Rails
- Defined in:
- lib/basic_assumption/default_assumption/rails.rb
Overview
Restful default behavior in the context of Rails
Instance Attribute Summary collapse
-
#action ⇒ Object
readonly
:nodoc:.
-
#context ⇒ Object
readonly
:nodoc:.
-
#name ⇒ Object
readonly
:nodoc:.
-
#request ⇒ Object
readonly
:nodoc:.
Instance Method Summary collapse
-
#block ⇒ Object
Returns a block that will attempt to do the correct thing depending on the plurality of the name passed to
assumeand the action for the current request. -
#initialize(name = nil, context = {}, request = nil) ⇒ Rails
constructor
:nodoc:.
-
#result ⇒ Object
:nodoc:.
Constructor Details
#initialize(name = nil, context = {}, request = nil) ⇒ Rails
:nodoc:
12 13 14 15 16 17 18 |
# File 'lib/basic_assumption/default_assumption/rails.rb', line 12 def initialize(name=nil, context={}, request=nil) #:nodoc: @context = context @name = Name.new(context.delete(:as) || name) @request = request @action = initialize_action end |
Instance Attribute Details
#action ⇒ Object (readonly)
:nodoc:
10 11 12 |
# File 'lib/basic_assumption/default_assumption/rails.rb', line 10 def action @action end |
#context ⇒ Object (readonly)
:nodoc:
10 11 12 |
# File 'lib/basic_assumption/default_assumption/rails.rb', line 10 def context @context end |
#name ⇒ Object (readonly)
:nodoc:
10 11 12 |
# File 'lib/basic_assumption/default_assumption/rails.rb', line 10 def name @name end |
#request ⇒ Object (readonly)
:nodoc:
10 11 12 |
# File 'lib/basic_assumption/default_assumption/rails.rb', line 10 def request @request end |
Instance Method Details
#block ⇒ Object
Returns a block that will attempt to do the correct thing depending on the plurality of the name passed to assume and the action for the current request. If the name is singular and the action is not ‘new’ or ‘create’, then assume will find an instance of an ActiveRecord model of the name that it received and an id value in the parameters. If the action is ‘new’ or ‘create’, assume will instantiate a new instance of the model class, passing in the values it finds in the params hash with for a key of the name passed to assume. For example:
class WidgetController < ApplicationController
default_assumption :rails
assume :widget
def create
.save! # widget is: Widget.new(params[:widget])
end
end
Note the object will have been instantiated but not saved, destroyed, etc.
If the name passed to assume is plural, assume returns all records for the model.
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.
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
conf.active_record.find_on_id = 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.
100 101 102 103 104 105 106 107 |
# File 'lib/basic_assumption/default_assumption/rails.rb', line 100 def block default = self Proc.new do |name, context| context[:controller] = self default.class.new(name, context, request).result end end |
#result ⇒ Object
:nodoc:
109 110 111 |
# File 'lib/basic_assumption/default_assumption/rails.rb', line 109 def result #:nodoc: action.outcome end |