Skip to content

Commit

Permalink
Update rspec matcher to work with custom version association names
Browse files Browse the repository at this point in the history
`have_a_version_with` and `have_a_version_with_changes` had the
`versions` association call hardcoded.
If `has_paper_trail` had the `versions` option set to something else
(e.g: `has_paper_trail versions: :paper_trail_versions`) the matcher
couldn't be used.

This fixes that issue by dynamically fetching the association name from
the `:versions_association_name` class attribute.
  • Loading branch information
kitop committed Feb 6, 2017
1 parent c25b4de commit 97681fc
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/paper_trail/frameworks/rspec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,16 @@

RSpec::Matchers.define :have_a_version_with do |attributes|
# check if the model has a version with the specified attributes
match { |actual| actual.versions.where_object(attributes).any? }
match do |actual|
versions_association = actual.class.versions_association_name
actual.send(versions_association).where_object(attributes).any?
end
end

RSpec::Matchers.define :have_a_version_with_changes do |attributes|
# check if the model has a version changes with the specified attributes
match { |actual| actual.versions.where_object_changes(attributes).any? }
match do |actual|
versions_association = actual.class.versions_association_name
actual.send(versions_association).where_object_changes(attributes).any?
end
end

0 comments on commit 97681fc

Please sign in to comment.