Skip to content

Commit ef72704

Browse files
Add support to match against touch
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
1 parent f55e0f9 commit ef72704

File tree

10 files changed

+57
-9
lines changed

10 files changed

+57
-9
lines changed

Diff for: CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
### 4.2.0 (Next)
1+
### 4.3.0 (Next)
22

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

89
### 4.1.0 (6/12/2020)

Diff for: README.md

+21
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,27 @@ RSpec.describe Article do
256256
it { is_expected.to embed_many(:comments) }
257257
end
258258

259+
# when the touch option is provided, then we can also verify that it is set
260+
261+
# by default, with_touch matches true when no parameters are provided
262+
describe Article do
263+
it { is_expected.to belong_to(:author).of_type(User).as_inverse_of(:articles).with_index.with_touch }
264+
end
265+
266+
# parameters are supported for explicit matching
267+
describe Comment do
268+
it { is_expected.to be_embedded_in(:article).as_inverse_of(:comments).with_polymorphism.with_touch(true) }
269+
end
270+
271+
describe Permalink do
272+
it { is_expected.to be_embedded_in(:linkable).as_inverse_of(:link).with_touch(false) }
273+
end
274+
275+
# touch can also take a symbol representing a field on the parent to touch
276+
describe Record do
277+
it { is_expected.to belong_to(:user).as_inverse_of(:record).with_touch(:record_updated_at) }
278+
end
279+
259280
RSpec.describe Comment do
260281
it { is_expected.to be_embedded_in(:article).as_inverse_of(:comments) }
261282
it { is_expected.to belong_to(:user).as_inverse_of(:comments) }

Diff for: lib/matchers/associations.rb

+15
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,12 @@ def with_counter_cache
119119
self
120120
end
121121

122+
def with_touch(touch = true)
123+
@association[:touch] = touch
124+
@expectation_message << " which specifies touch as #{@association[:touch]}"
125+
self
126+
end
127+
122128
def with_optional
123129
@association[:optional] = true
124130
@expectation_message << " which specifies optional as #{@association[:optional]}"
@@ -272,6 +278,15 @@ def matches?(actual)
272278
end
273279
end
274280

281+
unless @association[:touch].nil?
282+
if metadata.options[:touch] != @association[:touch]
283+
@negative_result_message = "#{@positive_result_message} which sets touch as #{metadata.options[:touch].inspect}"
284+
return false
285+
else
286+
@positive_result_message = "#{@positive_result_message} which sets touch as #{metadata.options[:touch].inspect}"
287+
end
288+
end
289+
275290
if @association[:optional]
276291
if metadata.options[:optional] != true
277292
@negative_result_message = "#{@positive_result_message} which did not set optional"

Diff for: lib/mongoid/rspec/version.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module Mongoid
22
module RSpec
3-
VERSION = '4.1.1'.freeze
3+
VERSION = '4.3.0'.freeze
44
end
55
end

Diff for: spec/models/article.rb

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require 'user'
2+
13
class Article
24
include Mongoid::Document
35
include Mongoid::Timestamps
@@ -13,7 +15,11 @@ class Article
1315

1416
embeds_many :comments, cascade_callbacks: true, inverse_of: :article
1517
embeds_one :permalink, inverse_of: :linkable, class_name: 'Permalink'
16-
belongs_to :author, class_name: 'User', inverse_of: :articles, index: true
18+
belongs_to :author,
19+
class_name: 'User',
20+
inverse_of: :articles,
21+
index: true,
22+
touch: true
1723

1824
validates :title, presence: true
1925

Diff for: spec/models/message.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class Message
88
if Mongoid::Compatibility::Version.mongoid6_or_newer?
99
belongs_to :user, optional: true
1010
else
11-
belongs_to :user
11+
belongs_to :user, touch: true
1212
end
1313

1414
validates :identifier, uniqueness: { message: 'uniqueness' }

Diff for: spec/models/permalink.rb

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require 'article'
2+
13
class Permalink
24
include Mongoid::Document
35

Diff for: spec/models/record.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
require 'user'
2+
13
class Record
24
include Mongoid::Document
35

4-
belongs_to :user, inverse_of: :record
6+
belongs_to :user, inverse_of: :record, touch: :record_updated_at, optional: true
57
end

Diff for: spec/models/user.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class User
22
include Mongoid::Document
3-
include Mongoid::Timestamps::Created
3+
include Mongoid::Timestamps
44

55
field :login
66
field :email
@@ -9,6 +9,7 @@ class User
99
field :password, type: String
1010
field :provider_uid
1111
field :locale
12+
field :record_updated_at, type: Time
1213

1314
belongs_to :site, inverse_of: :users
1415
has_many :articles, foreign_key: :author_id, order: :title

Diff for: spec/unit/associations_spec.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
end
2121

2222
describe Article do
23-
it { is_expected.to belong_to(:author).of_type(User).as_inverse_of(:articles).with_index }
23+
it { is_expected.to belong_to(:author).of_type(User).as_inverse_of(:articles).with_index.with_touch }
2424
it { is_expected.to embed_many(:comments).as_inverse_of(:article).with_cascading_callbacks }
2525
it { is_expected.to embed_one(:permalink).as_inverse_of(:linkable) }
2626
end
@@ -31,11 +31,11 @@
3131
end
3232

3333
describe Record do
34-
it { is_expected.to belong_to(:user).as_inverse_of(:record) }
34+
it { is_expected.to belong_to(:user).as_inverse_of(:record).with_touch(:record_updated_at) }
3535
end
3636

3737
describe Permalink do
38-
it { is_expected.to be_embedded_in(:linkable).as_inverse_of(:link) }
38+
it { is_expected.to be_embedded_in(:linkable).as_inverse_of(:link).with_touch(false) }
3939
end
4040

4141
describe Site do

0 commit comments

Comments
 (0)