Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

root/trunk/railties/lib/rails_generator/generators/components/scaffold/scaffold_generator.rb

Revision 8558, 3.6 kB (checked in by bitsweat, 1 year ago)

Move model dependency to the end of the scaffold generator manifest. Closes #10696 [drnic]

Line 
1 class ScaffoldGenerator < Rails::Generator::NamedBase
2   default_options :skip_timestamps => false, :skip_migration => false
3
4   attr_reader   :controller_name,
5                 :controller_class_path,
6                 :controller_file_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
15
16   def initialize(runtime_args, runtime_options = {})
17     super
18
19     @controller_name = @name.pluralize
20
21     base_name, @controller_class_path, @controller_file_path, @controller_class_nesting, @controller_class_nesting_depth = extract_modules(@controller_name)
22     @controller_class_name_without_nesting, @controller_underscore_name, @controller_plural_name = inflect_names(base_name)
23     @controller_singular_name=base_name.singularize
24     if @controller_class_nesting.empty?
25       @controller_class_name = @controller_class_name_without_nesting
26     else
27       @controller_class_name = "#{@controller_class_nesting}::#{@controller_class_name_without_nesting}"
28     end
29   end
30
31   def manifest
32     record do |m|
33       # Check for class naming collisions.
34       m.class_collisions(controller_class_path, "#{controller_class_name}Controller", "#{controller_class_name}Helper")
35       m.class_collisions(class_path, "#{class_name}")
36
37       # Controller, helper, views, and test directories.
38       m.directory(File.join('app/models', class_path))
39       m.directory(File.join('app/controllers', controller_class_path))
40       m.directory(File.join('app/helpers', controller_class_path))
41       m.directory(File.join('app/views', controller_class_path, controller_file_name))
42       m.directory(File.join('app/views/layouts', controller_class_path))
43       m.directory(File.join('test/functional', controller_class_path))
44       m.directory(File.join('test/unit', class_path))
45
46       for action in scaffold_views
47         m.template(
48           "view_#{action}.html.erb",
49           File.join('app/views', controller_class_path, controller_file_name, "#{action}.html.erb")
50         )
51       end
52
53       # Layout and stylesheet.
54       m.template('layout.html.erb', File.join('app/views/layouts', controller_class_path, "#{controller_file_name}.html.erb"))
55       m.template('style.css', 'public/stylesheets/scaffold.css')
56
57       m.template(
58         'controller.rb', File.join('app/controllers', controller_class_path, "#{controller_file_name}_controller.rb")
59       )
60
61       m.template('functional_test.rb', File.join('test/functional', controller_class_path, "#{controller_file_name}_controller_test.rb"))
62       m.template('helper.rb',          File.join('app/helpers',     controller_class_path, "#{controller_file_name}_helper.rb"))
63
64       m.route_resources controller_file_name
65
66       m.dependency 'model', [name] + @args, :collision => :skip
67     end
68   end
69
70   protected
71     # Override with your own usage banner.
72     def banner
73       "Usage: #{$0} scaffold ModelName [field:type, field:type]"
74     end
75
76     def add_options!(opt)
77       opt.separator ''
78       opt.separator 'Options:'
79       opt.on("--skip-timestamps",
80              "Don't add timestamps to the migration file for this model") { |v| options[:skip_timestamps] = v }
81       opt.on("--skip-migration",
82              "Don't generate a migration file for this model") { |v| options[:skip_migration] = v }
83     end
84
85     def scaffold_views
86       %w[ index show new edit ]
87     end
88
89     def model_name
90       class_name.demodulize
91     end
92 end
Note: See TracBrowser for help on using the browser.