Skip to content

Commit 138f1a2

Browse files
authored
Revert introduction of forwardable (#107)
* Revert introduction of `forwardable` Mongoid 7.1.0 introduced a regression (see https://jira.mongodb.org/browse/MONGOID-4849) by switching to the builtin ruby forwardable method of delegation rather than relying on the ActiveSupport-provided delegation used before. mongoid-locker was made compatible with the regressed version of mongoid in mongodb/mongoid#4739 Mongoid then fixed this regression in 7.1.1+ with mongodb/mongoid@d078acd, which goes back to the active support way of delegating. The `forwardable` inclusion in mongoid-locker causes incompatibilities when you then include another gem that expects to be able to set up a delegator within a mongoid document class. For example the `mongoid-history` gem expects to be able to delegate using the active-support style here: https://github.com/mongoid/mongoid-history/blob/c8c4de1235c01252dfbdb9fe6c0abfc078ec1443/lib/mongoid/history/trackable.rb#L24-L25 This PR reverts the change to introduce `forwardable`. See mongoid/mongoid-history#238 (comment) for an example of how this interacts poorly with mongoid-history. * Add changelog entry, bump next to minor version * Add an integration spec showing forwardable regression
1 parent 0795445 commit 138f1a2

6 files changed

Lines changed: 31 additions & 6 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
## Changelog
22

3-
### 2.1.1 (Next)
3+
### 2.2.0 (Next)
44

55
* Your contribution here.
6+
* [#107](https://github.com/mongoid/mongoid-locker/pull/107): Revert introduction of `forwardable` to ensure compatibility with mongoid-history - [@scpike](https://github.com/scpike).
67

78
### 2.1.0 (2024-05-07)
89

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ group :development, :test do
2929

3030
gem 'mongoid-compatibility'
3131
gem 'mongoid-danger', '~> 0.2.0'
32+
gem 'mongoid-history'
3233
gem 'rspec', '~> 3.9'
3334
gem 'rubocop', '0.81.0'
3435
gem 'rubocop-rspec', '1.38.1'

lib/mongoid/locker.rb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# frozen_string_literal: true
22

3-
require 'forwardable'
43
require 'securerandom'
54

65
module Mongoid
@@ -104,8 +103,6 @@ def reset!
104103

105104
# @api private
106105
def included(klass)
107-
klass.extend(Forwardable) unless klass.ancestors.include?(Forwardable)
108-
109106
klass.extend ClassMethods
110107
klass.singleton_class.instance_eval { attr_accessor(*MODULE_METHODS) }
111108

@@ -117,7 +114,7 @@ def included(klass)
117114
klass.backoff_algorithm = backoff_algorithm
118115
klass.locking_name_generator = locking_name_generator
119116

120-
klass.def_delegators(klass, *MODULE_METHODS)
117+
klass.delegate(*MODULE_METHODS, to: :class)
121118
klass.singleton_class.delegate(*(methods(false) - MODULE_METHODS.flat_map { |method| [method, "#{method}=".to_sym] } - %i[included reset! configure]), to: self)
122119
end
123120
end

lib/mongoid/locker/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
module Mongoid
44
module Locker
5-
VERSION = '2.1.1'
5+
VERSION = '2.2.0'
66
end
77
end
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# frozen_string_literal: true
2+
3+
require 'mongoid-history'
4+
5+
# This replicates the exception reported at https://github.com/mongoid/mongoid-history/issues/238#issuecomment-1063155193
6+
# when mongoid-locker required 'forwardable' instead of relying on the
7+
# active support-provided delegation method
8+
RSpec.describe 'MongoidHistory' do # rubocop:disable RSpec/DescribeClass
9+
# rubocop:disable RSpec/ExampleLength
10+
it 'does not raise an exception' do
11+
expect do
12+
Class.new do
13+
include Mongoid::Document
14+
include Mongoid::Locker
15+
include Mongoid::History::Trackable
16+
17+
field :title
18+
19+
track_history on: [:title]
20+
end
21+
end.not_to raise_exception
22+
end
23+
# rubocop:enable RSpec/ExampleLength
24+
end

spec/spec_helper.rb

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

33
ENV['RACK_ENV'] = 'test'
44

5+
require 'logger' # Required for compatibility with activesupport 7
6+
57
if ENV['COVERAGE']
68
require 'simplecov'
79
require 'simplecov_json_formatter'

0 commit comments

Comments
 (0)