2017-08-14 13:08:09 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
require "generators/generators_test_helper"
|
|
|
|
require "rails/generators/rails/scaffold_controller/scaffold_controller_generator"
|
2009-07-01 16:19:54 -04:00
|
|
|
|
2010-01-18 18:07:11 -05:00
|
|
|
class NamedBaseTest < Rails::Generators::TestCase
|
|
|
|
include GeneratorsTestHelper
|
2010-01-03 10:34:32 -05:00
|
|
|
tests Rails::Generators::ScaffoldControllerGenerator
|
2009-07-01 16:19:54 -04:00
|
|
|
|
2010-03-16 19:08:59 -04:00
|
|
|
def test_named_generator_with_underscore
|
2016-08-06 13:16:09 -04:00
|
|
|
g = generator ["line_item"]
|
|
|
|
assert_name g, "line_item", :name
|
2010-03-16 19:08:59 -04:00
|
|
|
assert_name g, %w(), :class_path
|
2016-08-06 13:16:09 -04:00
|
|
|
assert_name g, "LineItem", :class_name
|
|
|
|
assert_name g, "line_item", :file_path
|
|
|
|
assert_name g, "line_item", :file_name
|
|
|
|
assert_name g, "Line item", :human_name
|
|
|
|
assert_name g, "line_item", :singular_name
|
|
|
|
assert_name g, "line_items", :plural_name
|
|
|
|
assert_name g, "line_item", :i18n_scope
|
|
|
|
assert_name g, "line_items", :table_name
|
2010-03-16 19:08:59 -04:00
|
|
|
end
|
|
|
|
|
2009-07-01 16:19:54 -04:00
|
|
|
def test_named_generator_attributes
|
2016-08-06 13:16:09 -04:00
|
|
|
g = generator ["admin/foo"]
|
|
|
|
assert_name g, "admin/foo", :name
|
2010-01-25 07:13:29 -05:00
|
|
|
assert_name g, %w(admin), :class_path
|
2016-08-06 13:16:09 -04:00
|
|
|
assert_name g, "Admin::Foo", :class_name
|
|
|
|
assert_name g, "admin/foo", :file_path
|
|
|
|
assert_name g, "foo", :file_name
|
|
|
|
assert_name g, "Foo", :human_name
|
|
|
|
assert_name g, "foo", :singular_name
|
|
|
|
assert_name g, "foos", :plural_name
|
|
|
|
assert_name g, "admin.foo", :i18n_scope
|
|
|
|
assert_name g, "admin_foos", :table_name
|
2017-11-05 00:37:21 -04:00
|
|
|
assert_name g, "admin/foos", :controller_name
|
|
|
|
assert_name g, %w(admin), :controller_class_path
|
|
|
|
assert_name g, "Admin::Foos", :controller_class_name
|
|
|
|
assert_name g, "admin/foos", :controller_file_path
|
|
|
|
assert_name g, "foos", :controller_file_name
|
|
|
|
assert_name g, "admin.foos", :controller_i18n_scope
|
|
|
|
assert_name g, "admin_foo", :singular_route_name
|
|
|
|
assert_name g, "admin_foos", :plural_route_name
|
|
|
|
assert_name g, "@admin_foo", :redirect_resource_name
|
|
|
|
assert_name g, "admin_foo", :model_resource_name
|
|
|
|
assert_name g, "admin_foos", :index_helper
|
2010-01-25 07:13:29 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_named_generator_attributes_as_ruby
|
2016-08-06 13:16:09 -04:00
|
|
|
g = generator ["Admin::Foo"]
|
|
|
|
assert_name g, "Admin::Foo", :name
|
2010-01-25 07:13:29 -05:00
|
|
|
assert_name g, %w(admin), :class_path
|
2016-08-06 13:16:09 -04:00
|
|
|
assert_name g, "Admin::Foo", :class_name
|
|
|
|
assert_name g, "admin/foo", :file_path
|
|
|
|
assert_name g, "foo", :file_name
|
|
|
|
assert_name g, "foo", :singular_name
|
|
|
|
assert_name g, "Foo", :human_name
|
|
|
|
assert_name g, "foos", :plural_name
|
|
|
|
assert_name g, "admin.foo", :i18n_scope
|
|
|
|
assert_name g, "admin_foos", :table_name
|
2017-11-05 00:37:21 -04:00
|
|
|
assert_name g, "Admin::Foos", :controller_name
|
|
|
|
assert_name g, %w(admin), :controller_class_path
|
|
|
|
assert_name g, "Admin::Foos", :controller_class_name
|
|
|
|
assert_name g, "admin/foos", :controller_file_path
|
|
|
|
assert_name g, "foos", :controller_file_name
|
|
|
|
assert_name g, "admin.foos", :controller_i18n_scope
|
|
|
|
assert_name g, "admin_foo", :singular_route_name
|
|
|
|
assert_name g, "admin_foos", :plural_route_name
|
|
|
|
assert_name g, "@admin_foo", :redirect_resource_name
|
|
|
|
assert_name g, "admin_foo", :model_resource_name
|
|
|
|
assert_name g, "admin_foos", :index_helper
|
2009-07-01 16:19:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_named_generator_attributes_without_pluralized
|
2015-02-09 04:41:37 -05:00
|
|
|
original_pluralize_table_names = ActiveRecord::Base.pluralize_table_names
|
2009-07-01 16:19:54 -04:00
|
|
|
ActiveRecord::Base.pluralize_table_names = false
|
2015-02-09 04:41:37 -05:00
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
g = generator ["admin/foo"]
|
|
|
|
assert_name g, "admin_foo", :table_name
|
2010-03-16 19:08:59 -04:00
|
|
|
ensure
|
2015-02-09 04:41:37 -05:00
|
|
|
ActiveRecord::Base.pluralize_table_names = original_pluralize_table_names
|
2009-07-01 16:19:54 -04:00
|
|
|
end
|
|
|
|
|
2017-09-29 19:48:13 -04:00
|
|
|
def test_namespaced_scaffold_plural_names
|
2016-08-06 13:16:09 -04:00
|
|
|
g = generator ["admin/foo"]
|
|
|
|
assert_name g, "admin/foos", :controller_name
|
2010-01-25 07:13:29 -05:00
|
|
|
assert_name g, %w(admin), :controller_class_path
|
2016-08-06 13:16:09 -04:00
|
|
|
assert_name g, "Admin::Foos", :controller_class_name
|
|
|
|
assert_name g, "admin/foos", :controller_file_path
|
|
|
|
assert_name g, "foos", :controller_file_name
|
|
|
|
assert_name g, "admin.foos", :controller_i18n_scope
|
2010-01-25 07:13:29 -05:00
|
|
|
end
|
|
|
|
|
2017-09-29 19:48:13 -04:00
|
|
|
def test_namespaced_scaffold_plural_names_as_ruby
|
2016-08-06 13:16:09 -04:00
|
|
|
g = generator ["Admin::Foo"]
|
|
|
|
assert_name g, "Admin::Foos", :controller_name
|
2010-01-25 07:13:29 -05:00
|
|
|
assert_name g, %w(admin), :controller_class_path
|
2016-08-06 13:16:09 -04:00
|
|
|
assert_name g, "Admin::Foos", :controller_class_name
|
|
|
|
assert_name g, "admin/foos", :controller_file_path
|
|
|
|
assert_name g, "foos", :controller_file_name
|
|
|
|
assert_name g, "admin.foos", :controller_i18n_scope
|
2009-07-01 16:19:54 -04:00
|
|
|
end
|
|
|
|
|
2010-01-25 19:06:48 -05:00
|
|
|
def test_application_name
|
2016-08-06 13:16:09 -04:00
|
|
|
g = generator ["Admin::Foo"]
|
2015-04-28 09:16:43 -04:00
|
|
|
Rails.stub(:application, Object.new) do
|
|
|
|
assert_name g, "object", :application_name
|
|
|
|
end
|
|
|
|
|
|
|
|
Rails.stub(:application, nil) do
|
|
|
|
assert_name g, "application", :application_name
|
|
|
|
end
|
2010-01-25 19:06:48 -05:00
|
|
|
end
|
|
|
|
|
2010-06-19 17:35:54 -04:00
|
|
|
def test_index_helper
|
2016-08-06 13:16:09 -04:00
|
|
|
g = generator ["Post"]
|
|
|
|
assert_name g, "posts", :index_helper
|
2010-06-19 17:35:54 -04:00
|
|
|
end
|
|
|
|
|
2011-02-07 20:26:42 -05:00
|
|
|
def test_index_helper_to_pluralize_once
|
2016-08-06 13:16:09 -04:00
|
|
|
g = generator ["Stadium"]
|
|
|
|
assert_name g, "stadia", :index_helper
|
2011-02-07 20:26:42 -05:00
|
|
|
end
|
|
|
|
|
2010-06-19 17:35:54 -04:00
|
|
|
def test_index_helper_with_uncountable
|
2016-08-06 13:16:09 -04:00
|
|
|
g = generator ["Sheep"]
|
|
|
|
assert_name g, "sheep_index", :index_helper
|
2010-06-19 17:35:54 -04:00
|
|
|
end
|
|
|
|
|
2012-02-03 06:17:59 -05:00
|
|
|
def test_hide_namespace
|
2016-08-06 13:16:09 -04:00
|
|
|
g = generator ["Hidden"]
|
|
|
|
g.class.stub(:namespace, "hidden") do
|
2016-09-16 12:44:05 -04:00
|
|
|
assert_not_includes Rails::Generators.hidden_namespaces, "hidden"
|
2015-04-28 09:16:43 -04:00
|
|
|
g.class.hide!
|
2016-09-16 12:44:05 -04:00
|
|
|
assert_includes Rails::Generators.hidden_namespaces, "hidden"
|
2015-04-28 09:16:43 -04:00
|
|
|
end
|
2012-02-03 06:17:59 -05:00
|
|
|
end
|
|
|
|
|
2013-07-13 01:38:00 -04:00
|
|
|
def test_scaffold_plural_names_with_model_name_option
|
2016-08-06 13:16:09 -04:00
|
|
|
g = generator ["Admin::Foo"], model_name: "User"
|
|
|
|
assert_name g, "user", :singular_name
|
|
|
|
assert_name g, "User", :name
|
|
|
|
assert_name g, "user", :file_path
|
|
|
|
assert_name g, "User", :class_name
|
|
|
|
assert_name g, "user", :file_name
|
|
|
|
assert_name g, "User", :human_name
|
|
|
|
assert_name g, "users", :plural_name
|
|
|
|
assert_name g, "user", :i18n_scope
|
|
|
|
assert_name g, "users", :table_name
|
|
|
|
assert_name g, "Admin::Foos", :controller_name
|
2013-07-13 01:38:00 -04:00
|
|
|
assert_name g, %w(admin), :controller_class_path
|
2016-08-06 13:16:09 -04:00
|
|
|
assert_name g, "Admin::Foos", :controller_class_name
|
|
|
|
assert_name g, "admin/foos", :controller_file_path
|
|
|
|
assert_name g, "foos", :controller_file_name
|
|
|
|
assert_name g, "admin.foos", :controller_i18n_scope
|
2017-09-20 01:04:12 -04:00
|
|
|
assert_name g, "admin_user", :singular_route_name
|
|
|
|
assert_name g, "admin_users", :plural_route_name
|
|
|
|
assert_name g, "[:admin, @user]", :redirect_resource_name
|
|
|
|
assert_name g, "[:admin, user]", :model_resource_name
|
|
|
|
assert_name g, "admin_users", :index_helper
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_scaffold_plural_names
|
|
|
|
g = generator ["User"]
|
|
|
|
assert_name g, "@user", :redirect_resource_name
|
|
|
|
assert_name g, "user", :model_resource_name
|
|
|
|
assert_name g, "user", :singular_route_name
|
|
|
|
assert_name g, "users", :plural_route_name
|
2013-07-13 01:38:00 -04:00
|
|
|
end
|
|
|
|
|
2016-12-23 09:45:39 -05:00
|
|
|
private
|
2010-01-25 07:13:29 -05:00
|
|
|
|
|
|
|
def assert_name(generator, value, method)
|
|
|
|
assert_equal value, generator.send(method)
|
|
|
|
end
|
2009-07-01 16:19:54 -04:00
|
|
|
end
|