Class: ScaffoldGenerator

Inherits:
Rails::Generator::NamedBase show all
Defined in:
lib/rails_generator/generators/components/scaffold/scaffold_generator.rb

Instance Attribute Summary collapse

Attributes inherited from Rails::Generator::NamedBase

#class_name, #class_nesting, #class_nesting_depth, #class_path, #file_path, #name, #plural_name, #singular_name, #table_name

Attributes inherited from Rails::Generator::Base

#args, #destination_root, #source_root

Instance Method Summary collapse

Methods inherited from Rails::Generator::Base

#destination_path, #source_path

Constructor Details

#initialize(runtime_args, runtime_options = {}) ⇒ ScaffoldGenerator

Returns a new instance of ScaffoldGenerator.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/rails_generator/generators/components/scaffold/scaffold_generator.rb', line 51

def initialize(runtime_args, runtime_options = {})
  super

  # Take controller name from the next argument.  Default to the pluralized model name.
  @controller_name = args.shift
  @controller_name ||= ActiveRecord::Base.pluralize_table_names ? @name.pluralize : @name

  base_name, @controller_class_path, @controller_file_path, @controller_class_nesting, @controller_class_nesting_depth = extract_modules(@controller_name)
  @controller_class_name_without_nesting, @controller_singular_name, @controller_plural_name = inflect_names(base_name)

  if @controller_class_nesting.empty?
    @controller_class_name = @controller_class_name_without_nesting
  else
    @controller_class_name = "#{@controller_class_nesting}::#{@controller_class_name_without_nesting}"
  end
end

Instance Attribute Details

#controller_class_nameObject (readonly)

Returns the value of attribute controller_class_name.



40
41
42
# File 'lib/rails_generator/generators/components/scaffold/scaffold_generator.rb', line 40

def controller_class_name
  @controller_class_name
end

#controller_class_nestingObject (readonly)

Returns the value of attribute controller_class_nesting.



40
41
42
# File 'lib/rails_generator/generators/components/scaffold/scaffold_generator.rb', line 40

def controller_class_nesting
  @controller_class_nesting
end

#controller_class_nesting_depthObject (readonly)

Returns the value of attribute controller_class_nesting_depth.



40
41
42
# File 'lib/rails_generator/generators/components/scaffold/scaffold_generator.rb', line 40

def controller_class_nesting_depth
  @controller_class_nesting_depth
end

#controller_class_pathObject (readonly)

Returns the value of attribute controller_class_path.



40
41
42
# File 'lib/rails_generator/generators/components/scaffold/scaffold_generator.rb', line 40

def controller_class_path
  @controller_class_path
end

#controller_file_pathObject (readonly)

Returns the value of attribute controller_file_path.



40
41
42
# File 'lib/rails_generator/generators/components/scaffold/scaffold_generator.rb', line 40

def controller_file_path
  @controller_file_path
end

#controller_nameObject (readonly)

Returns the value of attribute controller_name.



40
41
42
# File 'lib/rails_generator/generators/components/scaffold/scaffold_generator.rb', line 40

def controller_name
  @controller_name
end

#controller_plural_nameObject (readonly) Also known as: controller_table_name

Returns the value of attribute controller_plural_name.



40
41
42
# File 'lib/rails_generator/generators/components/scaffold/scaffold_generator.rb', line 40

def controller_plural_name
  @controller_plural_name
end

#controller_singular_nameObject (readonly) Also known as: controller_file_name

Returns the value of attribute controller_singular_name.



40
41
42
# File 'lib/rails_generator/generators/components/scaffold/scaffold_generator.rb', line 40

def controller_singular_name
  @controller_singular_name
end

Instance Method Details

#manifestObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/rails_generator/generators/components/scaffold/scaffold_generator.rb', line 68

def manifest
  record do |m|
    # Check for class naming collisions.
    m.class_collisions controller_class_path, "#{controller_class_name}Controller", "#{controller_class_name}ControllerTest", "#{controller_class_name}Helper"

    # Controller, helper, views, and test directories.
    m.directory File.join('app/controllers', controller_class_path)
    m.directory File.join('app/helpers', controller_class_path)
    m.directory File.join('app/views', controller_class_path, controller_file_name)
    m.directory File.join('app/views/layouts', controller_class_path)
    m.directory File.join('test/functional', controller_class_path)

    # Depend on model generator but skip if the model exists.
    m.dependency 'model', [singular_name], :collision => :skip, :skip_migration => true

    # Scaffolded forms.
    m.complex_template "form.rhtml",
      File.join('app/views',
                controller_class_path,
                controller_file_name,
                "_form.rhtml"),
      :insert => 'form_scaffolding.rhtml',
      :sandbox => lambda { create_sandbox },
      :begin_mark => 'form',
      :end_mark => 'eoform',
      :mark_id => singular_name


    # Scaffolded views.
    scaffold_views.each do |action|
      m.template "view_#{action}.rhtml",
                 File.join('app/views',
                           controller_class_path,
                           controller_file_name,
                           "#{action}.rhtml"),
                 :assigns => { :action => action }
    end

    # Controller class, functional test, helper, and views.
    m.template 'controller.rb',
                File.join('app/controllers',
                          controller_class_path,
                          "#{controller_file_name}_controller.rb")

    m.template 'functional_test.rb',
                File.join('test/functional',
                          controller_class_path,
                          "#{controller_file_name}_controller_test.rb")

    m.template 'helper.rb',
                File.join('app/helpers',
                          controller_class_path,
                          "#{controller_file_name}_helper.rb")

    # Layout and stylesheet.
    m.template 'layout.rhtml',
                File.join('app/views/layouts',
                          controller_class_path,
                          "#{controller_file_name}.rhtml")

    m.template 'style.css',     'public/stylesheets/scaffold.css'


    # Unscaffolded views.
    unscaffolded_actions.each do |action|
      path = File.join('app/views',
                        controller_class_path,
                        controller_file_name,
                        "#{action}.rhtml")
      m.template "controller:view.rhtml", path,
                 :assigns => { :action => action, :path => path}
    end
  end
end