1 class GruffGenerator < Rails::Generator::NamedBase
3 attr_reader :controller_name,
4 :controller_class_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
15 def initialize(runtime_args, runtime_options = {})
18 # Take controller name from the next argument.
19 @controller_name = runtime_args.shift
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)
24 if @controller_class_nesting.empty?
25 @controller_class_name = @controller_class_name_without_nesting
27 @controller_class_name = "#{@controller_class_nesting}::#{@controller_class_name_without_nesting}"
33 # Check for class naming collisions.
34 m.class_collisions controller_class_path, "#{controller_class_name}Controller",
35 "#{controller_class_name}ControllerTest"
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)
41 m.template 'controller.rb',
42 File.join('app/controllers',
43 controller_class_path,
44 "#{controller_file_name}_controller.rb")
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? ? '' : '/'
50 m.template 'functional_test.rb',
51 File.join('test/functional',
52 controller_class_path,
53 "#{controller_file_name}_controller_test.rb")
59 # Override with your own usage banner.
61 "Usage: #{$0} gruff ControllerName"