Skip to content

Commit

Permalink
Issue #18: belongs_to should not cause an assumption that Migrant is …
Browse files Browse the repository at this point in the history
…being used on a model
  • Loading branch information
pascal-za committed Mar 6, 2012
1 parent 1ea1250 commit 4565f02
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/migrant/migration_generator.rb
Expand Up @@ -29,7 +29,7 @@ def run
# Rails 3.2+ caches table (non) existence so this needs to be cleared before we start
ActiveRecord::Base.connection.schema_cache.clear! if ActiveRecord::Base.connection.respond_to?(:schema_cache)

ActiveRecord::Base.descendants.select { |model| model.schema && model.schema.requires_migration? }.each do |model|
ActiveRecord::Base.descendants.select { |model| model.structure_defined? && model.schema.requires_migration? }.each do |model|
model.reset_column_information # db:migrate doesn't do this
@table_name = model.table_name
@columns = Hash[[:changed, :added, :deleted, :renamed, :transferred].collect { |a| [a,[]] }]
Expand Down
6 changes: 6 additions & 0 deletions lib/migrant/model_extensions.rb
Expand Up @@ -21,6 +21,8 @@ def structure(type=nil, &block)
# So, my_field in the structure block actually calls Migrant::Schema.my_field

create_migrant_schema
@structure_defined = true

if self.superclass == ActiveRecord::Base
@schema.define_structure(type, &block)
@schema.validations.each do |field, validation_options|
Expand All @@ -41,6 +43,10 @@ def structure(type=nil, &block)
end
end

def structure_defined?
@schema && @structure_defined || false
end

# Same as defining a structure block, but with no attributes besides
# relationships (such as in a many-to-many)
def no_structure
Expand Down
3 changes: 3 additions & 0 deletions test/rails_app/app/models/non_migrant_model.rb
@@ -0,0 +1,3 @@
class NonMigrantModel < ActiveRecord::Base
belongs_to :business
end
4 changes: 4 additions & 0 deletions test/test_data_schema.rb
Expand Up @@ -99,4 +99,8 @@ class TestDataSchema < Test::Unit::TestCase
end
end

should "still indicate a structure is not defined if a belongs_to association is added" do
assert_equal(false, NonMigrantModel.structure_defined?)
assert_equal(true, Business.structure_defined?)
end
end

0 comments on commit 4565f02

Please sign in to comment.