Class: Mdwa::Generators::AssociationGenerator
- Inherits:
-
Rails::Generators::Base
- Object
- Rails::Generators::Base
- Mdwa::Generators::AssociationGenerator
- Includes:
- Rails::Generators::Migration
- Defined in:
- lib/generators/mdwa/association/association_generator.rb
Instance Attribute Summary collapse
-
#attribute ⇒ Object
Returns the value of attribute attribute.
-
#model1 ⇒ Object
Returns the value of attribute model1.
-
#model2 ⇒ Object
Returns the value of attribute model2.
-
#pending_migrations ⇒ Object
Returns the value of attribute pending_migrations.
Class Method Summary collapse
-
.next_migration_number(dirname) ⇒ Object
Implement the required interface for Rails::Generators::Migration.
Instance Method Summary collapse
-
#initialize(*args, &block) ⇒ AssociationGenerator
constructor
A new instance of AssociationGenerator.
- #migrate ⇒ Object
- #model ⇒ Object
- #opposite ⇒ Object
- #run_rake_db_migrate ⇒ Object
Constructor Details
#initialize(*args, &block) ⇒ AssociationGenerator
Returns a new instance of AssociationGenerator.
28 29 30 31 32 33 34 |
# File 'lib/generators/mdwa/association/association_generator.rb', line 28 def initialize(*args, &block) super @association = MDWA::Generators::ModelAssociation.new(model1, model2, relation) end |
Instance Attribute Details
#attribute ⇒ Object
Returns the value of attribute attribute.
16 17 18 |
# File 'lib/generators/mdwa/association/association_generator.rb', line 16 def attribute @attribute end |
#model1 ⇒ Object
Returns the value of attribute model1.
16 17 18 |
# File 'lib/generators/mdwa/association/association_generator.rb', line 16 def model1 @model1 end |
#model2 ⇒ Object
Returns the value of attribute model2.
16 17 18 |
# File 'lib/generators/mdwa/association/association_generator.rb', line 16 def model2 @model2 end |
#pending_migrations ⇒ Object
Returns the value of attribute pending_migrations.
16 17 18 |
# File 'lib/generators/mdwa/association/association_generator.rb', line 16 def pending_migrations @pending_migrations end |
Class Method Details
.next_migration_number(dirname) ⇒ Object
Implement the required interface for Rails::Generators::Migration.
144 145 146 147 148 149 150 |
# File 'lib/generators/mdwa/association/association_generator.rb', line 144 def self.next_migration_number(dirname) #:nodoc: if ActiveRecord::Base. Time.now.utc.strftime("%Y%m%d%H%M%S") else "%.3d" % (current_migration_number(dirname) + 1) end end |
Instance Method Details
#migrate ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/generators/mdwa/association/association_generator.rb', line 86 def migrate return nil if .skip_migrations @pending_migrations = true case @association.relation.to_sym when :belongs_to, :nested_one @table = @association.model1 @field = @association.model2 migration_template 'migrate/one_field.rb', "db/migrate/add_#{@field.singular_name.foreign_key}_to_#{@table.plural_name}.rb" when :has_and_belongs_to_many # only generates if table does not exist yet if Dir.glob("db/migrate/*create_#{many_to_many_table_name}.rb").count.zero? migration_template 'migrate/many_to_many.rb', "db/migrate/create_#{many_to_many_table_name}.rb" else @pending_migrations = false end else @pending_migrations = false end end |
#model ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/generators/mdwa/association/association_generator.rb', line 37 def model return nil if .skip_models if @association.belongs_to? inject_into_class "app/models/#{@association.model1.space}/#{@association.model1.singular_name}.rb", @association.model1.model_class do ret = [] ret << "\n\tbelongs_to :#{@association.model2.singular_name}, :class_name => '#{@association.model2.klass}'" ret << "\tattr_accessible :#{@association.model2.singular_name.foreign_key}" ret.join("\n") end end if @association.has_one? inject_into_class "app/models/#{@association.model1.space}/#{@association.model1.singular_name}.rb", @association.model1.model_class do "\n\thas_one :#{@association.model2.singular_name}, :class_name => '#{@association.model2.klass}'\n" end end if @association.has_many? inject_into_class "app/models/#{@association.model1.space}/#{@association.model1.singular_name}.rb", @association.model1.model_class do "\n\thas_many :#{@association.model2.plural_name}, :class_name => '#{@association.model2.klass}'\n" end end if @association.has_and_belongs_to_many? inject_into_class "app/models/#{@association.model1.space}/#{@association.model1.singular_name}.rb", @association.model1.model_class do "\n\thas_and_belongs_to_many :#{@association.model2.plural_name}, :join_table => :#{many_to_many_table_name}\n" end inject_into_class "app/models/#{@association.model2.space}/#{@association.model2.singular_name}.rb", @association.model2.model_class do "\n\thas_and_belongs_to_many :#{@association.model1.plural_name}, :join_table => :#{many_to_many_table_name}\n" end end if @association.nested_one? inject_into_class "app/models/#{@association.model1.space}/#{@association.model1.singular_name}.rb", @association.model1.model_class do # belongs_to # attr_accessible attributes # attr_nested_attributes "\n\tbelongs_to :#{@association.model2.singular_name}, :class_name => '#{@association.model2.klass}'\n\tattr_accessible :#{@association.model2.singular_name}_attributes, :#{@association.model2.singular_name.foreign_key}\n\taccepts_nested_attributes_for :#{@association.model2.singular_name}, :allow_destroy => true\n" end end if @association.nested_many? inject_into_class "app/models/#{@association.model1.space}/#{@association.model1.singular_name}.rb", @association.model1.model_class do # has_many # attr_accessible attributes # attr_nested_attributes "\n\thas_many :#{@association.model2.plural_name}, :class_name => '#{@association.model2.klass}', :dependent => :destroy\n\tattr_accessible :#{@association.model2.plural_name}_attributes\n\taccepts_nested_attributes_for :#{@association.model2.plural_name}, :allow_destroy => true\n" end end end |
#opposite ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/generators/mdwa/association/association_generator.rb', line 112 def opposite if .with_opposite or .ask # belongs_to if @association.belongs_to? and (.with_opposite or ( .ask and yes?("#{@association.model1.name} belongs to #{@association.model2.singular_name}. Create has_many association?") )) generate "mdwa:association #{@association.model2.raw} has_many #{@association.model1.raw} #{'--force' if .force}" end # has_many if @association.has_many? and (.with_opposite or ( .ask and yes?("#{@association.model1.name} has many #{@association.model2.plural_name}. Create belongs_to association?") )) generate "mdwa:association #{@association.model2.raw} belongs_to #{@association.model1.raw} #{'--force' if .force}" end # has_one if @association.has_one? and (.with_opposite or ( .ask and yes?("#{@association.model1.name} has one #{@association.model2.singular_name}. Create belongs_to association?") )) generate "mdwa:association #{@association.model2.raw} belongs_to #{@association.model1.raw} #{'--force' if .force}" end # nested_many if @association.nested_many? and (.with_opposite or ( .ask and yes?("#{@association.model1.name} has many (nested) with #{@association.model2.plural_name}. Create belongs_to association?") )) generate "mdwa:association #{@association.model2.raw} belongs_to #{@association.model1.raw} #{'--force' if .force}" end # nested_one if @association.nested_one? and (.with_opposite or ( .ask and yes?("#{@association.model1.name} belongs nested to #{@association.model2.singular_name}. Create has_one association?") )) generate "mdwa:association #{@association.model2.raw} has_one #{@association.model1.raw} #{'--force' if .force}" end end end |
#run_rake_db_migrate ⇒ Object
108 109 110 |
# File 'lib/generators/mdwa/association/association_generator.rb', line 108 def run_rake_db_migrate rake('db:migrate') if !.skip_rake_migrate and @pending_migrations and yes? 'Run rake db:migrate?' end |