Class: Comfy::Generators::ScaffoldGenerator

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ ScaffoldGenerator

Returns a new instance of ScaffoldGenerator.


20
21
22
23
24
25
26
27
# File 'lib/generators/comfy/scaffold/scaffold_generator.rb', line 20

def initialize(*args, &block)
  super
  @model_attrs = []
  model_args.each do |arg|
    next unless arg.include?(":")
    @model_attrs << Rails::Generators::GeneratedAttribute.new(*arg.split(":"))
  end
end

Class Method Details

.next_migration_number(dirname) ⇒ Object


29
30
31
# File 'lib/generators/comfy/scaffold/scaffold_generator.rb', line 29

def self.next_migration_number(dirname)
  ActiveRecord::Generators::Base.next_migration_number(dirname)
end

Instance Method Details

#generate_controllerObject


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

def generate_controller
  template "controller.rb", "app/controllers/admin/#{file_name.pluralize}_controller.rb"
  template "tests/controller.rb", "test/controllers/admin/#{file_name.pluralize}_controller_test.rb"
end

#generate_modelObject


33
34
35
36
37
38
# File 'lib/generators/comfy/scaffold/scaffold_generator.rb', line 33

def generate_model
  migration_template "migration.rb", "db/migrate/create_#{file_name.pluralize}.rb"
  template "model.rb", "app/models/#{file_name}.rb"
  template "tests/model.rb", "test/models/#{file_name}_test.rb"
  template "tests/fixture.yml", "test/fixtures/#{file_name.pluralize}.yml"
end

63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/generators/comfy/scaffold/scaffold_generator.rb', line 63

def generate_navigation_link
  partial_path = "app/views/comfy/admin/cms/partials/_navigation_inner.html.haml"

  unless File.exist?(File.join(destination_root, partial_path))
    create_file partial_path
  end

  append_file partial_path do
    <<~HAML
      %li.nav-item
        = active_link_to '#{class_name.pluralize}', admin_#{file_name.pluralize}_path, class: 'nav-link'
    HAML
  end
end

#generate_routeObject


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

def generate_route
  route_string = <<~TEXT
    namespace :admin do
      resources :#{file_name.pluralize}
    end

  TEXT
  route route_string
end

#generate_viewsObject


45
46
47
48
49
50
51
# File 'lib/generators/comfy/scaffold/scaffold_generator.rb', line 45

def generate_views
  template "views/index.haml", "app/views/admin/#{file_name.pluralize}/index.html.haml"
  template "views/show.haml", "app/views/admin/#{file_name.pluralize}/show.html.haml"
  template "views/new.haml", "app/views/admin/#{file_name.pluralize}/new.html.haml"
  template "views/edit.haml", "app/views/admin/#{file_name.pluralize}/edit.html.haml"
  template "views/_form.haml", "app/views/admin/#{file_name.pluralize}/_form.html.haml"
end