1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 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 224 225 226 227 228 229 230 231 232 233 234 235 236
|
require 'test_helper'
class Book; end
class Folder; end
class BooksController < InheritedResources::Base
custom_actions collection: :search, resource: [:delete]
actions :index, :show
end
class ReadersController < InheritedResources::Base
actions :all, except: [ :edit, :update ]
end
class FoldersController < InheritedResources::Base
end
class Dean
def self.human_name; 'Dean'; end
end
class DeansController < InheritedResources::Base
belongs_to :school
end
module Controller
class User; end
class UsersController < InheritedResources::Base; end
module Admin
class UsersController < InheritedResources::Base; end
end
end
class ControllerGroup; end
module Controller
class GroupsController < InheritedResources::Base; end
end
module Library
class Base
end
class Category
end
class Subcategory
end
class SubcategoriesController < InheritedResources::Base
end
end
module MyEngine
class Engine < Rails::Engine
isolate_namespace MyEngine
end
class PeopleController < InheritedResources::Base; end
end
module MyNamespace
class PeopleController < InheritedResources::Base; end
end
module EmptyNamespace; end
class ActionsClassMethodTest < ActionController::TestCase
tests BooksController
def setup
draw_routes do
resources :books
end
end
def teardown
clear_routes
end
def test_cannot_render_actions
assert_raise AbstractController::ActionNotFound do
get :new
end
end
def test_actions_are_undefined
action_methods = BooksController.send(:action_methods).map(&:to_sym)
assert_equal 4, action_methods.size
[:index, :show, :delete, :search].each do |action|
assert action_methods.include?(action)
end
instance_methods = BooksController.send(:instance_methods).map(&:to_sym)
[:new, :edit, :create, :update, :destroy].each do |action|
assert !instance_methods.include?(action)
end
end
def test_actions_are_undefined_when_except_option_is_given
action_methods = ReadersController.send(:action_methods)
assert_equal 5, action_methods.size
['index', 'new', 'show', 'create', 'destroy'].each do |action|
assert action_methods.include? action
end
end
end
class DefaultsClassMethodTest < ActiveSupport::TestCase
def test_resource_class_is_set_to_nil_when_resource_model_cannot_be_found
assert_nil ReadersController.send(:resource_class)
end
def test_defaults_are_set
assert_equal Folder, FoldersController.send(:resource_class)
assert_equal :folder, FoldersController.send(:resources_configuration)[:self][:instance_name]
assert_equal :folders, FoldersController.send(:resources_configuration)[:self][:collection_name]
end
def test_defaults_can_be_overwriten
BooksController.send(:defaults, resource_class: String, instance_name: 'string', collection_name: 'strings')
assert_equal String, BooksController.send(:resource_class)
assert_equal :string, BooksController.send(:resources_configuration)[:self][:instance_name]
assert_equal :strings, BooksController.send(:resources_configuration)[:self][:collection_name]
BooksController.send(:defaults, class_name: 'Integer', instance_name: :integer, collection_name: :integers)
assert_equal Integer, BooksController.send(:resource_class)
assert_equal :integer, BooksController.send(:resources_configuration)[:self][:instance_name]
assert_equal :integers, BooksController.send(:resources_configuration)[:self][:collection_name]
end
def test_defaults_raises_invalid_key
assert_raise ArgumentError do
BooksController.send(:defaults, boom: String)
end
end
def test_url_helpers_are_recreated_when_defaults_change
BooksController.expects(:create_resources_url_helpers!).returns(true).once
BooksController.send(:defaults, instance_name: 'string', collection_name: 'strings')
end
end
class BelongsToErrorsTest < ActiveSupport::TestCase
def test_belongs_to_raise_errors_with_invalid_arguments
assert_raise ArgumentError do
DeansController.send(:belongs_to)
end
assert_raise ArgumentError do
DeansController.send(:belongs_to, :nice, invalid_key: '')
end
end
def test_belongs_to_raises_an_error_when_multiple_associations_are_given_with_options
assert_raise ArgumentError do
DeansController.send(:belongs_to, :arguments, :with_options, parent_class: Book)
end
end
def test_url_helpers_are_recreated_just_once_when_belongs_to_is_called_with_block
DeansController.expects(:create_resources_url_helpers!).returns(true).once
DeansController.send(:belongs_to, :school) do
belongs_to :association
end
ensure
DeansController.send(:parents_symbols=, [:school])
end
def test_url_helpers_are_recreated_just_once_when_belongs_to_is_called_with_multiple_blocks
DeansController.expects(:create_resources_url_helpers!).returns(true).once
DeansController.send(:belongs_to, :school) do
belongs_to :association do
belongs_to :nested
end
end
ensure
DeansController.send(:parents_symbols=, [:school])
end
def test_belongs_to_for_namespaced_controller_and_namespaced_model_fetches_model_in_the_namespace_firstly
Library::SubcategoriesController.send(:belongs_to, :category)
assert_equal Library::Category, Library::SubcategoriesController.resources_configuration[:category][:parent_class]
end
def test_belongs_to_for_namespaced_controller_and_non_namespaced_model_sets_parent_class_properly
Library::SubcategoriesController.send(:belongs_to, :book)
assert_equal Book, Library::SubcategoriesController.resources_configuration[:book][:parent_class]
end
def test_belongs_to_for_namespaced_model_sets_parent_class_properly
Library::SubcategoriesController.send(:belongs_to, :library, class_name: 'Library::Base')
assert_equal Library::Base, Library::SubcategoriesController.resources_configuration[:library][:parent_class]
end
def test_belongs_to_without_namespace_sets_parent_class_properly
FoldersController.send(:belongs_to, :book)
assert_equal Book, FoldersController.resources_configuration[:book][:parent_class]
end
end
class SpecialCasesClassMethodTest < ActionController::TestCase
def test_resource_class_to_corresponding_model_class
assert_equal Controller::User, Controller::UsersController.send(:resource_class)
assert_equal Controller::User, Controller::Admin::UsersController.send(:resource_class)
assert_equal ControllerGroup, Controller::GroupsController.send(:resource_class)
end
end
class MountableEngineTest < ActiveSupport::TestCase
def test_route_prefix_do_not_include_engine_name
puts MyEngine::PeopleController.send(:resources_configuration)[:self][:route_prefix]
assert_nil MyEngine::PeopleController.send(:resources_configuration)[:self][:route_prefix]
end
def test_route_prefix_present_when_parent_module_is_not_a_engine
assert_equal :my_namespace, MyNamespace::PeopleController.send(:resources_configuration)[:self][:route_prefix]
end
end
class EngineLoadErrorTest < ActiveSupport::TestCase
def test_does_not_crash_on_engine_load_error
ActiveSupport::Dependencies.autoload_paths << 'test/autoload'
EmptyNamespace.class_eval <<-RUBY
class PeopleController < InheritedResources::Base; end
RUBY
end
end
|