diff options
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/CHANGELOG | 2 | ||||
-rwxr-xr-x | activerecord/lib/active_record/fixtures.rb | 29 | ||||
-rw-r--r-- | activerecord/test/aggregations_test.rb | 2 | ||||
-rw-r--r-- | activerecord/test/fixtures/db_definitions/schema.rb | 10 | ||||
-rw-r--r-- | activerecord/test/fixtures/joke.rb | 3 | ||||
-rw-r--r-- | activerecord/test/fixtures/matey.rb | 2 | ||||
-rw-r--r-- | activerecord/test/fixtures/parrots.yml | 5 | ||||
-rw-r--r-- | activerecord/test/fixtures/ship.rb | 3 | ||||
-rw-r--r-- | activerecord/test/fixtures/ships.yml | 5 | ||||
-rwxr-xr-x | activerecord/test/fixtures_test.rb | 32 | ||||
-rw-r--r-- | activerecord/test/mixin_test.rb | 13 |
11 files changed, 84 insertions, 22 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index 611c3d703e..a8ceea8aed 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Foxy fixtures: allow mixed usage to make migration easier and more attractive. #10004 [lotswholetime] + * Make the record_timestamps class-inheritable so it can be set per model. #10004 [tmacedo] * Allow validates_acceptance_of to use a real attribute instead of only virtual (so you can record that the acceptance occured) #7457 [ambethia] diff --git a/activerecord/lib/active_record/fixtures.rb b/activerecord/lib/active_record/fixtures.rb index fbd614c969..05c0aa0e39 100755 --- a/activerecord/lib/active_record/fixtures.rb +++ b/activerecord/lib/active_record/fixtures.rb @@ -563,10 +563,12 @@ class Fixtures < YAML::Omap each do |label, fixture| row = fixture.to_hash - if model_class && model_class < ActiveRecord::Base && !row[primary_key_name] - # fill in timestamp columns if they aren't specified - timestamp_column_names.each do |name| - row[name] = now unless row.key?(name) + if model_class && model_class < ActiveRecord::Base + # fill in timestamp columns if they aren't specified and the model is set to record_timestamps + if model_class.record_timestamps + timestamp_column_names.each do |name| + row[name] = now unless row.key?(name) + end end # interpolate the fixture label @@ -575,12 +577,17 @@ class Fixtures < YAML::Omap end # generate a primary key if necessary - row[primary_key_name] = Fixtures.identify(label) if has_primary_key? + if has_primary_key_column? && !row.include?(primary_key_name) + row[primary_key_name] = Fixtures.identify(label) + end model_class.reflect_on_all_associations.each do |association| case association.macro when :belongs_to - if value = row.delete(association.name.to_s) + # Do not replace association name with association foreign key if they are named the same + fk_name = (association.options[:foreign_key] || "#{association.name}_id").to_s + + if association.name.to_s != fk_name && value = row.delete(association.name.to_s) if association.options[:polymorphic] if value.sub!(/\s*\(([^\)]*)\)\s*$/, "") target_type = $1 @@ -591,7 +598,6 @@ class Fixtures < YAML::Omap end end - fk_name = (association.options[:foreign_key] || "#{association.name}_id").to_s row[fk_name] = Fixtures.identify(value) end when :has_and_belongs_to_many @@ -601,7 +607,7 @@ class Fixtures < YAML::Omap targets.each do |target| join_fixtures["#{label}_#{target}"] = Fixture.new( - { association.primary_key_name => Fixtures.identify(label), + { association.primary_key_name => row[primary_key_name], association.association_foreign_key => Fixtures.identify(target) }, nil) end end @@ -633,10 +639,11 @@ class Fixtures < YAML::Omap @primary_key_name ||= model_class && model_class.primary_key end - def has_primary_key? - model_class && model_class.columns.any? { |c| c.name == primary_key_name } + def has_primary_key_column? + @has_primary_key_column ||= model_class && primary_key_name && + model_class.columns.find { |c| c.name == primary_key_name } end - + def timestamp_column_names @timestamp_column_names ||= %w(created_at created_on updated_at updated_on).select do |name| column_names.include?(name) diff --git a/activerecord/test/aggregations_test.rb b/activerecord/test/aggregations_test.rb index 299e0484e3..ea0339c63a 100644 --- a/activerecord/test/aggregations_test.rb +++ b/activerecord/test/aggregations_test.rb @@ -20,7 +20,7 @@ class AggregationsTest < Test::Unit::TestCase def test_change_single_value_object customers(:david).balance = Money.new(100) customers(:david).save - assert_equal 100, Customer.find(1).balance.amount + assert_equal 100, customers(:david).reload.balance.amount end def test_immutable_value_objects diff --git a/activerecord/test/fixtures/db_definitions/schema.rb b/activerecord/test/fixtures/db_definitions/schema.rb index 9b6b6987d2..f6bd0efa5d 100644 --- a/activerecord/test/fixtures/db_definitions/schema.rb +++ b/activerecord/test/fixtures/db_definitions/schema.rb @@ -335,10 +335,18 @@ ActiveRecord::Schema.define do t.column :parrot_id, :integer t.column :treasure_id, :integer end - + create_table :mateys, :id => false, :force => true do |t| t.column :pirate_id, :integer t.column :target_id, :integer t.column :weight, :integer end + + create_table :ships, :force => true do |t| + t.string :name + t.datetime :created_at + t.datetime :created_on + t.datetime :updated_at + t.datetime :updated_on + end end diff --git a/activerecord/test/fixtures/joke.rb b/activerecord/test/fixtures/joke.rb index 8006a43bd7..3978abc2ba 100644 --- a/activerecord/test/fixtures/joke.rb +++ b/activerecord/test/fixtures/joke.rb @@ -1,6 +1,3 @@ class Joke < ActiveRecord::Base set_table_name 'funny_jokes' end -class Joke < ActiveRecord::Base - set_table_name 'funny_jokes' -end
\ No newline at end of file diff --git a/activerecord/test/fixtures/matey.rb b/activerecord/test/fixtures/matey.rb index 86442e0470..47b0baa974 100644 --- a/activerecord/test/fixtures/matey.rb +++ b/activerecord/test/fixtures/matey.rb @@ -1,4 +1,4 @@ class Matey < ActiveRecord::Base belongs_to :pirate - belongs_to :target, :class_name => 'Pirate', :foreign_key => 'target_id' + belongs_to :target, :class_name => 'Pirate' end diff --git a/activerecord/test/fixtures/parrots.yml b/activerecord/test/fixtures/parrots.yml index 74bc6b4e78..dd2c9548e7 100644 --- a/activerecord/test/fixtures/parrots.yml +++ b/activerecord/test/fixtures/parrots.yml @@ -9,6 +9,11 @@ louis: frederick: name: $LABEL +polly: + id: 4 + name: $LABEL + treasures: sapphire, ruby + DEFAULTS: &DEFAULTS treasures: sapphire, ruby diff --git a/activerecord/test/fixtures/ship.rb b/activerecord/test/fixtures/ship.rb new file mode 100644 index 0000000000..05b09fc1b9 --- /dev/null +++ b/activerecord/test/fixtures/ship.rb @@ -0,0 +1,3 @@ +class Ship < ActiveRecord::Base + self.record_timestamps = false +end
\ No newline at end of file diff --git a/activerecord/test/fixtures/ships.yml b/activerecord/test/fixtures/ships.yml new file mode 100644 index 0000000000..137055aad1 --- /dev/null +++ b/activerecord/test/fixtures/ships.yml @@ -0,0 +1,5 @@ +black_pearl: + name: "Black Pearl" +interceptor: + id: 2 + name: "Interceptor" diff --git a/activerecord/test/fixtures_test.rb b/activerecord/test/fixtures_test.rb index de0321fbf4..d51a4029bc 100755 --- a/activerecord/test/fixtures_test.rb +++ b/activerecord/test/fixtures_test.rb @@ -1,5 +1,8 @@ require 'abstract_unit' +require 'fixtures/post' +require 'fixtures/binary' require 'fixtures/topic' +require 'fixtures/computer' require 'fixtures/developer' require 'fixtures/company' require 'fixtures/task' @@ -11,6 +14,7 @@ require 'fixtures/parrot' require 'fixtures/pirate' require 'fixtures/treasure' require 'fixtures/matey' +require 'fixtures/ship' class FixturesTest < Test::Unit::TestCase self.use_instantiated_fixtures = true @@ -452,7 +456,7 @@ class FasterFixturesTest < Test::Unit::TestCase end class FoxyFixturesTest < Test::Unit::TestCase - fixtures :parrots, :parrots_pirates, :pirates, :treasures, :mateys + fixtures :parrots, :parrots_pirates, :pirates, :treasures, :mateys, :ships, :computers, :developers def test_identifies_strings assert_equal(Fixtures.identify("foo"), Fixtures.identify("foo")) @@ -471,6 +475,12 @@ class FoxyFixturesTest < Test::Unit::TestCase end end + def test_does_not_populate_timestamp_columns_if_model_has_set_record_timestamps_to_false + TIMESTAMP_COLUMNS.each do |property| + assert_nil(ships(:black_pearl).send(property), "should not set #{property}") + end + end + def test_populates_all_columns_with_the_same_time last = nil @@ -498,10 +508,22 @@ class FoxyFixturesTest < Test::Unit::TestCase assert_not_equal(parrots(:george).id, parrots(:louis).id) end + def test_automatically_sets_primary_key + assert_not_nil(ships(:black_pearl)) + end + + def test_preserves_existing_primary_key + assert_equal(2, ships(:interceptor).id) + end + def test_resolves_belongs_to_symbols assert_equal(parrots(:george), pirates(:blackbeard).parrot) end + def test_ignores_belongs_to_symbols_if_association_and_foreign_key_are_named_the_same + assert_equal(developers(:david), computers(:workstation).developer) + end + def test_supports_join_tables assert(pirates(:blackbeard).parrots.include?(parrots(:george))) assert(pirates(:blackbeard).parrots.include?(parrots(:louis))) @@ -514,6 +536,12 @@ class FoxyFixturesTest < Test::Unit::TestCase assert(!parrots(:george).treasures.include?(treasures(:ruby))) end + def test_supports_inline_habtm_with_specified_id + assert(parrots(:polly).treasures.include?(treasures(:ruby))) + assert(parrots(:polly).treasures.include?(treasures(:sapphire))) + assert(!parrots(:polly).treasures.include?(treasures(:diamond))) + end + def test_supports_yaml_arrays assert(parrots(:louis).treasures.include?(treasures(:diamond))) assert(parrots(:louis).treasures.include?(treasures(:sapphire))) @@ -550,4 +578,4 @@ class ActiveSupportSubclassWithFixturesTest < ActiveSupport::TestCase def test_foo assert_equal parrots(:louis), Parrot.find_by_name("King Louis") end -end
\ No newline at end of file +end diff --git a/activerecord/test/mixin_test.rb b/activerecord/test/mixin_test.rb index f8d9edc646..d2118e6764 100644 --- a/activerecord/test/mixin_test.rb +++ b/activerecord/test/mixin_test.rb @@ -78,10 +78,17 @@ class TouchTest < Test::Unit::TestCase def test_create_turned_off Mixin.record_timestamps = false - assert_nil mixins(:set_1).updated_at - mixins(:set_1).save - assert_nil mixins(:set_1).updated_at + mixin = Mixin.new + assert_nil mixin.updated_at + mixin.save + assert_nil mixin.updated_at + + # Make sure Mixin.record_timestamps gets reset, even if this test fails, + # so that other tests do not fail because Mixin.record_timestamps == false + rescue Exception => e + raise e + ensure Mixin.record_timestamps = true end |