]> projects.mako.cc - selectricity-live/blob - vendor/plugins/gruff/generators/gruff/gruff_generator.rb
337f58ef3f159aac32812de4bf5d79650610399e
[selectricity-live] / vendor / plugins / gruff / generators / gruff / gruff_generator.rb
1 class GruffGenerator < Rails::Generator::NamedBase
2
3   attr_reader   :controller_name,
4                 :controller_class_path,
5                 :controller_file_path,
6                 :controller_class_nesting,
7                 :controller_class_nesting_depth,
8                 :controller_class_name,
9                 :controller_singular_name,
10                 :controller_plural_name,
11                 :parent_folder_for_require
12   alias_method  :controller_file_name,  :controller_singular_name
13   alias_method  :controller_table_name, :controller_plural_name
14
15   def initialize(runtime_args, runtime_options = {})
16     super
17
18     # Take controller name from the next argument.
19     @controller_name = runtime_args.shift
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_singular_name, @controller_plural_name = inflect_names(base_name)
23
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",
35                                                 "#{controller_class_name}ControllerTest"
36
37       # Controller, helper, views, and test directories.
38       m.directory File.join('app/controllers', controller_class_path)
39       m.directory File.join('test/functional', controller_class_path)
40
41       m.template 'controller.rb',
42                   File.join('app/controllers',
43                             controller_class_path,
44                             "#{controller_file_name}_controller.rb")
45
46       # For some reason this doesn't take effect if done in initialize()
47       @parent_folder_for_require = @controller_class_path.join('/').gsub(%r%app/controllers/?%, '')
48       @parent_folder_for_require += @parent_folder_for_require.blank? ? '' : '/'
49
50       m.template 'functional_test.rb',
51                   File.join('test/functional',
52                             controller_class_path,
53                             "#{controller_file_name}_controller_test.rb")
54
55     end
56   end
57
58   protected
59     # Override with your own usage banner.
60     def banner
61       "Usage: #{$0} gruff ControllerName"
62     end
63 end

Benjamin Mako Hill || Want to submit a patch?