Update rspec matcher to work with custom version association names

`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.
This commit is contained in:
Esteban Pastorino 2017-02-06 18:54:07 +01:00
parent c25b4de0d0
commit 60847d6d5d
3 changed files with 31 additions and 3 deletions

View File

@ -16,7 +16,8 @@ recommendations of [keepachangelog.com](http://keepachangelog.com/).
### Fixed
- None
- [#925](https://github.com/airblade/paper_trail/pull/925) - Update RSpec
matchers to work with custom version association names
## 6.0.2 (2016-12-13)

View File

@ -25,10 +25,16 @@ end
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

View File

@ -0,0 +1,21 @@
require "rails_helper"
describe Document, type: :model do
describe "`have_a_version_with` matcher", versioning: true do
it "works with custom versions association" do
document = Document.create!(name: "Foo")
document.update_attributes!(name: "Bar")
expect(document).to have_a_version_with(name: "Foo")
end
end
describe "`have_a_version_with_changes` matcher", versioning: true do
it "works with custom versions association" do
document = Document.create!(name: "Foo")
document.update_attributes!(name: "Bar")
expect(document).to have_a_version_with_changes(name: "Bar")
end
end
end