mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Added --model-name option scaffold_controller_generator.
This commit is contained in:
parent
44ef5a83c8
commit
4f642dcb39
5 changed files with 53 additions and 6 deletions
|
@ -1,3 +1,7 @@
|
|||
* Added `--model-name` scaffld\_controller\_generator option.
|
||||
|
||||
*yalab*
|
||||
|
||||
* Expose MiddlewareStack#unshift to environment configuration.
|
||||
|
||||
*Ben Pickles*
|
||||
|
|
|
@ -13,7 +13,7 @@ module Rails
|
|||
argument :attributes, type: :array, default: [], banner: "field:type field:type"
|
||||
|
||||
def create_controller_files
|
||||
template "controller.rb", File.join('app/controllers', class_path, "#{controller_file_name}_controller.rb")
|
||||
template "controller.rb", File.join('app/controllers', controller_class_path, "#{controller_file_name}_controller.rb")
|
||||
end
|
||||
|
||||
hook_for :template_engine, :test_framework, as: :scaffold
|
||||
|
|
|
@ -9,11 +9,19 @@ module Rails
|
|||
|
||||
def self.included(base) #:nodoc:
|
||||
base.class_option :force_plural, type: :boolean, desc: "Forces the use of a plural ModelName"
|
||||
base.class_option :model_name, type: :string, desc: "ModelName to be used"
|
||||
end
|
||||
|
||||
# Set controller variables on initialization.
|
||||
def initialize(*args) #:nodoc:
|
||||
super
|
||||
if options[:model_name]
|
||||
controller_name = name
|
||||
self.name = options[:model_name]
|
||||
assign_names!(self.name)
|
||||
else
|
||||
controller_name = name
|
||||
end
|
||||
|
||||
if name == name.pluralize && name.singularize != name.pluralize && !options[:force_plural]
|
||||
unless ResourceHelpers.skip_warn
|
||||
|
@ -24,19 +32,26 @@ module Rails
|
|||
assign_names!(name)
|
||||
end
|
||||
|
||||
@controller_name = name.pluralize
|
||||
assign_controller_names!(controller_name.pluralize)
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
attr_reader :controller_name
|
||||
attr_reader :controller_name, :controller_file_name
|
||||
|
||||
def controller_class_path
|
||||
class_path
|
||||
if options[:model_name]
|
||||
@controller_class_path
|
||||
else
|
||||
class_path
|
||||
end
|
||||
end
|
||||
|
||||
def controller_file_name
|
||||
@controller_file_name ||= file_name.pluralize
|
||||
def assign_controller_names!(name)
|
||||
@controller_name = name
|
||||
@controller_class_path = name.include?('/') ? name.split('/') : name.split('::')
|
||||
@controller_class_path.map! { |m| m.underscore }
|
||||
@controller_file_name = @controller_class_path.pop
|
||||
end
|
||||
|
||||
def controller_file_path
|
||||
|
|
|
@ -117,6 +117,25 @@ class NamedBaseTest < Rails::Generators::TestCase
|
|||
assert Rails::Generators.hidden_namespaces.include?('hidden')
|
||||
end
|
||||
|
||||
def test_scaffold_plural_names_with_model_name_option
|
||||
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
|
||||
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
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def assert_name(generator, value, method)
|
||||
|
|
|
@ -166,4 +166,13 @@ class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase
|
|||
assert_match(/render action: 'new'/, content)
|
||||
end
|
||||
end
|
||||
|
||||
def test_model_name_option
|
||||
run_generator ["Admin::User", "--model-name=User"]
|
||||
assert_file "app/controllers/admin/users_controller.rb" do |content|
|
||||
assert_instance_method :index, content do |m|
|
||||
assert_match("@users = User.all", m)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue