~michaelforrest/use-case-mapper/trunk

« back to all changes in this revision

Viewing changes to vendor/rails/railties/lib/rails_generator/generators/components/resource/resource_generator.rb

  • Committer: Richard Lee (Canonical)
  • Date: 2010-10-15 15:17:58 UTC
  • mfrom: (190.1.3 use-case-mapper)
  • Revision ID: richard.lee@canonical.com-20101015151758-wcvmfxrexsongf9d
Merge

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
class ResourceGenerator < 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_singular_name,
11
 
                :controller_plural_name
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
 
    @controller_name = @name.pluralize
19
 
 
20
 
    base_name, @controller_class_path, @controller_file_path, @controller_class_nesting, @controller_class_nesting_depth = extract_modules(@controller_name)
21
 
    @controller_class_name_without_nesting, @controller_singular_name, @controller_plural_name = inflect_names(base_name)
22
 
 
23
 
    if @controller_class_nesting.empty?
24
 
      @controller_class_name = @controller_class_name_without_nesting
25
 
    else
26
 
      @controller_class_name = "#{@controller_class_nesting}::#{@controller_class_name_without_nesting}"
27
 
    end
28
 
  end
29
 
 
30
 
  def manifest
31
 
    record do |m|
32
 
      # Check for class naming collisions.
33
 
      m.class_collisions("#{controller_class_name}Controller", "#{controller_class_name}Helper")
34
 
      m.class_collisions(class_name)
35
 
 
36
 
      # Controller, helper, views, and test directories.
37
 
      m.directory(File.join('app/models', class_path))
38
 
      m.directory(File.join('app/controllers', controller_class_path))
39
 
      m.directory(File.join('app/helpers', controller_class_path))
40
 
      m.directory(File.join('app/views', controller_class_path, controller_file_name))
41
 
      m.directory(File.join('test/functional', controller_class_path))
42
 
      m.directory(File.join('test/unit', class_path))
43
 
      m.directory(File.join('test/unit/helpers', class_path))
44
 
 
45
 
      m.dependency 'model', [name] + @args, :collision => :skip
46
 
 
47
 
      m.template(
48
 
        'controller.rb', File.join('app/controllers', controller_class_path, "#{controller_file_name}_controller.rb")
49
 
      )
50
 
 
51
 
      m.template('functional_test.rb', File.join('test/functional', controller_class_path, "#{controller_file_name}_controller_test.rb"))
52
 
      m.template('helper.rb',          File.join('app/helpers',     controller_class_path, "#{controller_file_name}_helper.rb"))
53
 
      m.template('helper_test.rb',     File.join('test/unit/helpers',    controller_class_path, "#{controller_file_name}_helper_test.rb"))
54
 
 
55
 
      m.route_resources controller_file_name
56
 
    end
57
 
  end
58
 
 
59
 
  protected
60
 
    def banner
61
 
      "Usage: #{$0} resource ModelName [field:type, field:type]"
62
 
    end
63
 
 
64
 
    def add_options!(opt)
65
 
      opt.separator ''
66
 
      opt.separator 'Options:'
67
 
      opt.on("--skip-timestamps",
68
 
             "Don't add timestamps to the migration file for this model") { |v| options[:skip_timestamps] = v }
69
 
      opt.on("--skip-migration",
70
 
             "Don't generate a migration file for this model") { |v| options[:skip_migration] = v }
71
 
    end
72
 
 
73
 
    def model_name
74
 
      class_name.demodulize
75
 
    end
76
 
end