Skip to content

Commit def73a2

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

File tree

10 files changed

+177
-102
lines changed

10 files changed

+177
-102
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_todo.yml

Lines changed: 7 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-24 22:56:32 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,10 @@ 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: 15
4243
# Configuration parameters: AllowedConstants.
4344
Style/Documentation:
4445
Exclude:
@@ -54,7 +55,8 @@ Style/Documentation:
5455
- 'lib/grape/roar/extensions/relations/mapper.rb'
5556
- 'lib/grape/roar/extensions/relations/validations/active_record.rb'
5657
- 'lib/grape/roar/extensions/relations/validations/misc.rb'
57-
- 'lib/grape/roar/extensions/relations/validations/mongoid.rb'
58+
- 'lib/grape/roar/extensions/relations/validations/mongoid/6.rb'
59+
- 'lib/grape/roar/extensions/relations/validations/mongoid/7.rb'
5860
- 'lib/grape/roar/formatter.rb'
5961
- 'lib/grape/roar/representer.rb'
6062

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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ Should you incorrectly describe a relationship (e.g. you specify has_one but you
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::One, got Mongoid::Association::Referenced::Many!
254254
```
255255

256256
#### Change how URLs are presented

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: 4 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,7 @@
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
81-
end
3+
if Gem::Version.new(Mongoid::VERSION) < Gem::Version.new('7')
4+
require_relative 'mongoid/6'
5+
else
6+
require_relative 'mongoid/7'
827
end
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# frozen_string_literal: true
2+
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
81+
end
82+
end
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# frozen_string_literal: true
2+
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+
_valid_relation?(
13+
relation,
14+
::Mongoid::Association::Referenced::BelongsTo,
15+
::Mongoid::Association::Referenced::BelongsTo::Proxy
16+
)
17+
end
18+
19+
def embeds_many_valid?(relation)
20+
_valid_relation?(
21+
relation,
22+
::Mongoid::Association::Embedded::EmbedsMany,
23+
::Mongoid::Association::Embedded::EmbedsMany::Proxy
24+
)
25+
end
26+
27+
def embeds_one_valid?(relation)
28+
_valid_relation?(
29+
relation,
30+
::Mongoid::Association::Embedded::EmbedsOne,
31+
::Mongoid::Association::Embedded::EmbedsOne::Proxy
32+
)
33+
end
34+
35+
def has_many_valid?(relation)
36+
_valid_relation?(
37+
relation,
38+
::Mongoid::Association::Referenced::HasMany,
39+
::Mongoid::Association::Referenced::HasMany::Proxy
40+
)
41+
end
42+
43+
def has_and_belongs_to_many_valid?(relation)
44+
_valid_relation?(
45+
relation,
46+
::Mongoid::Association::Referenced::HasAndBelongsToMany,
47+
::Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy
48+
)
49+
end
50+
51+
def has_one_valid?(relation)
52+
_valid_relation?(
53+
relation,
54+
::Mongoid::Association::Referenced::HasOne,
55+
::Mongoid::Association::Referenced::HasOne::Proxy
56+
)
57+
end
58+
59+
private
60+
61+
def _valid_relation?(relation, relation_klass, relation_proxy_klass)
62+
relation = klass.reflect_on_association(relation)
63+
64+
related = relation.is_a?(Hash) ? relation[:relation] : relation.relation
65+
66+
return true if related == relation_klass || related == relation_proxy_klass
67+
68+
invalid_relation(relation.class, related)
69+
end
70+
end
71+
end
72+
end
73+
end
74+
end
75+
end

spec/extensions/relations/validations/mongoid_spec.rb

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,12 @@ def initialize(klass)
1515
klass.new(model_klass)
1616
end
1717

18-
let(:map_reflection) do
19-
proc do |r|
20-
case r
21-
when /and/ then 'ManyToMany'
22-
when /many/ then 'Many'
23-
when /one/ then 'One'
24-
when /belongs/ then 'In'
25-
end
26-
end
27-
end
28-
2918
before do
3019
expect(model_klass).to receive(:reflect_on_association).twice do |r|
3120
if r == :test_rel
3221
{
33-
relation: "Mongoid::Relations::#{relation_klass}"\
34-
"::#{map_reflection.call(test_method)}".constantize
22+
relation: "Mongoid::Association::#{relation_klass}"\
23+
"::#{test_method.to_s.camelize}".constantize
3524
}
3625
else
3726
{ relation: double }
@@ -65,7 +54,7 @@ def initialize(klass)
6554
end
6655
end
6756

68-
context 'referenced' do
57+
context 'embedded' do
6958
let(:relation_klass) { 'Embedded' }
7059

7160
%i[embeds_one embeds_many].each do |test_method|

0 commit comments

Comments
 (0)