1 class ScaffoldGenerator
< Rails
::Generator::NamedBase
2 default_options
:skip_timestamps => false, :skip_migration => false, :force_plural => false
4 attr_reader
:controller_name,
5 :controller_class_path,
7 :controller_class_nesting,
8 :controller_class_nesting_depth,
9 :controller_class_name,
10 :controller_underscore_name,
11 :controller_singular_name,
12 :controller_plural_name
13 alias_method
:controller_file_name, :controller_underscore_name
14 alias_method
:controller_table_name, :controller_plural_name
16 def initialize(runtime_args
, runtime_options
= {})
19 if @name == @name.pluralize
&& !options
[:force_plural]
20 logger
.warning
"Plural version of the model detected, using singularized version. Override with --force-plural."
21 @name = @name.singularize
24 @controller_name = @name.pluralize
26 base_name
, @controller_class_path, @controller_file_path, @controller_class_nesting, @controller_class_nesting_depth = extract_modules(@controller_name)
27 @controller_class_name_without_nesting, @controller_underscore_name, @controller_plural_name = inflect_names(base_name
)
28 @controller_singular_name=base_name
.singularize
29 if @controller_class_nesting.empty
?
30 @controller_class_name = @controller_class_name_without_nesting
32 @controller_class_name = "#{@controller_class_nesting}::#{@controller_class_name_without_nesting}"
38 # Check for class naming collisions.
39 m
.class_collisions("#{controller_class_name}Controller", "#{controller_class_name}Helper")
40 m
.class_collisions(class_name
)
42 # Controller, helper, views, test and stylesheets directories.
43 m
.directory(File
.join('app/models', class_path
))
44 m
.directory(File
.join('app/controllers', controller_class_path
))
45 m
.directory(File
.join('app/helpers', controller_class_path
))
46 m
.directory(File
.join('app/views', controller_class_path
, controller_file_name
))
47 m
.directory(File
.join('app/views/layouts', controller_class_path
))
48 m
.directory(File
.join('test/functional', controller_class_path
))
49 m
.directory(File
.join('test/unit', class_path
))
50 m
.directory(File
.join('public/stylesheets', class_path
))
52 for action
in scaffold_views
54 "view_#{action}.html.erb",
55 File
.join('app/views', controller_class_path
, controller_file_name
, "#{action}.html.erb")
59 # Layout and stylesheet.
60 m
.template('layout.html.erb', File
.join('app/views/layouts', controller_class_path
, "#{controller_file_name}.html.erb"))
61 m
.template('style.css', 'public/stylesheets/scaffold.css')
64 'controller.rb', File
.join('app/controllers', controller_class_path
, "#{controller_file_name}_controller.rb")
67 m
.template('functional_test.rb', File
.join('test/functional', controller_class_path
, "#{controller_file_name}_controller_test.rb"))
68 m
.template('helper.rb', File
.join('app/helpers', controller_class_path
, "#{controller_file_name}_helper.rb"))
70 m
.route_resources controller_file_name
72 m
.dependency
'model', [name
] + @args, :collision => :skip
77 # Override with your own usage banner.
79 "Usage: #{$0} scaffold ModelName [field:type, field:type]"
84 opt
.separator
'Options:'
85 opt
.on("--skip-timestamps",
86 "Don't add timestamps to the migration file for this model") { |v
| options
[:skip_timestamps] = v
}
87 opt
.on("--skip-migration",
88 "Don't generate a migration file for this model") { |v
| options
[:skip_migration] = v
}
89 opt
.on("--force-plural",
90 "Forces the generation of a plural ModelName") { |v
| options
[:force_plural] = v
}
94 %w
[ index show new edit
]