Skip to content
Browse files

Added fixtures :all to test_helper.rb to assume that most people just…

… want all their fixtures loaded all the time [DHH] Added fixtures :all as a way of loading all fixtures in the fixture directory at once (closes #7214) [manfred]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6227 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 781985f commit 87b7b5b3e74cb6ce3efc7e472c933bf846b26cc3 @dhh dhh committed
View
2 activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Added fixtures :all as a way of loading all fixtures in the fixture directory at once #7214 [manfred]
+
* Added database connection as a yield parameter to ActiveRecord::Base.transaction so you can manually rollback [DHH]. Example:
transaction do |transaction|
View
15 activerecord/lib/active_record/fixtures.rb
@@ -244,12 +244,15 @@ def self.instantiate_all_loaded_fixtures(object, load_instances=true)
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
+ 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, class_names[table_name.to_sym], File.join(fixtures_directory, table_name.to_s))
end
+
all_loaded_fixtures.merge! fixtures_map
connection.transaction(Thread.current['open_transactions'] == 0) do
@@ -291,8 +294,8 @@ def insert_fixtures
end
end
- private
+ private
def read_fixture_files
if File.file?(yaml_file_path)
# YAML fixtures
@@ -460,7 +463,13 @@ def self.set_fixture_class(class_names = {})
end
def self.fixtures(*table_names)
- table_names = table_names.flatten.map { |n| n.to_s }
+ if table_names.first == :all
+ table_names = Dir["#{fixture_path}/*.yml"] + Dir["#{fixture_path}/*.csv"]
+ table_names.map! { |f| File.basename(f).split('.')[0..-2].join('.') }
+ else
+ table_names = table_names.flatten.map { |n| n.to_s }
+ end
+
self.fixture_table_names |= table_names
require_fixture_classes(table_names)
setup_fixture_accessors(table_names)
View
0 activerecord/test/fixtures/all/developers.yml
No changes.
View
0 activerecord/test/fixtures/all/people.csv
No changes.
View
0 activerecord/test/fixtures/all/tasks.yml
No changes.
View
9 activerecord/test/fixtures_test.rb
@@ -399,3 +399,12 @@ def load_fixtures
raise 'argh'
end
end
+
+class LoadAllFixturesTest < Test::Unit::TestCase
+ write_inheritable_attribute :fixture_path, File.join(File.dirname(__FILE__), '/fixtures/all')
+ fixtures :all
+
+ def test_all_there
+ assert_equal %w(developers people tasks), fixture_table_names.sort
+ end
+end
View
2 railties/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Added fixtures :all to test_helper.rb to assume that most people just want all their fixtures loaded all the time [DHH]
+
* Added config/initializers where all ruby files within it are automatically loaded after the Rails configuration is done, so you don't have to litter the environment.rb file with a ton of mixed stuff [DHH]
* For new apps, generate a random secret for the cookie-based session store. [Jeremy Kemper]
View
7 railties/helpers/test_helper.rb
@@ -24,5 +24,10 @@ class Test::Unit::TestCase
# then set this back to true.
self.use_instantiated_fixtures = false
+ # Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order.
+ # If you need to control the loading order (due to foreign key constraints etc), you'll
+ # need to change this line to explicitly name the order you desire.
+ fixtures :all
+
# Add more helper methods to be used by all tests here...
-end
+end

0 comments on commit 87b7b5b

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