Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/test-activerecord.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ jobs:
strategy:
matrix:
entry:
- { ruby: '3.4', postgresql: '17', activerecord: '~> 8.0.2', grape: '2.4.0' }
- { ruby: '3.4', postgresql: '15', activerecord: '~> 6.1.0', grape: '~> 1.8.0' }
- { ruby: '3.4', postgresql: '16', activerecord: '~> 7.2.0', grape: '~> 2.4.0' }
- { ruby: '3.4', postgresql: '17', activerecord: '~> 8.0.3', grape: '~> 2.4.0' }
name: test (ruby=${{ matrix.entry.ruby }}, postgresql=${{ matrix.entry.postgresql }}, activerecord=${{ matrix.entry.activerecord }}, grape=${{ matrix.entry.grape }})
env:
ACTIVERECORD_VERSION: ${{ matrix.entry.activerecord }}
Expand Down
6 changes: 5 additions & 1 deletion .github/workflows/test-mongodb.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ jobs:
strategy:
matrix:
entry:
- { ruby: '3.4', mongoid: '6.4.8', mongodb: '6.0', grape: '1.7.0' }
- { ruby: '3.4', mongoid: '~> 6.4.8', mongodb: '6.0', grape: '~> 1.7.0' }
- { ruby: '3.4', mongoid: '~> 6.4.8', mongodb: '6.0', grape: '~> 2.0.0' }
- { ruby: '3.4', mongoid: '~> 7.5.4', mongodb: '7.0', grape: '~> 2.4.0' }
- { ruby: '3.4', mongoid: '~> 8.1.11', mongodb: '8.0', grape: '~> 2.4.0' }
- { ruby: '3.4', mongoid: '~> 9.0.8', mongodb: '8.0', grape: '~> 2.4.0' }
name: test (ruby=${{ matrix.entry.ruby }}, mongoid=${{ matrix.entry.mongoid }}, mongodb=${{ matrix.entry.mongodb }}, grape=${{ matrix.entry.grape }})
env:
MONGOID_VERSION: ${{ matrix.entry.mongoid }}
Expand Down
3 changes: 0 additions & 3 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ AllCops:
TargetRubyVersion: 3.0
NewCops: enable
Exclude:
- bin/**/*
- gemfiles/**/*
- spec/**/*
- vendor/**/*

inherit_from: .rubocop_todo.yml
Expand Down
135 changes: 130 additions & 5 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2025-09-24 16:57:49 UTC using RuboCop version 1.80.2.
# on 2025-09-25 02:19:21 UTC using RuboCop version 1.80.2.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
Expand All @@ -26,7 +26,7 @@ Naming/FileName:
- 'Rakefile.rb'
- 'lib/grape-roar.rb'

# Offense count: 6
# Offense count: 9
# Configuration parameters: NamePrefix, ForbiddenPrefixes, AllowedMethods, MethodDefinitionMacros, UseSorbetSigs.
# NamePrefix: is_, has_, have_, does_
# ForbiddenPrefixes: is_, has_, have_, does_
Expand All @@ -36,9 +36,121 @@ Naming/PredicatePrefix:
Exclude:
- 'spec/**/*'
- 'lib/grape/roar/extensions/relations/validations/active_record.rb'
- 'lib/grape/roar/extensions/relations/validations/mongoid.rb'
- 'lib/grape/roar/extensions/relations/validations/mongoid/6.rb'
- 'lib/grape/roar/extensions/relations/validations/mongoid/7.rb'

# Offense count: 14
# Offense count: 8
# This cop supports unsafe autocorrection (--autocorrect-all).
RSpec/BeEql:
Exclude:
- 'spec/extensions/relations/adapters/active_record_spec.rb'
- 'spec/extensions/relations/adapters/mongoid_spec.rb'
- 'spec/extensions/relations/validations/active_record_spec.rb'
- 'spec/extensions/relations/validations/mongoid_spec.rb'
- 'spec/relations_spec.rb'

# Offense count: 11
# Configuration parameters: Prefixes, AllowedPatterns.
# Prefixes: when, with, without
RSpec/ContextWording:
Exclude:
- 'spec/decorator_spec.rb'
- 'spec/extensions/relations/validations/mongoid_spec.rb'
- 'spec/nested_representer_spec.rb'
- 'spec/present_with_spec.rb'
- 'spec/relations_spec.rb'
- 'spec/representer_spec.rb'
- 'spec/support/all/grape_app_context.rb'

# Offense count: 4
# Configuration parameters: CountAsOne.
RSpec/ExampleLength:
Max: 14

# Offense count: 3
RSpec/ExpectInHook:
Exclude:
- 'spec/extensions/relations/dsl_methods_spec.rb'
- 'spec/extensions/relations/validations/active_record_spec.rb'
- 'spec/extensions/relations/validations/mongoid_spec.rb'

# Offense count: 1
RSpec/MessageChain:
Exclude:
- 'spec/extensions/relations/dsl_methods_spec.rb'

# Offense count: 18
# Configuration parameters: .
# SupportedStyles: have_received, receive
RSpec/MessageSpies:
EnforcedStyle: receive

# Offense count: 10
RSpec/MultipleExpectations:
Max: 5

# Offense count: 58
# Configuration parameters: EnforcedStyle, IgnoreSharedExamples.
# SupportedStyles: always, named_only
RSpec/NamedSubject:
Exclude:
- 'spec/decorator_spec.rb'
- 'spec/extensions/relations/adapters/active_record_spec.rb'
- 'spec/extensions/relations/adapters/mongoid_spec.rb'
- 'spec/extensions/relations/dsl_methods_spec.rb'
- 'spec/extensions/relations/mapper_spec.rb'
- 'spec/extensions/relations/validations/active_record_spec.rb'
- 'spec/extensions/relations/validations/mongoid_spec.rb'
- 'spec/nested_representer_spec.rb'
- 'spec/present_with_spec.rb'
- 'spec/relations_spec.rb'
- 'spec/representer_spec.rb'
- 'spec/support/all/grape_app_context.rb'

# Offense count: 2
# Configuration parameters: AllowedGroups.
RSpec/NestedGroups:
Max: 4

# Offense count: 2
# This cop supports unsafe autocorrection (--autocorrect-all).
RSpec/ReceiveMessages:
Exclude:
- 'spec/extensions/relations/mapper_spec.rb'

# Offense count: 8
# Configuration parameters: CustomTransform, IgnoreMethods, IgnoreMetadata.
RSpec/SpecFilePathFormat:
Exclude:
- '**/spec/routing/**/*'
- 'spec/decorator_spec.rb'
- 'spec/extensions/relations/adapters/adapters_module_spec.rb'
- 'spec/extensions/relations/dsl_methods_spec.rb'
- 'spec/extensions/relations/mapper_spec.rb'
- 'spec/nested_representer_spec.rb'
- 'spec/present_with_spec.rb'
- 'spec/relations_spec.rb'
- 'spec/representer_spec.rb'

# Offense count: 3
RSpec/StubbedMock:
Exclude:
- 'spec/extensions/relations/dsl_methods_spec.rb'
- 'spec/extensions/relations/mapper_spec.rb'

# Offense count: 11
RSpec/SubjectStub:
Exclude:
- 'spec/extensions/relations/dsl_methods_spec.rb'
- 'spec/extensions/relations/mapper_spec.rb'

# Offense count: 1
# This cop supports unsafe autocorrection (--autocorrect-all).
RSpec/VerifiedDoubleReference:
Exclude:
- 'spec/extensions/relations/mapper_spec.rb'

# Offense count: 15
# Configuration parameters: AllowedConstants.
Style/Documentation:
Exclude:
Expand All @@ -54,13 +166,26 @@ Style/Documentation:
- 'lib/grape/roar/extensions/relations/mapper.rb'
- 'lib/grape/roar/extensions/relations/validations/active_record.rb'
- 'lib/grape/roar/extensions/relations/validations/misc.rb'
- 'lib/grape/roar/extensions/relations/validations/mongoid.rb'
- 'lib/grape/roar/extensions/relations/validations/mongoid/6.rb'
- 'lib/grape/roar/extensions/relations/validations/mongoid/7.rb'
- 'lib/grape/roar/formatter.rb'
- 'lib/grape/roar/representer.rb'

# Offense count: 2
Style/OpenStructUse:
Exclude:
- 'spec/extensions/relations/dsl_methods_spec.rb'

# Offense count: 1
# Configuration parameters: AllowedMethods.
# AllowedMethods: respond_to_missing?
Style/OptionalBooleanParameter:
Exclude:
- 'lib/grape/roar/extensions/relations/dsl_methods.rb'

# Offense count: 2
# This cop supports safe autocorrection (--autocorrect).
# Configuration parameters: AllowHeredoc, AllowURI, AllowQualifiedName, URISchemes, IgnoreCopDirectives, AllowedPatterns, SplitStrings.
# URISchemes: http, https
Layout/LineLength:
Max: 304
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
### 0.5.0 (Next)

* [#31](https://github.com/ruby-grape/grape-roar/pull/31): Add support for Mongoid 7, 8 and 9 - [@dblock](https://github.com/dblock).
* [#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).
* [#28](https://github.com/ruby-grape/grape-roar/pull/28): Replaced Travis-CI with GHA - [@dblock](https://github.com/dblock).
* Your contribution here.
Expand Down
4 changes: 2 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

source 'https://rubygems.org'

gemspec

gem 'activerecord', ENV['ACTIVERECORD_VERSION'], require: 'active_record' if ENV.key?('ACTIVERECORD_VERSION')
gem 'grape', ENV['GRAPE_VERSION'] if ENV.key?('GRAPE_VERSION')
gem 'mongoid', ENV['MONGOID_VERSION'], require: 'mongoid' if ENV.key?('MONGOID_VERSION')

gemspec

group :test do
gem 'rack-test'
gem 'rspec'
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,11 +246,11 @@ Although this example uses `Grape::Roar::Decorator`, you can also use a module a

#### Errors

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:
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:

```ruby
Grape::Roar::Extensions::Relations::Exceptions::InvalidRelationError:
Expected Mongoid::Relations::Referenced::One, got Mongoid::Relations::Referenced::Many!
Expected Mongoid::Association::Referenced::HasOne, got Mongoid::Association::Referenced::HasMany!
```

#### Change how URLs are presented
Expand Down Expand Up @@ -308,7 +308,7 @@ module Extensions
include Validations::ActiveRecord

# We map your domain object to the correct adapter
# during runtime.
# at runtime.
valid_for { |klass| klass < ::ActiveRecord::Base }

def collection_methods
Expand Down
4 changes: 2 additions & 2 deletions lib/grape/roar/extensions/relations/adapters.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# frozen_string_literal: true

require 'grape/roar/extensions/relations/adapters/base'
require 'grape/roar/extensions/relations/adapters/active_record'
require 'grape/roar/extensions/relations/adapters/mongoid'
require 'grape/roar/extensions/relations/adapters/active_record' if defined?(ActiveRecord)
require 'grape/roar/extensions/relations/adapters/mongoid' if defined?(Mongoid)

module Grape
module Roar
Expand Down
2 changes: 1 addition & 1 deletion lib/grape/roar/extensions/relations/adapters/mongoid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def collection_methods

def name_for_represented(represented)
klass_name = if represented.instance_of?(
::Mongoid::Relations::Targets::Enumerable
::Enumerable
)
represented.klass.name
else
Expand Down
49 changes: 11 additions & 38 deletions lib/grape/roar/extensions/relations/validations/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,55 +9,28 @@ module ActiveRecord
include Validations::Misc

def belongs_to_valid?(relation)
relation = klass.reflections[relation]

return true if relation.is_a?(
::ActiveRecord::Reflection::BelongsToReflection
)

invalid_relation(
::ActiveRecord::Reflection::BelongsToReflection,
relation.class
)
_valid_relation? relation, ::ActiveRecord::Reflection::BelongsToReflection
end

def has_many_valid?(relation)
relation = klass.reflections[relation]

return true if relation.is_a?(
::ActiveRecord::Reflection::HasManyReflection
)

invalid_relation(
::ActiveRecord::Reflection::HasManyReflection,
relation.class
)
_valid_relation? relation, ::ActiveRecord::Reflection::HasManyReflection
end

def has_and_belongs_to_many_valid?(relation)
relation = klass.reflections[relation]

return true if relation.is_a?(
::ActiveRecord::Reflection::HasAndBelongsToManyReflection
)

invalid_relation(
::ActiveRecord::Reflection::HasAndBelongsToManyReflection,
relation.class
)
_valid_relation? relation, ::ActiveRecord::Reflection::HasAndBelongsToManyReflection
end

def has_one_valid?(relation)
relation = klass.reflections[relation]
_valid_relation? relation, ::ActiveRecord::Reflection::HasOneReflection
end

private

return true if relation.is_a?(
::ActiveRecord::Reflection::HasOneReflection
)
def _valid_relation?(relation, relation_klass)
relation = klass.reflections[relation]
return true if relation.is_a?(relation_klass)

invalid_relation(
::ActiveRecord::Reflection::HasOneReflection,
relation.class
)
invalid_relation(relation_klass, relation.class)
end
end
end
Expand Down
Loading