Skip to content
Browse files

Foxy fixtures: allow mixed usage to make migration easier and more at…

…tractive. Closes #10004.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8218 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 1370d15 commit 8b3f83105ce719642a8f079f72c4f03c97cfc321 @jeremy jeremy committed Nov 26, 2007
View
2 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]
View
29 activerecord/lib/active_record/fixtures.rb
@@ -563,10 +563,12 @@ def insert_fixtures
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 @@ def insert_fixtures
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 @@ def insert_fixtures
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 @@ def insert_fixtures
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 @@ def primary_key_name
@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)
View
2 activerecord/test/aggregations_test.rb
@@ -20,7 +20,7 @@ def test_find_multiple_value_object
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
View
10 activerecord/test/fixtures/db_definitions/schema.rb
@@ -335,10 +335,18 @@ def create_table(*args, &block)
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
View
3 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
View
2 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
View
5 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
View
3 activerecord/test/fixtures/ship.rb
@@ -0,0 +1,3 @@
+class Ship < ActiveRecord::Base
+ self.record_timestamps = false
+end
View
5 activerecord/test/fixtures/ships.yml
@@ -0,0 +1,5 @@
+black_pearl:
+ name: "Black Pearl"
+interceptor:
+ id: 2
+ name: "Interceptor"
View
32 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/pirate'
require 'fixtures/treasure'
require 'fixtures/matey'
+require 'fixtures/ship'
class FixturesTest < Test::Unit::TestCase
self.use_instantiated_fixtures = true
@@ -452,7 +456,7 @@ def test_cache
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 @@ def test_populates_timestamp_columns
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 @@ def test_generates_unique_ids
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 @@ def test_supports_inline_habtm
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
+end
View
13 activerecord/test/mixin_test.rb
@@ -78,10 +78,17 @@ def test_many_updates
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

0 comments on commit 8b3f831

Please sign in to comment.
Something went wrong with that request. Please try again.