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

Attributes included from Rails::Generator::Options

#options

Instance Method Summary collapse

Methods inherited from Rails::Generator::Base

#destination_path, #source_path

Methods included from Rails::Generator::Options

included

Constructor Details

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

Returns a new instance of ScaffoldGenerator.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rails_generator/generators/components/scaffold/scaffold_generator.rb', line 16

def initialize(runtime_args, runtime_options = {})
  super

  @controller_name = @name.pluralize

  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_underscore_name, @controller_plural_name = inflect_names(base_name)
  @controller_singular_name=base_name.singularize
  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.



4
5
6
# File 'lib/rails_generator/generators/components/scaffold/scaffold_generator.rb', line 4

def controller_class_name
  @controller_class_name
end

#controller_class_nestingObject (readonly)

Returns the value of attribute controller_class_nesting.



4
5
6
# File 'lib/rails_generator/generators/components/scaffold/scaffold_generator.rb', line 4

def controller_class_nesting
  @controller_class_nesting
end

#controller_class_nesting_depthObject (readonly)

Returns the value of attribute controller_class_nesting_depth.



4
5
6
# File 'lib/rails_generator/generators/components/scaffold/scaffold_generator.rb', line 4

def controller_class_nesting_depth
  @controller_class_nesting_depth
end

#controller_class_pathObject (readonly)

Returns the value of attribute controller_class_path.



4
5
6
# File 'lib/rails_generator/generators/components/scaffold/scaffold_generator.rb', line 4

def controller_class_path
  @controller_class_path
end

#controller_file_pathObject (readonly)

Returns the value of attribute controller_file_path.



4
5
6
# File 'lib/rails_generator/generators/components/scaffold/scaffold_generator.rb', line 4

def controller_file_path
  @controller_file_path
end

#controller_nameObject (readonly)

Returns the value of attribute controller_name.



4
5
6
# File 'lib/rails_generator/generators/components/scaffold/scaffold_generator.rb', line 4

def controller_name
  @controller_name
end

#controller_plural_nameObject (readonly) Also known as: controller_table_name

Returns the value of attribute controller_plural_name.



4
5
6
# File 'lib/rails_generator/generators/components/scaffold/scaffold_generator.rb', line 4

def controller_plural_name
  @controller_plural_name
end

#controller_singular_nameObject (readonly)

Returns the value of attribute controller_singular_name.



4
5
6
# File 'lib/rails_generator/generators/components/scaffold/scaffold_generator.rb', line 4

def controller_singular_name
  @controller_singular_name
end

#controller_underscore_nameObject (readonly) Also known as: controller_file_name

Returns the value of attribute controller_underscore_name.



4
5
6
# File 'lib/rails_generator/generators/components/scaffold/scaffold_generator.rb', line 4

def controller_underscore_name
  @controller_underscore_name
end

Instance Method Details

#manifestObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/rails_generator/generators/components/scaffold/scaffold_generator.rb', line 31

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

    # Controller, helper, views, test and stylesheets directories.
    m.directory(File.join('app/models', class_path))
    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))
    m.directory(File.join('test/unit', class_path))
    m.directory(File.join('public/stylesheets', class_path))

    for action in scaffold_views
      m.template(
        "view_#{action}.html.erb",
        File.join('app/views', controller_class_path, controller_file_name, "#{action}.html.erb")
      )
    end

    # Layout and stylesheet.
    m.template('layout.html.erb', File.join('app/views/layouts', controller_class_path, "#{controller_file_name}.html.erb"))
    m.template('style.css', 'public/stylesheets/scaffold.css')

    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"))

    m.route_resources controller_file_name

    m.dependency 'model', [name] + @args, :collision => :skip
  end
end