Class: ThoughtBot::Shoulda::Controller::ResourceOptions
- Inherits:
-
Object
- Object
- ThoughtBot::Shoulda::Controller::ResourceOptions
- Defined in:
- lib/shoulda/controller/resource_options.rb
Overview
A ResourceOptions object is passed into should_be_restful in order to configure the tests for your controller.
Example:
class UsersControllerTest < Test::Unit::TestCase
fixtures :all
def setup
...normal setup code...
@user = User.find(:first)
end
should_be_restful do |resource|
resource.identifier = :id
resource.klass = User
resource.object = :user
resource.parent = []
resource.actions = [:index, :show, :new, :edit, :update, :create, :destroy]
resource.formats = [:html, :xml]
resource.create.params = { :name => "bob", :email => '[email protected]', :age => 13}
resource.update.params = { :name => "sue" }
resource.create.redirect = "user_url(@user)"
resource.update.redirect = "user_url(@user)"
resource.destroy.redirect = "users_url"
resource.create.flash = /created/i
resource.update.flash = /updated/i
resource.destroy.flash = /removed/i
end
end
Whenever possible, the resource attributes will be set to sensible defaults.
Defined Under Namespace
Classes: ActionOptions, DeniedOptions
Instance Attribute Summary collapse
-
#actions ⇒ Object
Actions that should be tested.
-
#create ⇒ Object
ActionOptions object specifying options for the create action.
-
#denied ⇒ Object
DeniedOptions object specifying which actions should return deny a request, and what should happen in that case.
-
#destroy ⇒ Object
ActionOptions object specifying options for the desrtoy action.
-
#formats ⇒ Object
Formats that should be tested.
-
#identifier ⇒ Object
Name of key in params that references the primary key.
-
#klass ⇒ Object
Name of the ActiveRecord class this resource is responsible for.
-
#object ⇒ Object
Name of the instantiated ActiveRecord object that should be used by some of the tests.
-
#parent ⇒ Object
(also: #parents)
Name of the parent AR objects.
-
#update ⇒ Object
ActionOptions object specifying options for the update action.
Instance Method Summary collapse
-
#initialize ⇒ ResourceOptions
constructor
:nodoc:.
-
#normalize!(target) ⇒ Object
:nodoc:.
Constructor Details
#initialize ⇒ ResourceOptions
:nodoc:
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/shoulda/controller/resource_options.rb', line 176 def initialize # :nodoc: @create = ActionOptions.new @update = ActionOptions.new @destroy = ActionOptions.new @denied = DeniedOptions.new @create.flash ||= /created/i @update.flash ||= /updated/i @destroy.flash ||= /removed/i @denied.flash ||= /denied/i @create.params ||= {} @update.params ||= {} @actions = VALID_ACTIONS @formats = VALID_FORMATS @denied.actions = [] end |
Instance Attribute Details
#actions ⇒ Object
Actions that should be tested. Must be a subset of VALID_ACTIONS (default). Tests for each actionw will only be generated if the action is listed here. The special value of :all will test all of the REST actions.
Example (for a read-only controller):
resource.actions = [:show, :index]
154 155 156 |
# File 'lib/shoulda/controller/resource_options.rb', line 154 def actions @actions end |
#create ⇒ Object
ActionOptions object specifying options for the create action.
165 166 167 |
# File 'lib/shoulda/controller/resource_options.rb', line 165 def create @create end |
#denied ⇒ Object
DeniedOptions object specifying which actions should return deny a request, and what should happen in that case.
174 175 176 |
# File 'lib/shoulda/controller/resource_options.rb', line 174 def denied @denied end |
#destroy ⇒ Object
ActionOptions object specifying options for the desrtoy action.
171 172 173 |
# File 'lib/shoulda/controller/resource_options.rb', line 171 def destroy @destroy end |
#formats ⇒ Object
Formats that should be tested. Must be a subset of VALID_FORMATS (default). Each action will be tested against the formats listed here. The special value of :all will test all of the supported formats.
Example:
resource.actions = [:html, :xml]
162 163 164 |
# File 'lib/shoulda/controller/resource_options.rb', line 162 def formats @formats end |
#identifier ⇒ Object
Name of key in params that references the primary key. Will almost always be :id (default), unless you are using a plugin or have patched rails.
110 111 112 |
# File 'lib/shoulda/controller/resource_options.rb', line 110 def identifier @identifier end |
#klass ⇒ Object
Name of the ActiveRecord class this resource is responsible for. Automatically determined from test class if not explicitly set. UserTest => “User”
114 115 116 |
# File 'lib/shoulda/controller/resource_options.rb', line 114 def klass @klass end |
#object ⇒ Object
Name of the instantiated ActiveRecord object that should be used by some of the tests. Defaults to the underscored name of the AR class. CompanyManager => :company_manager
118 119 120 |
# File 'lib/shoulda/controller/resource_options.rb', line 118 def object @object end |
#parent ⇒ Object Also known as: parents
Name of the parent AR objects. Can be set as parent= or parents=, and can take either the name of the parent resource (if there’s only one), or an array of names (if there’s more than one).
Example:
# in the routes...
map.resources :companies do
map.resources :people do
map.resources :limbs
end
end
# in the tests...
class PeopleControllerTest < Test::Unit::TestCase
should_be_restful do |resource|
resource.parent = :companies
end
end
class LimbsControllerTest < Test::Unit::TestCase
should_be_restful do |resource|
resource.parents = [:companies, :people]
end
end
144 145 146 |
# File 'lib/shoulda/controller/resource_options.rb', line 144 def parent @parent end |
#update ⇒ Object
ActionOptions object specifying options for the update action.
168 169 170 |
# File 'lib/shoulda/controller/resource_options.rb', line 168 def update @update end |
Instance Method Details
#normalize!(target) ⇒ Object
:nodoc:
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/shoulda/controller/resource_options.rb', line 195 def normalize!(target) # :nodoc: @denied.actions = VALID_ACTIONS if @denied.actions == :all @actions = VALID_ACTIONS if @actions == :all @formats = VALID_FORMATS if @formats == :all @denied.actions = @denied.actions.map(&:to_sym) @actions = @actions.map(&:to_sym) @formats = @formats.map(&:to_sym) ensure_valid_members(@actions, VALID_ACTIONS, 'actions') ensure_valid_members(@denied.actions, VALID_ACTIONS, 'denied.actions') ensure_valid_members(@formats, VALID_FORMATS, 'formats') @identifier ||= :id @klass ||= target.name.gsub(/ControllerTest$/, '').singularize.constantize @object ||= @klass.name.tableize.singularize @parent ||= [] @parent = [@parent] unless @parent.is_a? Array collection_helper = [@parent, @object.to_s.pluralize, 'url'].flatten.join('_') collection_args = @parent.map {|n| "@#{object}.#{n}"}.join(', ') @destroy.redirect ||= "#{collection_helper}(#{collection_args})" member_helper = [@parent, @object, 'url'].flatten.join('_') member_args = [@parent.map {|n| "@#{object}.#{n}"}, "@#{object}"].flatten.join(', ') @create.redirect ||= "#{member_helper}(#{member_args})" @update.redirect ||= "#{member_helper}(#{member_args})" @denied.redirect ||= "new_session_url" end |