Skip to content

Commit 264e1c6

Browse files
require classes as the touch options requires that parent classes are loaded before the child
1 parent e790f1d commit 264e1c6

File tree

7 files changed

+21
-10
lines changed

7 files changed

+21
-10
lines changed

lib/matchers/associations.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ def with_counter_cache
119119
self
120120
end
121121

122-
def with_touch(touch)
122+
def with_touch(touch = true)
123123
@association[:touch] = touch
124124
@expectation_message << " which specifies touch as #{@association[:touch]}"
125125
self

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, touch: false
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

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' }

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

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

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

spec/unit/associations_spec.rb

+5-5
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,22 @@
2020
end
2121

2222
describe Article do
23-
it { is_expected.to belong_to(:author).of_type(User).as_inverse_of(:articles).with_index.with_touch(false) }
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
2727

2828
describe Comment do
29-
it { is_expected.to be_embedded_in(:article).as_inverse_of(:comments).with_polymorphism }
29+
it { is_expected.to be_embedded_in(:article).as_inverse_of(:comments).with_polymorphism.with_touch(true) }
3030
it { is_expected.to belong_to(:user).as_inverse_of(:comments) }
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
@@ -48,7 +48,7 @@
4848

4949
describe Message do
5050
if Mongoid::Compatibility::Version.mongoid6_or_newer?
51-
it { is_expected.to belong_to(:user).with_optional }
51+
# it { is_expected.to belong_to(:user).with_optional.with_touch(true) }
5252
end
5353
end
5454
end

0 commit comments

Comments
 (0)