Class: Lab::Generators::ScaffoldGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Includes:
Rails::Generators::Migration
Defined in:
lib/generators/lab/scaffold/scaffold_generator.rb

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ ScaffoldGenerator

Returns a new instance of ScaffoldGenerator.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/generators/lab/scaffold/scaffold_generator.rb', line 20

def initialize(*args, &block)
  super

  @controller_actions = []
  @model_attributes = []
  @skip_model = options.skip_model?

  args_for_c_m.each do |arg|
    if arg.include?(':')
      @model_attributes << Rails::Generators::GeneratedAttribute.new(*arg.split(':'))
    else
      @controller_actions << arg
      @controller_actions << 'create' if arg == 'new'
      @controller_actions << 'update' if arg == 'edit'
    end
  end

  @controller_actions.uniq!
  @model_attributes.uniq!

  if @controller_actions.empty?
    @controller_actions = all_actions
  end

  if @model_attributes.empty?
    @skip_model = true # skip model if no attributes
    if model_exists?
      model_columns_for_attributes.each do |column|
        @model_attributes << Rails::Generators::GeneratedAttribute.new(column.name.to_s, column.type.to_s)
      end
    else
      @model_attributes << Rails::Generators::GeneratedAttribute.new('name', 'string')
    end
  end
end

Instance Method Details

#create_controllerObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/generators/lab/scaffold/scaffold_generator.rb', line 68

def create_controller
  unless options.skip_controller?
    template 'controller.rb', "app/controllers/#{plural_name}_controller.rb"

    template 'helper.rb', "app/helpers/#{plural_name}_helper.rb"

    controller_actions.each do |action|
      if %w[index show new edit].include?(action) # Actions with templates
        template "views/#{view_language}/#{action}.html.#{view_language}", "app/views/#{plural_name}/#{action}.html.#{view_language}"
      end
    end

    if form_partial?
      template "views/#{view_language}/_form.html.#{view_language}", "app/views/#{plural_name}/_form.html.#{view_language}"
    end

    route "resources #{plural_name.to_sym.inspect}"
  end
end

#create_migrationObject



62
63
64
65
66
# File 'lib/generators/lab/scaffold/scaffold_generator.rb', line 62

def create_migration
  unless @skip_model || options.skip_migration?
    migration_template "migration.rb", "db/migrate/create_#{plural_name}.rb"
  end
end

#create_modelObject



56
57
58
59
60
# File 'lib/generators/lab/scaffold/scaffold_generator.rb', line 56

def create_model
  unless @skip_model
    template "model.rb", "app/models/#{singular_name}.rb"
  end
end