public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Make fixture accessors work when fixture name is not same as the table name. 
[#124 state:resolved]

Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
Rhett Sutphin (author)
Sun Jul 13 18:01:52 -0700 2008
lifo (committer)
Sun Jul 13 18:01:52 -0700 2008
commit  d72c66532f959846cdc2d7fb1dc1ef6ba87bdcb1
tree    f683d74335a43b992e9a1f9e5979d95e1ec12342
parent  697ee1a50dea7580a7240535d3ad89d2d090721a
...
541
542
543
544
 
545
546
547
 
548
549
550
...
963
964
965
966
 
967
968
 
969
970
971
...
541
542
543
 
544
545
546
547
548
549
550
551
...
964
965
966
 
967
968
 
969
970
971
972
0
@@ -541,10 +541,11 @@ class Fixtures < (RUBY_VERSION < '1.9' ? YAML::Omap : Hash)
0
     label.to_s.hash.abs
0
   end
0
 
0
-  attr_reader :table_name
0
+  attr_reader :table_name, :name
0
 
0
   def initialize(connection, table_name, class_name, fixture_path, file_filter = DEFAULT_FILTER_RE)
0
     @connection, @table_name, @fixture_path, @file_filter = connection, table_name, fixture_path, file_filter
0
+    @name = table_name # preserve fixture base name
0
     @class_name = class_name ||
0
                   (ActiveRecord::Base.pluralize_table_names ? @table_name.singularize.camelize : @table_name.camelize)
0
     @table_name = "#{ActiveRecord::Base.table_name_prefix}#{@table_name}#{ActiveRecord::Base.table_name_suffix}"
0
@@ -963,9 +964,9 @@ module Test #:nodoc:
0
           fixtures = Fixtures.create_fixtures(fixture_path, fixture_table_names, fixture_class_names)
0
           unless fixtures.nil?
0
             if fixtures.instance_of?(Fixtures)
0
-              @loaded_fixtures[fixtures.table_name] = fixtures
0
+              @loaded_fixtures[fixtures.name] = fixtures
0
             else
0
-              fixtures.each { |f| @loaded_fixtures[f.table_name] = f }
0
+              fixtures.each { |f| @loaded_fixtures[f.name] = f }
0
             end
0
           end
0
         end
...
15
16
17
 
18
19
20
...
373
374
375
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
376
377
378
...
15
16
17
18
19
20
21
...
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
0
@@ -15,6 +15,7 @@ require 'models/pirate'
0
 require 'models/treasure'
0
 require 'models/matey'
0
 require 'models/ship'
0
+require 'models/book'
0
 
0
 class FixturesTest < ActiveRecord::TestCase
0
   self.use_instantiated_fixtures = true
0
@@ -373,6 +374,34 @@ class CheckSetTableNameFixturesTest < ActiveRecord::TestCase
0
   end
0
 end
0
 
0
+class FixtureNameIsNotTableNameFixturesTest < ActiveRecord::TestCase
0
+  set_fixture_class :items => Book
0
+  fixtures :items
0
+  # Set to false to blow away fixtures cache and ensure our fixtures are loaded 
0
+  # and thus takes into account our set_fixture_class
0
+  self.use_transactional_fixtures = false
0
+
0
+  def test_named_accessor
0
+    assert_kind_of Book, items(:dvd)
0
+  end
0
+end
0
+
0
+class FixtureNameIsNotTableNameMultipleFixturesTest < ActiveRecord::TestCase
0
+  set_fixture_class :items => Book, :funny_jokes => Joke
0
+  fixtures :items, :funny_jokes
0
+  # Set to false to blow away fixtures cache and ensure our fixtures are loaded 
0
+  # and thus takes into account our set_fixture_class
0
+  self.use_transactional_fixtures = false
0
+
0
+  def test_named_accessor_of_differently_named_fixture
0
+    assert_kind_of Book, items(:dvd)
0
+  end
0
+
0
+  def test_named_accessor_of_same_named_fixture
0
+    assert_kind_of Joke, funny_jokes(:a_joke)
0
+  end
0
+end
0
+
0
 class CustomConnectionFixturesTest < ActiveRecord::TestCase
0
   set_fixture_class :courses => Course
0
   fixtures :courses

Comments