Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 6940

Show
Ignore:
Timestamp:
06/05/07 03:47:02 (1 year ago)
Author:
bitsweat
Message:

Fixtures: people(:technomancy, :josh) returns both fixtures. Closes #7880.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activerecord/CHANGELOG

    r6919 r6940  
    11*SVN* 
     2 
     3* Fixtures: people(:technomancy, :josh) returns both fixtures.  #7880 [technomancy, Josh Peek] 
    24 
    35* Calculations support non-numeric foreign keys.  #8154 [kamal] 
  • trunk/activerecord/lib/active_record/fixtures.rb

    r6859 r6940  
    502502      end 
    503503 
    504       def self.setup_fixture_accessors(table_names=nil) 
     504      def self.setup_fixture_accessors(table_names = nil) 
    505505        (table_names || fixture_table_names).each do |table_name| 
    506           table_name = table_name.to_s.tr('.','_') 
    507  
    508           define_method(table_name) do |fixture, *optionals| 
    509             force_reload = optionals.shift 
     506          table_name = table_name.to_s.tr('.', '_') 
     507 
     508          define_method(table_name) do |*fixtures| 
     509            force_reload = fixtures.pop if fixtures.last == true || fixtures.last == :reload 
     510 
    510511            @fixture_cache[table_name] ||= Hash.new 
    511             @fixture_cache[table_name][fixture] = nil if force_reload 
    512  
    513             if @loaded_fixtures[table_name][fixture.to_s] 
    514               @fixture_cache[table_name][fixture] ||= @loaded_fixtures[table_name][fixture.to_s].find 
    515             else 
    516               raise StandardError, "No fixture with name '#{fixture}' found for table '#{table_name}'" 
    517             end 
     512 
     513            instances = fixtures.map do |fixture| 
     514              @fixture_cache[table_name].delete(fixture) if force_reload 
     515 
     516              if @loaded_fixtures[table_name][fixture.to_s] 
     517                @fixture_cache[table_name][fixture] ||= @loaded_fixtures[table_name][fixture.to_s].find 
     518              else 
     519                raise StandardError, "No fixture with name '#{fixture}' found for table '#{table_name}'" 
     520              end 
     521            end 
     522 
     523            instances.size == 1 ? instances.first : instances 
    518524          end 
    519525        end 
  • trunk/activerecord/test/associations_test.rb

    r6832 r6940  
    744744    number_of_clients = companies(:first_firm).clients.size 
    745745    the_client = companies(:first_firm).clients.find_or_create_by_name("Yet another client") 
    746     assert_equal number_of_clients + 1, companies(:first_firm, :refresh).clients.size 
     746    assert_equal number_of_clients + 1, companies(:first_firm, :reload).clients.size 
    747747    assert_equal the_client, companies(:first_firm).clients.find_or_create_by_name("Yet another client") 
    748     assert_equal number_of_clients + 1, companies(:first_firm, :refresh).clients.size 
     748    assert_equal number_of_clients + 1, companies(:first_firm, :reload).clients.size 
    749749  end 
    750750 
  • trunk/activerecord/test/fixtures_test.rb

    r6798 r6940  
    260260    assert_equal 50, accounts(:signals37).credit_limit 
    261261  end 
     262 
     263  def test_accessor_methods_with_multiple_args 
     264    assert_equal 2, topics(:first, :second).size 
     265    assert_raise(StandardError) { topics([:first, :second]) } 
     266  end 
     267 
     268  uses_mocha 'reloading_fixtures_through_accessor_methods' do 
     269    def test_reloading_fixtures_through_accessor_methods 
     270      assert_equal "The First Topic", topics(:first).title 
     271      @loaded_fixtures['topics']['first'].expects(:find).returns(stub(:title => "Fresh Topic!")) 
     272      assert_equal "Fresh Topic!", topics(:first, true).title 
     273    end 
     274  end 
    262275end 
    263276