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

Changeset 3756

Show
Ignore:
Timestamp:
03/04/06 15:11:17 (3 years ago)
Author:
minam
Message:

Make counter_cache work with polymorphic belongs_to

Files:

Legend:

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

    r3753 r3756  
    11*SVN* 
     2 
     3* Make counter_cache work with polymorphic belongs_to [Jamis Buck] 
    24 
    35* Fixed that calling HasOneProxy#build_model repeatedly would cause saving to happen #4058 [anna@wota.jp] 
  • trunk/activerecord/lib/active_record/associations.rb

    r3681 r3756  
    507507          end 
    508508       
    509           if options[:counter_cache] 
    510             module_eval( 
    511               "after_create '#{reflection.class_name}.increment_counter(\"#{self.to_s.underscore.pluralize + "_count"}\", #{reflection.primary_key_name})" + 
    512               " unless #{reflection.name}.nil?'" 
    513             ) 
    514  
    515             module_eval( 
    516               "before_destroy '#{reflection.class_name}.decrement_counter(\"#{self.to_s.underscore.pluralize + "_count"}\", #{reflection.primary_key_name})" + 
    517               " unless #{reflection.name}.nil?'" 
    518             )           
    519           end 
    520  
    521509          # deprecated api 
    522510          deprecated_has_association_method(reflection.name) 
    523511          deprecated_association_comparison_method(reflection.name, reflection.class_name) 
     512        end 
     513 
     514        if options[:counter_cache] 
     515          module_eval( 
     516            "after_create '#{reflection.name}.class.increment_counter(\"#{self.to_s.underscore.pluralize + "_count"}\", #{reflection.primary_key_name})" + 
     517            " unless #{reflection.name}.nil?'" 
     518          ) 
     519 
     520          module_eval( 
     521            "before_destroy '#{reflection.name}.class.decrement_counter(\"#{self.to_s.underscore.pluralize + "_count"}\", #{reflection.primary_key_name})" + 
     522            " unless #{reflection.name}.nil?'" 
     523          )           
    524524        end 
    525525      end 
  • trunk/activerecord/test/associations_join_model_test.rb

    r3623 r3756  
    105105    assert_equal [authors(:mary)], posts(:authorless).authors 
    106106  end 
     107 
     108  def test_belongs_to_polymorphic_with_counter_cache 
     109    assert_equal 0, posts(:welcome)[:taggings_count] 
     110    tagging = posts(:welcome).taggings.create(:tag => tags(:general)) 
     111    assert_equal 1, posts(:welcome, :reload)[:taggings_count] 
     112    tagging.destroy 
     113    assert posts(:welcome, :reload)[:taggings_count].zero? 
     114  end 
    107115end 
  • trunk/activerecord/test/fixtures/db_definitions/schema.rb

    r3279 r3756  
    99  create_table "tags", :force => true do |t| 
    1010    t.column "name", :string 
     11    t.column :taggings_count, :integer, :default => 0 
    1112  end 
    1213 
     
    1718  end 
    1819 
     20  add_column :posts, :taggings_count, :integer, :default => 0 
     21 
    1922end 
  • trunk/activerecord/test/fixtures/tagging.rb

    r3209 r3756  
    11class Tagging < ActiveRecord::Base 
    22  belongs_to :tag 
    3   belongs_to :taggable, :polymorphic => true 
     3  belongs_to :taggable, :polymorphic => true, :counter_cache => true 
    44end