Class: NestedScaffold::Generators::ActiveRecordGenerator

Inherits:
ActiveRecord::Generators::ModelGenerator
  • Object
show all
Includes:
Base
Defined in:
lib/generators/active_record/active_record_generator.rb

Instance Method Summary collapse

Constructor Details

#initialize(args, *options) ⇒ ActiveRecordGenerator

:nodoc:



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/generators/active_record/active_record_generator.rb', line 14

def initialize(args, *options) #:nodoc:
  super
  #DIRTY HACK remove noise. don't know why but the first attribute type is always suffixed by " #{parent_name}"
  attributes.each do |attribute|
    attribute.type = attribute.type.to_s.split(' ').first if attribute.type.to_s.include?(' ')
  end
  # prefer parent:references to parent_id:integer
  attributes.delete_if {|a| (a.name == nested_parent_id) && (a.type.to_s == 'integer')}
  unless attributes.map {|a| [a.name, a.type.to_s]}.include? [nested_parent_name, 'references']
    #DIRTY HACK add 'references' attribute
    attributes.insert 0, Rails::Generators::GeneratedAttribute.new(nested_parent_name, :references)
  end
end

Instance Method Details

#add_associationObject

add has_many_association to parent model



29
30
31
32
33
# File 'lib/generators/active_record/active_record_generator.rb', line 29

def add_association
  if File.exist?(parent_file = File.join('app/models', "#{nested_parent_name}.rb"))
    inject_into_file parent_file, "  has_many :#{table_name}\n", :after => / < (ActiveRecord::Base|ApplicationRecord)\n/
  end
end