aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorNicholas Seckar <nseckar@gmail.com>2006-02-27 20:29:28 +0000
committerNicholas Seckar <nseckar@gmail.com>2006-02-27 20:29:28 +0000
commitada11d6313bd1f74c6a229caca36ca063b38da4e (patch)
tree27f5a7e4a3f913d7cbe4200594afe6421a2fa3d6 /activerecord
parentd0bd952aff3f42ed95eed701916bbdf9e6f01cca (diff)
downloadrails-ada11d6313bd1f74c6a229caca36ca063b38da4e.tar.gz
rails-ada11d6313bd1f74c6a229caca36ca063b38da4e.tar.bz2
rails-ada11d6313bd1f74c6a229caca36ca063b38da4e.zip
Add set_fixture_class to allow the use of table name accessors with models which use set_default_name. Closes #3935
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3690 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG2
-rwxr-xr-xactiverecord/lib/active_record/fixtures.rb25
-rwxr-xr-xactiverecord/test/abstract_unit.rb2
-rw-r--r--activerecord/test/fixtures/db_definitions/sqlite.sql5
-rw-r--r--activerecord/test/fixtures/funny_jokes.yml14
-rw-r--r--activerecord/test/fixtures/joke.rb6
-rwxr-xr-xactiverecord/test/fixtures_test.rb28
7 files changed, 66 insertions, 16 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index 2ca0ffdb20..35e645d33e 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Add set_fixture_class to allow the use of table name accessors with models which use set_default_name. [Kevin Clark]
+
* Added that fixtures to placed in subdirectories of the main fixture files are also loaded #3937 [dblack@wobblini.net]
* Define attribute query methods to avoid method_missing calls. #3677 [jonathan@bluewire.net.nz]
diff --git a/activerecord/lib/active_record/fixtures.rb b/activerecord/lib/active_record/fixtures.rb
index 789190fe79..7a0ffbf4ce 100755
--- a/activerecord/lib/active_record/fixtures.rb
+++ b/activerecord/lib/active_record/fixtures.rb
@@ -237,14 +237,13 @@ class Fixtures < YAML::Omap
cattr_accessor :all_loaded_fixtures
self.all_loaded_fixtures = {}
- def self.create_fixtures(fixtures_directory, *table_names)
+ def self.create_fixtures(fixtures_directory, table_names, class_names)
table_names = table_names.flatten.map { |n| n.to_s }
connection = block_given? ? yield : ActiveRecord::Base.connection
-
ActiveRecord::Base.silence do
fixtures_map = {}
fixtures = table_names.map do |table_name|
- fixtures_map[table_name] = Fixtures.new(connection, File.split(table_name.to_s).last, File.join(fixtures_directory, table_name.to_s))
+ fixtures_map[table_name] = Fixtures.new(connection, File.split(table_name.to_s).last, class_names[table_name.to_sym], File.join(fixtures_directory, table_name.to_s))
end
all_loaded_fixtures.merge! fixtures_map
@@ -267,10 +266,10 @@ class Fixtures < YAML::Omap
attr_reader :table_name
- def initialize(connection, table_name, fixture_path, file_filter = DEFAULT_FILTER_RE)
+ def initialize(connection, table_name, class_name, fixture_path, file_filter = DEFAULT_FILTER_RE)
@connection, @table_name, @fixture_path, @file_filter = connection, table_name, fixture_path, file_filter
-
- @class_name = ActiveRecord::Base.pluralize_table_names ? @table_name.singularize.camelize : @table_name.camelize
+ @class_name = class_name ||
+ (ActiveRecord::Base.pluralize_table_names ? @table_name.singularize.camelize : @table_name.camelize)
@table_name = ActiveRecord::Base.table_name_prefix + @table_name + ActiveRecord::Base.table_name_suffix
read_fixture_files
end
@@ -423,6 +422,7 @@ module Test #:nodoc:
class TestCase #:nodoc:
cattr_accessor :fixture_path
class_inheritable_accessor :fixture_table_names
+ class_inheritable_accessor :fixture_class_names
class_inheritable_accessor :use_transactional_fixtures
class_inheritable_accessor :use_instantiated_fixtures # true, false, or :no_instances
class_inheritable_accessor :pre_loaded_fixtures
@@ -431,9 +431,16 @@ module Test #:nodoc:
self.use_transactional_fixtures = false
self.use_instantiated_fixtures = true
self.pre_loaded_fixtures = false
-
+
+ self.fixture_class_names = {}
+
@@already_loaded_fixtures = {}
-
+ self.fixture_class_names = {}
+
+ def self.set_fixture_class(class_names = {})
+ self.fixture_class_names = self.fixture_class_names.merge(class_names)
+ end
+
def self.fixtures(*table_names)
table_names = table_names.flatten.map { |n| n.to_s }
self.fixture_table_names |= table_names
@@ -548,7 +555,7 @@ module Test #:nodoc:
private
def load_fixtures
@loaded_fixtures = {}
- fixtures = Fixtures.create_fixtures(fixture_path, fixture_table_names)
+ fixtures = Fixtures.create_fixtures(fixture_path, fixture_table_names, fixture_class_names)
unless fixtures.nil?
if fixtures.instance_of?(Fixtures)
@loaded_fixtures[fixtures.table_name] = fixtures
diff --git a/activerecord/test/abstract_unit.rb b/activerecord/test/abstract_unit.rb
index 6348394685..498f19b32f 100755
--- a/activerecord/test/abstract_unit.rb
+++ b/activerecord/test/abstract_unit.rb
@@ -16,7 +16,7 @@ class Test::Unit::TestCase #:nodoc:
self.use_transactional_fixtures = (ENV['AR_NO_TX_FIXTURES'] != "yes")
def create_fixtures(*table_names, &block)
- Fixtures.create_fixtures(File.dirname(__FILE__) + "/fixtures/", table_names, &block)
+ Fixtures.create_fixtures(File.dirname(__FILE__) + "/fixtures/", table_names, {}, &block)
end
def assert_date_from_db(expected, actual, message = nil)
diff --git a/activerecord/test/fixtures/db_definitions/sqlite.sql b/activerecord/test/fixtures/db_definitions/sqlite.sql
index 685c2abc8b..5a7fec3d7e 100644
--- a/activerecord/test/fixtures/db_definitions/sqlite.sql
+++ b/activerecord/test/fixtures/db_definitions/sqlite.sql
@@ -4,6 +4,11 @@ CREATE TABLE 'accounts' (
'credit_limit' INTEGER DEFAULT NULL
);
+CREATE TABLE 'funny_jokes' (
+ 'id' INTEGER PRIMARY KEY NOT NULL,
+ 'name' TEXT DEFAULT NULL
+);
+
CREATE TABLE 'companies' (
'id' INTEGER PRIMARY KEY NOT NULL,
'type' VARCHAR(255) DEFAULT NULL,
diff --git a/activerecord/test/fixtures/funny_jokes.yml b/activerecord/test/fixtures/funny_jokes.yml
new file mode 100644
index 0000000000..834481bc60
--- /dev/null
+++ b/activerecord/test/fixtures/funny_jokes.yml
@@ -0,0 +1,14 @@
+a_joke:
+ id: 1
+ name: Knock knock
+
+another_joke:
+ id: 2
+ name: The Aristocrats
+a_joke:
+ id: 1
+ name: Knock knock
+
+another_joke:
+ id: 2
+ name: The Aristocrats \ No newline at end of file
diff --git a/activerecord/test/fixtures/joke.rb b/activerecord/test/fixtures/joke.rb
new file mode 100644
index 0000000000..8006a43bd7
--- /dev/null
+++ b/activerecord/test/fixtures/joke.rb
@@ -0,0 +1,6 @@
+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_test.rb b/activerecord/test/fixtures_test.rb
index 29f7b886a9..72cb977c87 100755
--- a/activerecord/test/fixtures_test.rb
+++ b/activerecord/test/fixtures_test.rb
@@ -4,6 +4,7 @@ require 'fixtures/developer'
require 'fixtures/company'
require 'fixtures/task'
require 'fixtures/reply'
+require 'fixtures/joke'
class FixturesTest < Test::Unit::TestCase
self.use_instantiated_fixtures = true
@@ -110,7 +111,7 @@ class FixturesTest < Test::Unit::TestCase
def test_deprecated_yaml_extension
assert_raise(Fixture::FormatError) {
- Fixtures.new(nil, 'bad_extension', File.join(File.dirname(__FILE__), 'fixtures'))
+ Fixtures.new(nil, 'bad_extension', 'BadExtension', File.join(File.dirname(__FILE__), 'fixtures'))
}
end
@@ -141,26 +142,26 @@ class FixturesTest < Test::Unit::TestCase
end
def test_empty_yaml_fixture
- assert_not_nil Fixtures.new( Account.connection, "accounts", File.dirname(__FILE__) + "/fixtures/naked/yml/accounts")
+ assert_not_nil Fixtures.new( Account.connection, "accounts", 'Account', File.dirname(__FILE__) + "/fixtures/naked/yml/accounts")
end
def test_empty_yaml_fixture_with_a_comment_in_it
- assert_not_nil Fixtures.new( Account.connection, "companies", File.dirname(__FILE__) + "/fixtures/naked/yml/companies")
+ assert_not_nil Fixtures.new( Account.connection, "companies", 'Company', File.dirname(__FILE__) + "/fixtures/naked/yml/companies")
end
def test_dirty_dirty_yaml_file
assert_raises(Fixture::FormatError) do
- Fixtures.new( Account.connection, "courses", File.dirname(__FILE__) + "/fixtures/naked/yml/courses")
+ Fixtures.new( Account.connection, "courses", 'Course', File.dirname(__FILE__) + "/fixtures/naked/yml/courses")
end
end
def test_empty_csv_fixtures
- assert_not_nil Fixtures.new( Account.connection, "accounts", File.dirname(__FILE__) + "/fixtures/naked/csv/accounts")
+ assert_not_nil Fixtures.new( Account.connection, "accounts", 'Account', File.dirname(__FILE__) + "/fixtures/naked/csv/accounts")
end
def test_omap_fixtures
assert_nothing_raised do
- fixtures = Fixtures.new(Account.connection, 'categories', File.dirname(__FILE__) + '/fixtures/categories_ordered')
+ fixtures = Fixtures.new(Account.connection, 'categories', 'Category', File.dirname(__FILE__) + '/fixtures/categories_ordered')
i = 0
fixtures.each do |name, fixture|
@@ -326,3 +327,18 @@ class ForeignKeyFixturesTest < Test::Unit::TestCase
assert true
end
end
+
+class SetTableNameFixturesTest < Test::Unit::TestCase
+ set_fixture_class :funny_jokes => 'Joke'
+ fixtures :funny_jokes
+
+ def test_table_method
+ assert_kind_of Joke, funny_jokes(:a_joke)
+ end
+end
+
+
+
+
+
+