Froze rails gems
[depot.git] / vendor / rails / railties / lib / rails_generator / generators / components / scaffold / scaffold_generator.rb
1 class ScaffoldGenerator < Rails::Generator::NamedBase
2 default_options :skip_timestamps => false, :skip_migration => false, :force_plural => 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 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
22 end
23
24 @controller_name = @name.pluralize
25
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
31 else
32 @controller_class_name = "#{@controller_class_nesting}::#{@controller_class_name_without_nesting}"
33 end
34 end
35
36 def manifest
37 record do |m|
38 # Check for class naming collisions.
39 m.class_collisions("#{controller_class_name}Controller", "#{controller_class_name}Helper")
40 m.class_collisions(class_name)
41
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))
51
52 for action in scaffold_views
53 m.template(
54 "view_#{action}.html.erb",
55 File.join('app/views', controller_class_path, controller_file_name, "#{action}.html.erb")
56 )
57 end
58
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')
62
63 m.template(
64 'controller.rb', File.join('app/controllers', controller_class_path, "#{controller_file_name}_controller.rb")
65 )
66
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"))
69
70 m.route_resources controller_file_name
71
72 m.dependency 'model', [name] + @args, :collision => :skip
73 end
74 end
75
76 protected
77 # Override with your own usage banner.
78 def banner
79 "Usage: #{$0} scaffold ModelName [field:type, field:type]"
80 end
81
82 def add_options!(opt)
83 opt.separator ''
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 }
91 end
92
93 def scaffold_views
94 %w[ index show new edit ]
95 end
96
97 def model_name
98 class_name.demodulize
99 end
100 end