Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add active record's counter_cache support #311

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/shoulda/matchers/active_record.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'shoulda/matchers/active_record/association_matcher'
require 'shoulda/matchers/active_record/association_matchers/counter_cache_matcher'
require 'shoulda/matchers/active_record/association_matchers/order_matcher'
require 'shoulda/matchers/active_record/association_matchers/through_matcher'
require 'shoulda/matchers/active_record/association_matchers/dependent_matcher'
Expand Down
6 changes: 6 additions & 0 deletions lib/shoulda/matchers/active_record/association_matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ def order(order)
self
end

def counter_cache(counter_cache = true)
counter_cache_matcher = AssociationMatchers::CounterCacheMatcher.new(counter_cache, name)
add_submatcher(counter_cache_matcher)
self
end

def conditions(conditions)
@options[:conditions] = conditions
self
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module Shoulda # :nodoc:
module Matchers
module ActiveRecord # :nodoc:
module AssociationMatchers
class CounterCacheMatcher
attr_accessor :missing_option

def initialize(counter_cache, name)
@counter_cache = counter_cache
@name = name
@missing_option = ''
end

def description
"counter_cache => #{counter_cache}"
end

def matches?(subject)
subject = ModelReflector.new(subject, name)

if subject.option_set_properly?(counter_cache, :counter_cache)
true
else
self.missing_option = "#{name} should have #{description}"
false
end
end

private
attr_accessor :counter_cache, :name
end
end
end
end
end
19 changes: 19 additions & 0 deletions spec/shoulda/matchers/active_record/association_matcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,25 @@
belonging_to_parent.should_not belong_to(:parent).dependent(:destroy)
end

it 'accepts an association with a valid :counter_cache option' do
belonging_to_parent(:counter_cache => :attribute_count).
should belong_to(:parent).counter_cache(:attribute_count)
end

it 'defaults :counter_cache to true' do
belonging_to_parent(:counter_cache => true).
should belong_to(:parent).counter_cache
end

it 'rejects an association with a bad :counter_cache option' do
belonging_to_parent(:counter_cache => :attribute_count).
should_not belong_to(:parent).counter_cache(true)
end

it 'rejects an association that has no :counter_cache option' do
belonging_to_parent.should_not belong_to(:parent).counter_cache
end

it 'accepts an association with a valid :conditions option' do
define_model :parent, :adopter => :boolean
define_model :child, :parent_id => :integer do
Expand Down