Skip to content

Commit

Permalink
Make fixture accessors work when fixture name is not same as the tabl…
Browse files Browse the repository at this point in the history
…e name. [#124 state:resolved]

Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
  • Loading branch information
Rhett Sutphin authored and lifo committed Jul 14, 2008
1 parent 697ee1a commit d72c665
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
7 changes: 4 additions & 3 deletions activerecord/lib/active_record/fixtures.rb
Expand Up @@ -541,10 +541,11 @@ def self.identify(label)
label.to_s.hash.abs
end

attr_reader :table_name
attr_reader :table_name, :name

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
@name = table_name # preserve fixture base name
@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}"
Expand Down Expand Up @@ -963,9 +964,9 @@ def load_fixtures
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
@loaded_fixtures[fixtures.name] = fixtures
else
fixtures.each { |f| @loaded_fixtures[f.table_name] = f }
fixtures.each { |f| @loaded_fixtures[f.name] = f }
end
end
end
Expand Down
29 changes: 29 additions & 0 deletions activerecord/test/cases/fixtures_test.rb
Expand Up @@ -15,6 +15,7 @@
require 'models/treasure'
require 'models/matey'
require 'models/ship'
require 'models/book'

class FixturesTest < ActiveRecord::TestCase
self.use_instantiated_fixtures = true
Expand Down Expand Up @@ -373,6 +374,34 @@ def test_table_method
end
end

class FixtureNameIsNotTableNameFixturesTest < ActiveRecord::TestCase
set_fixture_class :items => Book
fixtures :items
# Set to false to blow away fixtures cache and ensure our fixtures are loaded
# and thus takes into account our set_fixture_class
self.use_transactional_fixtures = false

def test_named_accessor
assert_kind_of Book, items(:dvd)
end
end

class FixtureNameIsNotTableNameMultipleFixturesTest < ActiveRecord::TestCase
set_fixture_class :items => Book, :funny_jokes => Joke
fixtures :items, :funny_jokes
# Set to false to blow away fixtures cache and ensure our fixtures are loaded
# and thus takes into account our set_fixture_class
self.use_transactional_fixtures = false

def test_named_accessor_of_differently_named_fixture
assert_kind_of Book, items(:dvd)
end

def test_named_accessor_of_same_named_fixture
assert_kind_of Joke, funny_jokes(:a_joke)
end
end

class CustomConnectionFixturesTest < ActiveRecord::TestCase
set_fixture_class :courses => Course
fixtures :courses
Expand Down

0 comments on commit d72c665

Please sign in to comment.