Class: Stager::ScaffoldGenerator

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

Instance Method Summary collapse

Instance Method Details

#create_controllerObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/generators/stager/scaffold/scaffold_generator.rb', line 72

def create_controller
  template 'controller.rb', "app/controllers/#{plural_name}_controller.rb"
  template 'helper.rb', "app/helpers/#{plural_name}_helper.rb"
  
  namespaces = plural_name.split('/')
  resource = namespaces.pop
  if namespaces.present?
    route "namespace(:#{namespaces.first}) { resources :#{resource} }"
  else
    route "resources :#{resource}"
  end
  
  template "spec/controller.rb", "spec/controllers/#{plural_name}_controller_spec.rb"
end

#create_migrationObject



66
67
68
69
70
# File 'lib/generators/stager/scaffold/scaffold_generator.rb', line 66

def create_migration
  unless @skip_model || options[:skip_migration]
    migration_template 'migration.rb', "db/migrate/create_#{model_path.pluralize.gsub('/', '_')}.rb"
  end
end

#create_modelObject



57
58
59
60
61
62
63
64
# File 'lib/generators/stager/scaffold/scaffold_generator.rb', line 57

def create_model
  unless @skip_model
    template 'model.rb', "app/models/#{model_path}.rb"
    
    template "spec/model.rb", "spec/models/#{model_path}_spec.rb"
    template 'spec/fixtures.yml', "spec/fixtures/#{model_path.pluralize}.yml"
  end
end

#create_presenterObject



99
100
101
102
103
# File 'lib/generators/stager/scaffold/scaffold_generator.rb', line 99

def create_presenter
  unless options[:skip_presenter]
    invoke "exhibit:presenter", [scaffold_name, @namespaced_model ? nil : "--no_namespace"]
  end
end

#create_viewsObject



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/generators/stager/scaffold/scaffold_generator.rb', line 87

def create_views
  controller_actions.each do |action|
    if %w[index show new edit].include?(action)
      template "views/#{action}.html.haml", "app/views/#{plural_name}/#{action}.html.haml"
    end
  end
  
  if form_partial?
    template "views/_form.html.haml", "app/views/#{plural_name}/_form.html.haml"
  end
end

#set_optionsObject



18
19
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
55
# File 'lib/generators/stager/scaffold/scaffold_generator.rb', line 18

def set_options
  @controller_actions = []
  @model_attributes = []
  @invert_actions = false
  @skip_model = false
  @namespaced_model = false
  
  model_and_controller_args.each do |arg|
    if arg == '!'
      @invert_actions = true
    elsif 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
  
  if options[:namespaced_model]
    @namespaced_model = true
  end
  
  if @invert_actions || @controller_actions.empty?
    @controller_actions = all_actions - @controller_actions
  end
  
  if @model_attributes.empty?
    @skip_model = true
    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