Skip to content

Commit 597fc05

Browse files
committed
Added support for Mongoid 7.
1 parent 4db3213 commit 597fc05

File tree

25 files changed

+482
-265
lines changed

25 files changed

+482
-265
lines changed

.github/workflows/test-mongodb.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ jobs:
99
entry:
1010
- { ruby: '3.4', mongoid: '~> 6.4.8', mongodb: '6.0', grape: '~> 1.7.0' }
1111
- { ruby: '3.4', mongoid: '~> 6.4.8', mongodb: '6.0', grape: '~> 2.0.0' }
12+
- { ruby: '3.4', mongoid: '~> 7.5.4', mongodb: '7.0', grape: '~> 2.4.0' }
1213
name: test (ruby=${{ matrix.entry.ruby }}, mongoid=${{ matrix.entry.mongoid }}, mongodb=${{ matrix.entry.mongodb }}, grape=${{ matrix.entry.grape }})
1314
env:
1415
MONGOID_VERSION: ${{ matrix.entry.mongoid }}

.rubocop.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@ AllCops:
22
TargetRubyVersion: 3.0
33
NewCops: enable
44
Exclude:
5-
- bin/**/*
6-
- gemfiles/**/*
7-
- spec/**/*
85
- vendor/**/*
96

107
inherit_from: .rubocop_todo.yml

.rubocop_todo.yml

Lines changed: 130 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config`
3-
# on 2025-09-24 17:33:03 UTC using RuboCop version 1.80.2.
3+
# on 2025-09-25 02:12:55 UTC using RuboCop version 1.80.2.
44
# The point is for the user to remove these configuration records
55
# one by one as the offenses are removed from the code base.
66
# Note that changes in the inspected code, or installation of new
@@ -26,7 +26,7 @@ Naming/FileName:
2626
- 'Rakefile.rb'
2727
- 'lib/grape-roar.rb'
2828

29-
# Offense count: 6
29+
# Offense count: 9
3030
# Configuration parameters: NamePrefix, ForbiddenPrefixes, AllowedMethods, MethodDefinitionMacros, UseSorbetSigs.
3131
# NamePrefix: is_, has_, have_, does_
3232
# ForbiddenPrefixes: is_, has_, have_, does_
@@ -36,9 +36,121 @@ Naming/PredicatePrefix:
3636
Exclude:
3737
- 'spec/**/*'
3838
- 'lib/grape/roar/extensions/relations/validations/active_record.rb'
39-
- 'lib/grape/roar/extensions/relations/validations/mongoid.rb'
39+
- 'lib/grape/roar/extensions/relations/validations/mongoid/6.rb'
40+
- 'lib/grape/roar/extensions/relations/validations/mongoid/7.rb'
4041

41-
# Offense count: 14
42+
# Offense count: 8
43+
# This cop supports unsafe autocorrection (--autocorrect-all).
44+
RSpec/BeEql:
45+
Exclude:
46+
- 'spec/extensions/relations/adapters/active_record_spec.rb'
47+
- 'spec/extensions/relations/adapters/mongoid_spec.rb'
48+
- 'spec/extensions/relations/validations/active_record_spec.rb'
49+
- 'spec/extensions/relations/validations/mongoid_spec.rb'
50+
- 'spec/relations_spec.rb'
51+
52+
# Offense count: 11
53+
# Configuration parameters: Prefixes, AllowedPatterns.
54+
# Prefixes: when, with, without
55+
RSpec/ContextWording:
56+
Exclude:
57+
- 'spec/decorator_spec.rb'
58+
- 'spec/extensions/relations/validations/mongoid_spec.rb'
59+
- 'spec/nested_representer_spec.rb'
60+
- 'spec/present_with_spec.rb'
61+
- 'spec/relations_spec.rb'
62+
- 'spec/representer_spec.rb'
63+
- 'spec/support/all/grape_app_context.rb'
64+
65+
# Offense count: 4
66+
# Configuration parameters: CountAsOne.
67+
RSpec/ExampleLength:
68+
Max: 14
69+
70+
# Offense count: 3
71+
RSpec/ExpectInHook:
72+
Exclude:
73+
- 'spec/extensions/relations/dsl_methods_spec.rb'
74+
- 'spec/extensions/relations/validations/active_record_spec.rb'
75+
- 'spec/extensions/relations/validations/mongoid_spec.rb'
76+
77+
# Offense count: 1
78+
RSpec/MessageChain:
79+
Exclude:
80+
- 'spec/extensions/relations/dsl_methods_spec.rb'
81+
82+
# Offense count: 18
83+
# Configuration parameters: .
84+
# SupportedStyles: have_received, receive
85+
RSpec/MessageSpies:
86+
EnforcedStyle: receive
87+
88+
# Offense count: 10
89+
RSpec/MultipleExpectations:
90+
Max: 5
91+
92+
# Offense count: 58
93+
# Configuration parameters: EnforcedStyle, IgnoreSharedExamples.
94+
# SupportedStyles: always, named_only
95+
RSpec/NamedSubject:
96+
Exclude:
97+
- 'spec/decorator_spec.rb'
98+
- 'spec/extensions/relations/adapters/active_record_spec.rb'
99+
- 'spec/extensions/relations/adapters/mongoid_spec.rb'
100+
- 'spec/extensions/relations/dsl_methods_spec.rb'
101+
- 'spec/extensions/relations/mapper_spec.rb'
102+
- 'spec/extensions/relations/validations/active_record_spec.rb'
103+
- 'spec/extensions/relations/validations/mongoid_spec.rb'
104+
- 'spec/nested_representer_spec.rb'
105+
- 'spec/present_with_spec.rb'
106+
- 'spec/relations_spec.rb'
107+
- 'spec/representer_spec.rb'
108+
- 'spec/support/all/grape_app_context.rb'
109+
110+
# Offense count: 2
111+
# Configuration parameters: AllowedGroups.
112+
RSpec/NestedGroups:
113+
Max: 4
114+
115+
# Offense count: 2
116+
# This cop supports unsafe autocorrection (--autocorrect-all).
117+
RSpec/ReceiveMessages:
118+
Exclude:
119+
- 'spec/extensions/relations/mapper_spec.rb'
120+
121+
# Offense count: 8
122+
# Configuration parameters: CustomTransform, IgnoreMethods, IgnoreMetadata.
123+
RSpec/SpecFilePathFormat:
124+
Exclude:
125+
- '**/spec/routing/**/*'
126+
- 'spec/decorator_spec.rb'
127+
- 'spec/extensions/relations/adapters/adapters_module_spec.rb'
128+
- 'spec/extensions/relations/dsl_methods_spec.rb'
129+
- 'spec/extensions/relations/mapper_spec.rb'
130+
- 'spec/nested_representer_spec.rb'
131+
- 'spec/present_with_spec.rb'
132+
- 'spec/relations_spec.rb'
133+
- 'spec/representer_spec.rb'
134+
135+
# Offense count: 3
136+
RSpec/StubbedMock:
137+
Exclude:
138+
- 'spec/extensions/relations/dsl_methods_spec.rb'
139+
- 'spec/extensions/relations/mapper_spec.rb'
140+
141+
# Offense count: 11
142+
RSpec/SubjectStub:
143+
Exclude:
144+
- 'spec/extensions/relations/dsl_methods_spec.rb'
145+
- 'spec/extensions/relations/mapper_spec.rb'
146+
147+
# Offense count: 1
148+
# This cop supports unsafe autocorrection (--autocorrect-all).
149+
RSpec/VerifiedDoubleReference:
150+
Exclude:
151+
- 'spec/extensions/relations/mapper_spec.rb'
152+
153+
# Offense count: 15
42154
# Configuration parameters: AllowedConstants.
43155
Style/Documentation:
44156
Exclude:
@@ -54,13 +166,26 @@ Style/Documentation:
54166
- 'lib/grape/roar/extensions/relations/mapper.rb'
55167
- 'lib/grape/roar/extensions/relations/validations/active_record.rb'
56168
- 'lib/grape/roar/extensions/relations/validations/misc.rb'
57-
- 'lib/grape/roar/extensions/relations/validations/mongoid.rb'
169+
- 'lib/grape/roar/extensions/relations/validations/mongoid/6.rb'
170+
- 'lib/grape/roar/extensions/relations/validations/mongoid/7.rb'
58171
- 'lib/grape/roar/formatter.rb'
59172
- 'lib/grape/roar/representer.rb'
60173

174+
# Offense count: 2
175+
Style/OpenStructUse:
176+
Exclude:
177+
- 'spec/extensions/relations/dsl_methods_spec.rb'
178+
61179
# Offense count: 1
62180
# Configuration parameters: AllowedMethods.
63181
# AllowedMethods: respond_to_missing?
64182
Style/OptionalBooleanParameter:
65183
Exclude:
66184
- 'lib/grape/roar/extensions/relations/dsl_methods.rb'
185+
186+
# Offense count: 2
187+
# This cop supports safe autocorrection (--autocorrect).
188+
# Configuration parameters: AllowHeredoc, AllowURI, AllowQualifiedName, URISchemes, IgnoreCopDirectives, AllowedPatterns, SplitStrings.
189+
# URISchemes: http, https
190+
Layout/LineLength:
191+
Max: 304

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
### 0.5.0 (Next)
22

3+
* [#31](https://github.com/ruby-grape/grape-roar/pull/31): Add support for Mongoid 7 - [@dblock](https://github.com/dblock).
34
* [#23](https://github.com/ruby-grape/grape-roar/pull/23): Resolves pollution issue with invoking representers on singletons - [@mach-kernel](https://github.com/mach-kernel).
45
* [#28](https://github.com/ruby-grape/grape-roar/pull/28): Replaced Travis-CI with GHA - [@dblock](https://github.com/dblock).
56
* Your contribution here.

Gemfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
source 'https://rubygems.org'
44

5-
gemspec
6-
75
gem 'activerecord', ENV['ACTIVERECORD_VERSION'], require: 'active_record' if ENV.key?('ACTIVERECORD_VERSION')
86
gem 'grape', ENV['GRAPE_VERSION'] if ENV.key?('GRAPE_VERSION')
97
gem 'mongoid', ENV['MONGOID_VERSION'], require: 'mongoid' if ENV.key?('MONGOID_VERSION')
108

9+
gemspec
10+
1111
group :test do
1212
gem 'rack-test'
1313
gem 'rspec'

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -246,11 +246,11 @@ Although this example uses `Grape::Roar::Decorator`, you can also use a module a
246246

247247
#### Errors
248248

249-
Should you incorrectly describe a relationship (e.g. you specify has_one but your model specifies has_many), an exception will be raised to notify you of the mismatch:
249+
Should you incorrectly describe a relationship (e.g. you specify `has_one` but your model specifies `has_many`), an exception will be raised to notify you of the mismatch:
250250

251251
```ruby
252252
Grape::Roar::Extensions::Relations::Exceptions::InvalidRelationError:
253-
Expected Mongoid::Relations::Referenced::One, got Mongoid::Relations::Referenced::Many!
253+
Expected Mongoid::Association::Referenced::HasOne, got Mongoid::Association::Referenced::HasMany!
254254
```
255255

256256
#### Change how URLs are presented
@@ -308,7 +308,7 @@ module Extensions
308308
include Validations::ActiveRecord
309309

310310
# We map your domain object to the correct adapter
311-
# during runtime.
311+
# at runtime.
312312
valid_for { |klass| klass < ::ActiveRecord::Base }
313313

314314
def collection_methods

lib/grape/roar/extensions/relations/adapters.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# frozen_string_literal: true
22

33
require 'grape/roar/extensions/relations/adapters/base'
4-
require 'grape/roar/extensions/relations/adapters/active_record'
5-
require 'grape/roar/extensions/relations/adapters/mongoid'
4+
require 'grape/roar/extensions/relations/adapters/active_record' if defined?(ActiveRecord)
5+
require 'grape/roar/extensions/relations/adapters/mongoid' if defined?(Mongoid)
66

77
module Grape
88
module Roar

lib/grape/roar/extensions/relations/adapters/mongoid.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def collection_methods
1818

1919
def name_for_represented(represented)
2020
klass_name = if represented.instance_of?(
21-
::Mongoid::Relations::Targets::Enumerable
21+
::Enumerable
2222
)
2323
represented.klass.name
2424
else
Lines changed: 5 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,9 @@
11
# frozen_string_literal: true
22

3-
module Grape
4-
module Roar
5-
module Extensions
6-
module Relations
7-
module Validations
8-
module Mongoid
9-
include Validations::Misc
10-
11-
def belongs_to_valid?(relation)
12-
relation = klass.reflect_on_association(relation)
13-
14-
return true if relation[:relation] ==
15-
::Mongoid::Relations::Referenced::In
16-
17-
invalid_relation(
18-
::Mongoid::Relations::Referenced::In, relation[:relation]
19-
)
20-
end
21-
22-
def embeds_many_valid?(relation)
23-
relation = klass.reflect_on_association(relation)
24-
25-
return true if relation[:relation] ==
26-
::Mongoid::Relations::Embedded::Many
27-
28-
invalid_relation(
29-
::Mongoid::Relations::Embedded::Many, relation[:relation]
30-
)
31-
end
32-
33-
def embeds_one_valid?(relation)
34-
relation = klass.reflect_on_association(relation)
35-
36-
return true if relation[:relation] ==
37-
::Mongoid::Relations::Embedded::One
38-
39-
invalid_relation(
40-
::Mongoid::Relations::Embedded::One, relation[:relation]
41-
)
42-
end
43-
44-
def has_many_valid?(relation)
45-
relation = klass.reflect_on_association(relation)
46-
47-
return true if relation[:relation] ==
48-
::Mongoid::Relations::Referenced::Many
49-
50-
invalid_relation(
51-
::Mongoid::Relations::Referenced::Many, relation[:relation]
52-
)
53-
end
54-
55-
def has_and_belongs_to_many_valid?(relation)
56-
relation = klass.reflect_on_association(relation)
57-
58-
return true if relation[:relation] ==
59-
::Mongoid::Relations::Referenced::ManyToMany
60-
61-
invalid_relation(
62-
::Mongoid::Relations::Referenced::ManyToMany,
63-
relation[:relation]
64-
)
65-
end
66-
67-
def has_one_valid?(relation)
68-
relation = klass.reflect_on_association(relation)
69-
70-
return true if relation[:relation] ==
71-
::Mongoid::Relations::Referenced::One
72-
73-
invalid_relation(
74-
::Mongoid::Relations::Referenced::One, relation[:relation]
75-
)
76-
end
77-
end
78-
end
79-
end
80-
end
3+
if defined?(Mongoid)
4+
if Gem::Version.new(Mongoid::VERSION) < Gem::Version.new('7')
5+
require_relative 'mongoid/6'
6+
else
7+
require_relative 'mongoid/7'
818
end
829
end

0 commit comments

Comments
 (0)