Class: Ingoweiss::ScaffoldGenerator

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ScopeHelpers

#instance_variable_scope, #scope, #scope_prefix, #scoped?, #scoped_controller_class_name, #scoped_controller_plural_name, #singleton?, #unscoped?

Class Method Details

.source_rootObject



18
19
20
# File 'lib/generators/ingoweiss/scaffold/scaffold_generator.rb', line 18

def self.source_root
  @source_root ||= File.expand_path('../../templates', __FILE__)
end

Instance Method Details

#add_routesObject



22
23
24
25
# File 'lib/generators/ingoweiss/scaffold/scaffold_generator.rb', line 22

def add_routes
  return if options[:skip_route]
  route "resource#{'s' unless options[:singleton]} :#{options[:singleton] ? singular_name : plural_name}"
end

#generate_modelObject



27
28
29
30
31
# File 'lib/generators/ingoweiss/scaffold/scaffold_generator.rb', line 27

def generate_model
  arguments = [singular_name] + attributes.collect{ |a| [a.name, a.type].join(':') }
  arguments << "#{scope.last.singularize}_id:integer" if scope.any?
  invoke :model, arguments
end

#inject_associationsObject



33
34
35
36
37
38
39
40
41
42
# File 'lib/generators/ingoweiss/scaffold/scaffold_generator.rb', line 33

def inject_associations
  return if scope.empty?
  parent = scope.last.singularize
  inject_into_file("app/models/#{parent}.rb", :after => /< ActiveRecord::Base\n/) do
    options[:singleton] ? "  has_one :#{singular_name}\n" : "  has_many :#{plural_name}\n"
  end
  inject_into_file("app/models/#{singular_name}.rb", :after => /< ActiveRecord::Base\n/) do
    "  belongs_to :#{parent}\n"
  end
end