Class: BasicAssumption::DefaultAssumption::Rails

Inherits:
Object
  • Object
show all
Defined in:
lib/basic_assumption/default_assumption/rails.rb

Overview

Restful default behavior in the context of Rails

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#actionObject (readonly)

:nodoc:



10
11
12
# File 'lib/basic_assumption/default_assumption/rails.rb', line 10

def action
  @action
end

#contextObject (readonly)

:nodoc:



10
11
12
# File 'lib/basic_assumption/default_assumption/rails.rb', line 10

def context
  @context
end

#nameObject (readonly)

:nodoc:



10
11
12
# File 'lib/basic_assumption/default_assumption/rails.rb', line 10

def name
  @name
end

#requestObject (readonly)

:nodoc:



10
11
12
# File 'lib/basic_assumption/default_assumption/rails.rb', line 10

def request
  @request
end

Instance Method Details

#blockObject

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
    widget.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

#resultObject

:nodoc:



109
110
111
# File 'lib/basic_assumption/default_assumption/rails.rb', line 109

def result #:nodoc:
  action.outcome
end