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.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/rails_generator/generators/components/scaffold/scaffold_generator.rb', line 55

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.



44
45
46
# File 'lib/rails_generator/generators/components/scaffold/scaffold_generator.rb', line 44

def controller_class_name
  @controller_class_name
end

#controller_class_nestingObject (readonly)

Returns the value of attribute controller_class_nesting.



44
45
46
# File 'lib/rails_generator/generators/components/scaffold/scaffold_generator.rb', line 44

def controller_class_nesting
  @controller_class_nesting
end

#controller_class_nesting_depthObject (readonly)

Returns the value of attribute controller_class_nesting_depth.



44
45
46
# File 'lib/rails_generator/generators/components/scaffold/scaffold_generator.rb', line 44

def controller_class_nesting_depth
  @controller_class_nesting_depth
end

#controller_class_pathObject (readonly)

Returns the value of attribute controller_class_path.



44
45
46
# File 'lib/rails_generator/generators/components/scaffold/scaffold_generator.rb', line 44

def controller_class_path
  @controller_class_path
end

#controller_file_pathObject (readonly)

Returns the value of attribute controller_file_path.



44
45
46
# File 'lib/rails_generator/generators/components/scaffold/scaffold_generator.rb', line 44

def controller_file_path
  @controller_file_path
end

#controller_nameObject (readonly)

Returns the value of attribute controller_name.



44
45
46
# File 'lib/rails_generator/generators/components/scaffold/scaffold_generator.rb', line 44

def controller_name
  @controller_name
end

#controller_plural_nameObject (readonly) Also known as: controller_table_name

Returns the value of attribute controller_plural_name.



44
45
46
# File 'lib/rails_generator/generators/components/scaffold/scaffold_generator.rb', line 44

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.



44
45
46
# File 'lib/rails_generator/generators/components/scaffold/scaffold_generator.rb', line 44

def controller_singular_name
  @controller_singular_name
end

Instance Method Details

#manifestObject



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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/rails_generator/generators/components/scaffold/scaffold_generator.rb', line 72

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
      
     
     #copying the partial for ajax search
     m.template "partial_list_elements.rhtml",
                 File.join('app/views',
                           controller_class_path,
                           controller_file_name,
                           "_list_elements.rhtml")      
                            
    # 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")

    # Uncomment to create a layout. This kind of scaffold use ever the same layout for
    # all views
   
   # m.template 'layout.rhtml',
    #            File.join('app/views/layouts',
     #                     controller_class_path,
      #                    "#{controller_file_name}.rhtml")

   # Uncomment to create a style sheet when scaffolding
   
   # 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