Skip to content

Commit

Permalink
Add specs for new counter_cache syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
theodorton committed May 31, 2024
1 parent e4bc2df commit 4224dec
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def normalize_value
when String, Symbol
{ active: true, column: counter_cache.to_s }
when Hash
{ active: true }.merge(counter_cache)
{ active: true, column: nil }.merge(counter_cache)
else
raise ArgumentError, 'Invalid counter_cache option'
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,28 @@
expect(belonging_to_parent).not_to belong_to(:parent).counter_cache
end

if rails_version > 7.1
it 'accepts :counter_cache with a hash' do
expect(belonging_to_parent(counter_cache: { active: true })).
to belong_to(:parent).counter_cache
end

it 'accepts :counter_cache with active false when passed' do
expect(belonging_to_parent(counter_cache: { active: false })).
to belong_to(:parent).counter_cache(active: false)
end

it 'rejects :counter_cache with active false when mismatch' do
expect(belonging_to_parent(counter_cache: { active: true })).
not_to belong_to(:parent).counter_cache(active: false)
end

it 'rejects :counter_cache with when column mismatch' do
expect(belonging_to_parent(counter_cache: { column: :attribute_count })).
not_to belong_to(:parent).counter_cache(true)
end
end

it 'accepts an association with a valid :inverse_of option' do
expect(belonging_to_with_inverse(:parent, :children)).
to belong_to(:parent).inverse_of(:children)
Expand Down

0 comments on commit 4224dec

Please sign in to comment.