0
module ThoughtBot # :nodoc:
0
module Shoulda # :nodoc:
0
+ # Formats tested by #should_be_restful. Defaults to [:html, :xml]
0
VALID_FORMATS = Dir.glob(File.join(File.dirname(__FILE__), 'formats', '*.rb')).map { |f| File.basename(f, '.rb') }.map(&:to_sym) # :doc:
0
VALID_FORMATS.each {|f| require "shoulda/controller/formats/#{f}"}
0
+ # Actions tested by #should_be_restful
0
VALID_ACTIONS = [:index, :show, :new, :edit, :create, :update, :destroy] # :doc:
0
+ # A ResourceOptions object is passed into should_be_restful in order to configure the tests for your controller.
0
+ # class UsersControllerTest < Test::Unit::TestCase
0
+ # ...normal setup code...
0
+ # @user = User.find(:first)
0
+ # should_be_restful do |resource|
0
+ # resource.identifier = :id
0
+ # resource.klass = User
0
+ # resource.object = :user
0
+ # resource.parent = []
0
+ # resource.actions = [:index, :show, :new, :edit, :update, :create, :destroy]
0
+ # resource.formats = [:html, :xml]
0
+ # resource.create.params = { :name => "bob", :email => 'bob@bob.com', :age => 13}
0
+ # resource.update.params = { :name => "sue" }
0
+ # resource.create.redirect = "user_url(@user)"
0
+ # resource.update.redirect = "user_url(@user)"
0
+ # resource.destroy.redirect = "users_url"
0
+ # resource.create.flash = /created/i
0
+ # resource.update.flash = /updated/i
0
+ # resource.destroy.flash = /removed/i
0
+ # Whenever possible, the resource attributes will be set to sensible defaults.
0
+ # Configuration options for the create, update, destroy actions under should_be_restful
0
+ # String evaled to get the target of the redirection.
0
+ # All of the instance variables set by the controller will be available to the
0
+ # resource.create.redirect = "user_url(@user.company, @user)"
0
+ # Defaults to a generated url based on the name of the controller, the action, and the resource.parents list.
0
+ attr_accessor :redirect
0
+ # String or Regexp describing a value expected in the flash. Will match against any flash key.
0
+ # Hash describing the params that should be sent in with this action.
0
+ # Configuration options for the denied actions under should_be_restful
0
+ # context "The public" do
0
+ # @request.session[:logged_in] = false
0
+ # should_be_restful do |resource|
0
+ # resource.parent = :user
0
+ # resource.denied.actions = [:index, :show, :edit, :new, :create, :update, :destroy]
0
+ # resource.denied.flash = /get outta here/i
0
+ # resource.denied.redirect = 'new_session_url'
0
+ # String evaled to get the target of the redirection.
0
+ # All of the instance variables set by the controller will be available to the
0
+ # resource.create.redirect = "user_url(@user.company, @user)"
0
+ attr_accessor :redirect
0
+ # String or Regexp describing a value expected in the flash. Will match against any flash key.
0
+ # resource.create.flash = /created/
0
+ # Actions that should be denied (only used by resource.denied). <i>Note that these actions will
0
+ # only be tested if they are also listed in +resource.actions+</i>
0
+ # The special value of :all will deny all of the REST actions.
0
+ attr_accessor :actions
0
+ # Name of key in params that references the primary key.
0
+ # Will almost always be :id (default), unless you are using a plugin or have patched rails.
0
+ attr_accessor :identifier
0
+ # Name of the ActiveRecord class this resource is responsible for. Automatically determined from
0
+ # test class if not explicitly set. UserTest => "User"
0
+ # Name of the instantiated ActiveRecord object that should be used by some of the tests.
0
+ # Defaults to the underscored name of the AR class. CompanyManager => :company_manager
0
+ # Name of the parent AR objects. Can be set as parent= or parents=, and can take either
0
+ # the name of the parent resource (if there's only one), or an array of names (if there's
0
+ # map.resources :companies do
0
+ # map.resources :people do
0
+ # map.resources :limbs
0
+ # class PeopleControllerTest < Test::Unit::TestCase
0
+ # should_be_restful do |resource|
0
+ # resource.parent = :companies
0
+ # class LimbsControllerTest < Test::Unit::TestCase
0
+ # should_be_restful do |resource|
0
+ # resource.parents = [:companies, :people]
0
+ alias parents= parent=
0
+ # Actions that should be tested. Must be a subset of VALID_ACTIONS (default).
0
+ # Tests for each actionw will only be generated if the action is listed here.
0
+ # The special value of :all will test all of the REST actions.
0
+ # Example (for a read-only controller):
0
+ # resource.actions = [:show, :index]
0
+ attr_accessor :actions
0
+ # Formats that should be tested. Must be a subset of VALID_FORMATS (default).
0
+ # Each action will be tested against the formats listed here. The special value
0
+ # of :all will test all of the supported formats.
0
+ # resource.actions = [:html, :xml]
0
+ attr_accessor :formats
0
+ # ActionOptions object specifying options for the create action.
0
+ # ActionOptions object specifying options for the update action.
0
+ # ActionOptions object specifying options for the desrtoy action.
0
+ attr_accessor :destroy
0
+ # DeniedOptions object specifying which actions should return deny a request, and what should happen in that case.
0
+ def initialize # :nodoc:
0
+ @create = ActionOptions.new
0
+ @update = ActionOptions.new
0
+ @destroy = ActionOptions.new
0
+ @denied = DeniedOptions.new
0
+ @create.flash ||= /created/i
0
+ @update.flash ||= /updated/i
0
+ @destroy.flash ||= /removed/i
0
+ @denied.flash ||= /denied/i
0
+ @actions = VALID_ACTIONS
0
+ @formats = VALID_FORMATS
0
+ def normalize!(target) # :nodoc:
0
+ @denied.actions = VALID_ACTIONS if @denied.actions == :all
0
+ @actions = VALID_ACTIONS if @actions == :all
0
+ @formats = VALID_FORMATS if @formats == :all
0
+ @denied.actions = @denied.actions.map(&:to_sym)
0
+ @actions = @actions.map(&:to_sym)
0
+ @formats = @formats.map(&:to_sym)
0
+ ensure_valid_members(@actions, VALID_ACTIONS, 'actions')
0
+ ensure_valid_members(@denied.actions, VALID_ACTIONS, 'denied.actions')
0
+ ensure_valid_members(@formats, VALID_FORMATS, 'formats')
0
+ @klass ||= target.name.gsub(/ControllerTest$/, '').singularize.constantize
0
+ @object ||= @klass.name.tableize.singularize
0
+ @parent = [@parent] unless @parent.is_a? Array
0
+ collection_helper = [@parent, @object.to_s.pluralize, 'url'].flatten.join('_')
0
+ collection_args = @parent.map {|n| "@#{object}.#{n}"}.join(', ')
0
+ @destroy.redirect ||= "#{collection_helper}(#{collection_args})"
0
+ member_helper = [@parent, @object, 'url'].flatten.join('_')
0
+ member_args = [@parent.map {|n| "@#{object}.#{n}"}, "@#{object}"].flatten.join(', ')
0
+ @create.redirect ||= "#{member_helper}(#{member_args})"
0
+ @update.redirect ||= "#{member_helper}(#{member_args})"
0
+ @denied.redirect ||= "new_session_url"
0
+ def ensure_valid_members(ary, valid_members, name) # :nodoc:
0
+ invalid = ary - valid_members
0
+ raise ArgumentError, "Unsupported #{name}: #{invalid.inspect}" unless invalid.empty?
Comments
No one has commented yet.