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.



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

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



27
28
29
# File 'lib/generators/comfy/scaffold/scaffold_generator.rb', line 27

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

Instance Method Details

#generate_controllerObject



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

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



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

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


58
59
60
61
62
63
64
65
66
# File 'lib/generators/comfy/scaffold/scaffold_generator.rb', line 58

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
    "\n%li= active_link_to '#{class_name.pluralize}', admin_#{file_name.pluralize}_path\n"
  end
end

#generate_routeObject



51
52
53
54
55
56
# File 'lib/generators/comfy/scaffold/scaffold_generator.rb', line 51

def generate_route
  route_string  = "  namespace :admin do\n"
  route_string << "    resources :#{file_name.pluralize}\n"
  route_string << "  end\n"
  route route_string[2..-1]
end

#generate_viewsObject



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

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