Remove deprecated automatic counter caches on has_many :through

This commit is contained in:
Rafael Mendonça França 2015-01-03 23:19:29 -03:00
parent 37175a24bd
commit 87c8ce340c
3 changed files with 4 additions and 40 deletions

@ -1,3 +1,7 @@
* Remove deprecated automatic counter caches on `has_many :through`.
*Rafael Mendonça França*
* Change the way in which callback chains can be halted.
The preferred method to halt a callback chain from now on is to explicitly

@ -49,16 +49,7 @@ def insert_record(record, validate = true, raise = false)
end
save_through_record(record)
if has_cached_counter? && !through_reflection_updates_counter_cache?
ActiveSupport::Deprecation.warn(<<-MSG.squish)
Automatic updating of counter caches on through associations has been
deprecated, and will be removed in Rails 5. Instead, please set the
appropriate `counter_cache` options on the `has_many` and `belongs_to`
for your associations to #{through_reflection.name}.
MSG
update_counter_in_database(1)
end
record
end
@ -211,11 +202,6 @@ def find_target
def invertible_for?(record)
false
end
def through_reflection_updates_counter_cache?
counter_name = cached_counter_attribute_name
inverse_updates_counter_named?(counter_name, through_reflection)
end
end
end
end

@ -1,26 +0,0 @@
require "cases/helper"
class DeprecatedCounterCacheOnHasManyThroughTest < ActiveRecord::TestCase
class Post < ActiveRecord::Base
has_many :taggings, as: :taggable
has_many :tags, through: :taggings
end
class Tagging < ActiveRecord::Base
belongs_to :taggable, polymorphic: true
belongs_to :tag
end
class Tag < ActiveRecord::Base
end
test "counter caches are updated in the database if the belongs_to association doesn't specify a counter cache" do
post = Post.create!(title: 'Hello', body: 'World!')
assert_deprecated { post.tags << Tag.create!(name: 'whatever') }
assert_equal 1, post.tags.size
assert_equal 1, post.tags_count
assert_equal 1, post.reload.tags.size
assert_equal 1, post.reload.tags_count
end
end