Skip to content

Commit

Permalink
Add support to match against touch
Browse files Browse the repository at this point in the history
add support with with_touch matcher

add support for touch matcher

Update readme

revert section of readme

push tests

update changelog

require classes as the touch options requires that parent classes are loaded before the child

update documentation

generate appraisal files

fix rubocop

remove version on debug dev dependency

remove debug as a dev dependency

update readme

remove require debug

remove commented out code

update version

remove test on comment
  • Loading branch information
sairamsrinivasan committed May 27, 2024
1 parent f55e0f9 commit ef72704
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 9 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
### 4.2.0 (Next)
### 4.3.0 (Next)

* [#239](https://github.com/mongoid/mongoid-rspec/pull/239): Add support for if/unless validator options - [@knovoselic](https://github.com/knovoselic).
* [#244](https://github.com/mongoid/mongoid-rspec/pull/244): Migration from TravisCI to GitHub Actions - [@skalibog](https://github.com/skalibog).
* [#245](https://github.com/mongoid/mongoid-rspec/pull/245): Add support for Mongoid 9 - [@saisrinivasan](https://github.com/SairamSrinivasan).
* [#246](https://github.com/mongoid/mongoid-rspec/pull/246): Add support for touch options - [@saisrinivasan](https://github.com/SairamSrinivasan).
* Your contribution here.

### 4.1.0 (6/12/2020)
Expand Down
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,27 @@ RSpec.describe Article do
it { is_expected.to embed_many(:comments) }
end

# when the touch option is provided, then we can also verify that it is set

# by default, with_touch matches true when no parameters are provided
describe Article do
it { is_expected.to belong_to(:author).of_type(User).as_inverse_of(:articles).with_index.with_touch }
end

# parameters are supported for explicit matching
describe Comment do
it { is_expected.to be_embedded_in(:article).as_inverse_of(:comments).with_polymorphism.with_touch(true) }
end

describe Permalink do
it { is_expected.to be_embedded_in(:linkable).as_inverse_of(:link).with_touch(false) }
end

# touch can also take a symbol representing a field on the parent to touch
describe Record do
it { is_expected.to belong_to(:user).as_inverse_of(:record).with_touch(:record_updated_at) }
end

RSpec.describe Comment do
it { is_expected.to be_embedded_in(:article).as_inverse_of(:comments) }
it { is_expected.to belong_to(:user).as_inverse_of(:comments) }
Expand Down
15 changes: 15 additions & 0 deletions lib/matchers/associations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ def with_counter_cache
self
end

def with_touch(touch = true)
@association[:touch] = touch
@expectation_message << " which specifies touch as #{@association[:touch]}"
self
end

def with_optional
@association[:optional] = true
@expectation_message << " which specifies optional as #{@association[:optional]}"
Expand Down Expand Up @@ -272,6 +278,15 @@ def matches?(actual)
end
end

unless @association[:touch].nil?
if metadata.options[:touch] != @association[:touch]
@negative_result_message = "#{@positive_result_message} which sets touch as #{metadata.options[:touch].inspect}"
return false
else
@positive_result_message = "#{@positive_result_message} which sets touch as #{metadata.options[:touch].inspect}"
end
end

if @association[:optional]
if metadata.options[:optional] != true
@negative_result_message = "#{@positive_result_message} which did not set optional"
Expand Down
2 changes: 1 addition & 1 deletion lib/mongoid/rspec/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Mongoid
module RSpec
VERSION = '4.1.1'.freeze
VERSION = '4.3.0'.freeze
end
end
8 changes: 7 additions & 1 deletion spec/models/article.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'user'

class Article
include Mongoid::Document
include Mongoid::Timestamps
Expand All @@ -13,7 +15,11 @@ class Article

embeds_many :comments, cascade_callbacks: true, inverse_of: :article
embeds_one :permalink, inverse_of: :linkable, class_name: 'Permalink'
belongs_to :author, class_name: 'User', inverse_of: :articles, index: true
belongs_to :author,
class_name: 'User',
inverse_of: :articles,
index: true,
touch: true

validates :title, presence: true

Expand Down
2 changes: 1 addition & 1 deletion spec/models/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Message
if Mongoid::Compatibility::Version.mongoid6_or_newer?
belongs_to :user, optional: true
else
belongs_to :user
belongs_to :user, touch: true
end

validates :identifier, uniqueness: { message: 'uniqueness' }
Expand Down
2 changes: 2 additions & 0 deletions spec/models/permalink.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'article'

class Permalink
include Mongoid::Document

Expand Down
4 changes: 3 additions & 1 deletion spec/models/record.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require 'user'

class Record
include Mongoid::Document

belongs_to :user, inverse_of: :record
belongs_to :user, inverse_of: :record, touch: :record_updated_at, optional: true
end
3 changes: 2 additions & 1 deletion spec/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class User
include Mongoid::Document
include Mongoid::Timestamps::Created
include Mongoid::Timestamps

field :login
field :email
Expand All @@ -9,6 +9,7 @@ class User
field :password, type: String
field :provider_uid
field :locale
field :record_updated_at, type: Time

belongs_to :site, inverse_of: :users
has_many :articles, foreign_key: :author_id, order: :title
Expand Down
6 changes: 3 additions & 3 deletions spec/unit/associations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
end

describe Article do
it { is_expected.to belong_to(:author).of_type(User).as_inverse_of(:articles).with_index }
it { is_expected.to belong_to(:author).of_type(User).as_inverse_of(:articles).with_index.with_touch }
it { is_expected.to embed_many(:comments).as_inverse_of(:article).with_cascading_callbacks }
it { is_expected.to embed_one(:permalink).as_inverse_of(:linkable) }
end
Expand All @@ -31,11 +31,11 @@
end

describe Record do
it { is_expected.to belong_to(:user).as_inverse_of(:record) }
it { is_expected.to belong_to(:user).as_inverse_of(:record).with_touch(:record_updated_at) }
end

describe Permalink do
it { is_expected.to be_embedded_in(:linkable).as_inverse_of(:link) }
it { is_expected.to be_embedded_in(:linkable).as_inverse_of(:link).with_touch(false) }
end

describe Site do
Expand Down

0 comments on commit ef72704

Please sign in to comment.