mirror of
https://github.com/github/rails.git
synced 2026-02-10 14:15:00 -05:00
Allow counter_cache to accept a column name
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3825 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
*SVN*
|
||||
|
||||
* Allow :counter_cache to take a column name for custom counter cache columns [Jamis Buck]
|
||||
|
||||
* Documentation fixes for :dependent [robby@planetargon.com]
|
||||
|
||||
* Allow set_fixture_class to take Classes instead of strings for a class in a module. Raise FixtureClassNotFound if a fixture can't load. [Rick Olson]
|
||||
|
||||
@@ -460,7 +460,8 @@ module ActiveRecord
|
||||
# * <tt>:counter_cache</tt> - caches the number of belonging objects on the associate class through use of increment_counter
|
||||
# and decrement_counter. The counter cache is incremented when an object of this class is created and decremented when it's
|
||||
# destroyed. This requires that a column named "#{table_name}_count" (such as comments_count for a belonging Comment class)
|
||||
# is used on the associate class (such as a Post class).
|
||||
# is used on the associate class (such as a Post class). You can also specify a custom counter cache column by given that
|
||||
# name instead of a true/false value to this option (e.g., <tt>:counter_cache => :my_custom_counter</tt>.)
|
||||
# * <tt>:include</tt> - specify second-order associations that should be eager loaded when this object is loaded.
|
||||
#
|
||||
# Option examples:
|
||||
@@ -515,13 +516,17 @@ module ActiveRecord
|
||||
end
|
||||
|
||||
if options[:counter_cache]
|
||||
cache_column = options[:counter_cache] == true ?
|
||||
"#{self.to_s.underscore.pluralize}_count" :
|
||||
options[:counter_cache]
|
||||
|
||||
module_eval(
|
||||
"after_create '#{reflection.name}.class.increment_counter(\"#{self.to_s.underscore.pluralize + "_count"}\", #{reflection.primary_key_name})" +
|
||||
"after_create '#{reflection.name}.class.increment_counter(\"#{cache_column}\", #{reflection.primary_key_name})" +
|
||||
" unless #{reflection.name}.nil?'"
|
||||
)
|
||||
|
||||
module_eval(
|
||||
"before_destroy '#{reflection.name}.class.decrement_counter(\"#{self.to_s.underscore.pluralize + "_count"}\", #{reflection.primary_key_name})" +
|
||||
"before_destroy '#{reflection.name}.class.decrement_counter(\"#{cache_column}\", #{reflection.primary_key_name})" +
|
||||
" unless #{reflection.name}.nil?'"
|
||||
)
|
||||
end
|
||||
|
||||
@@ -917,7 +917,27 @@ class BelongsToAssociationsTest < Test::Unit::TestCase
|
||||
assert_not_nil computer.developer, ":foreign key == attribute didn't lock up" # '
|
||||
end
|
||||
|
||||
def xtest_counter_cache
|
||||
def test_counter_cache
|
||||
topic = Topic.create :title => "Zoom-zoom-zoom"
|
||||
assert_equal 0, topic[:replies_count]
|
||||
|
||||
reply = Reply.create(:title => "re: zoom", :content => "speedy quick!")
|
||||
reply.topic = topic
|
||||
|
||||
assert_equal 1, topic.reload[:replies_count]
|
||||
end
|
||||
|
||||
def test_custom_counter_cache
|
||||
reply = Reply.create(:title => "re: zoom", :content => "speedy quick!")
|
||||
assert_equal 0, reply[:replies_count]
|
||||
|
||||
silly = SillyReply.create(:title => "gaga", :content => "boo-boo")
|
||||
silly.reply = reply
|
||||
|
||||
assert_equal 1, reply.reload[:replies_count]
|
||||
end
|
||||
|
||||
def xtest_size_uses_counter_cache
|
||||
apple = Firm.create("name" => "Apple")
|
||||
final_cut = apple.clients.create("name" => "Final Cut")
|
||||
|
||||
|
||||
@@ -1,20 +1,20 @@
|
||||
ActiveRecord::Schema.define do
|
||||
|
||||
create_table "taggings", :force => true do |t|
|
||||
t.column "tag_id", :integer
|
||||
t.column "taggable_type", :string
|
||||
t.column "taggable_id", :integer
|
||||
create_table :taggings, :force => true do |t|
|
||||
t.column :tag_id, :integer
|
||||
t.column :taggable_type, :string
|
||||
t.column :taggable_id, :integer
|
||||
end
|
||||
|
||||
create_table "tags", :force => true do |t|
|
||||
t.column "name", :string
|
||||
create_table :tags, :force => true do |t|
|
||||
t.column :name, :string
|
||||
t.column :taggings_count, :integer, :default => 0
|
||||
end
|
||||
|
||||
create_table "categorizations", :force => true do |t|
|
||||
t.column "category_id", :integer
|
||||
t.column "post_id", :integer
|
||||
t.column "author_id", :integer
|
||||
create_table :categorizations, :force => true do |t|
|
||||
t.column :category_id, :integer
|
||||
t.column :post_id, :integer
|
||||
t.column :author_id, :integer
|
||||
end
|
||||
|
||||
add_column :posts, :taggings_count, :integer, :default => 0
|
||||
|
||||
1
activerecord/test/fixtures/reply.rb
vendored
1
activerecord/test/fixtures/reply.rb
vendored
@@ -33,4 +33,5 @@ class Reply < Topic
|
||||
end
|
||||
|
||||
class SillyReply < Reply
|
||||
belongs_to :reply, :foreign_key => "parent_id", :counter_cache => :replies_count
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user