Class: Common::Generators::ScaffoldGenerator

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

Instance Method Summary collapse

Methods inherited from Base

banner, source_root

Constructor Details

#initialize(*args, &block) ⇒ ScaffoldGenerator

complete:scaffold Admin:User login:string email:string time_zone:string



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/generators/common/scaffold/scaffold_generator.rb', line 20

def initialize(*args, &block)
  super
  @controller_actions = []
  args_for_c_m.each do |arg|
    if arg == '!'
      options[:invert] = 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.invert? || @controller_actions.empty?
    @controller_actions = all_actions - @controller_actions
  end
end

Instance Method Details

#create_controllerObject



44
45
46
47
48
49
# File 'lib/generators/common/scaffold/scaffold_generator.rb', line 44

def create_controller
  unless options.skip_controller?
    template 'controller.rb', "app/controllers/#{plural_name_path}_controller.rb"
    template 'helper.rb', "app/helpers/#{plural_name_path}_helper.rb"
  end
end

#create_migrationsObject



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

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

#create_modelObject



38
39
40
41
42
43
# File 'lib/generators/common/scaffold/scaffold_generator.rb', line 38

def create_model
  unless options.skip_model?
    template 'model.rb', "app/models/#{singular_name_path}.rb"
    template "rspec/model.rb", "spec/models/#{singular_name_path}_spec.rb"
  end
end

#create_viewsObject



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/generators/common/scaffold/scaffold_generator.rb', line 50

def create_views
  unless options.skip_controller?
    @controller_actions.each do |action|
      if %w[index show new edit].include?(action) # Actions with templates
        template "views/#{action}.html.haml", "app/views/#{plural_name_path}/#{action}.html.haml"
      end
    end
    template "views/_form.html.haml", "app/views/#{plural_name_path}/_form.html.haml"
    template "views/_collection.html.haml", "app/views/#{plural_name_path}/_#{collection_name}.html.haml"
    route "resources #{plural_name.to_sym.inspect}"
  end
end