Skip to content

Commit 0d31b3c

Browse files
authored
enable rubocop on some files and address the resulting complaints (#5664)
1 parent 7c1e6ac commit 0d31b3c

File tree

4 files changed

+157
-94
lines changed

4 files changed

+157
-94
lines changed

lib/mongoid/atomic.rb

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,25 @@
11
# frozen_string_literal: true
2-
# rubocop:todo all
32

4-
require "mongoid/atomic/modifiers"
5-
require "mongoid/atomic/paths"
3+
require 'mongoid/atomic/modifiers'
4+
require 'mongoid/atomic/paths'
65

76
module Mongoid
8-
97
# This module contains the logic for supporting atomic operations against the
108
# database.
119
module Atomic
1210
extend ActiveSupport::Concern
1311

14-
UPDATES = [
15-
:atomic_array_pushes,
16-
:atomic_array_pulls,
17-
:atomic_array_add_to_sets,
18-
:atomic_pulls,
19-
:delayed_atomic_sets,
20-
:delayed_atomic_pulls,
21-
:delayed_atomic_unsets
22-
]
12+
UPDATES = %i[
13+
atomic_array_pushes
14+
atomic_array_pulls
15+
atomic_array_add_to_sets
16+
atomic_pulls
17+
delayed_atomic_sets
18+
delayed_atomic_pulls
19+
delayed_atomic_unsets
20+
].freeze
2321

2422
included do
25-
2623
# When MongoDB finally fully implements the positional operator, we can
2724
# get rid of all indexing related code in Mongoid.
2825
attr_accessor :_index
@@ -117,6 +114,8 @@ def atomic_array_add_to_sets
117114
# person.atomic_updates(children)
118115
#
119116
# @return [ Hash ] The updates and their modifiers.
117+
#
118+
# rubocop:disable Style/OptionalBooleanParameter
120119
def atomic_updates(_use_indexes = false)
121120
process_flagged_destroys
122121
mods = Modifiers.new
@@ -127,7 +126,8 @@ def atomic_updates(_use_indexes = false)
127126
end
128127
mods
129128
end
130-
alias :_updates :atomic_updates
129+
alias _updates atomic_updates
130+
# rubocop:enable Style/OptionalBooleanParameter
131131

132132
# Get the removal modifier for the document. Will be nil on root
133133
# documents, $unset on embeds_one, $set on embeds_many.
@@ -179,13 +179,11 @@ def atomic_position
179179
#
180180
# @return [ Object ] The associated path.
181181
def atomic_paths
182-
@atomic_paths ||= begin
183-
if _association
184-
_association.path(self)
185-
else
186-
Atomic::Paths::Root.new(self)
187-
end
188-
end
182+
@atomic_paths ||= if _association
183+
_association.path(self)
184+
else
185+
Atomic::Paths::Root.new(self)
186+
end
189187
end
190188

191189
# Get all the attributes that need to be pulled.
@@ -202,7 +200,7 @@ def atomic_pulls
202200
path ||= doc.flag_as_destroyed
203201
doc._id
204202
end
205-
pulls[path] = { "_id" => { "$in" => ids }} and path = nil
203+
pulls[path] = { '_id' => { '$in' => ids } } and path = nil
206204
end
207205
pulls
208206
end
@@ -224,7 +222,13 @@ def atomic_pushes
224222
#
225223
# @return [ Hash ] The $set operations.
226224
def atomic_sets
227-
updateable? ? setters : settable? ? { atomic_path => as_attributes } : {}
225+
if updateable?
226+
setters
227+
elsif settable?
228+
{ atomic_path => as_attributes }
229+
else
230+
{}
231+
end
228232
end
229233

230234
# Get all the attributes that need to be unset.

lib/mongoid/changeable.rb

Lines changed: 58 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# frozen_string_literal: true
2-
# rubocop:todo all
32

43
module Mongoid
5-
64
# Defines behavior for dirty tracking.
75
module Changeable
86
extend ActiveSupport::Concern
@@ -53,12 +51,10 @@ def changed_attributes
5351
#
5452
# @return [ Hash<String, Array<Object, Object> ] The changes.
5553
def changes
56-
_changes = {}
57-
changed.each do |attr|
54+
changed.each_with_object({}) do |attr, changes|
5855
change = attribute_change(attr)
59-
_changes[attr] = change if change
60-
end
61-
_changes.with_indifferent_access
56+
changes[attr] = change if change
57+
end.with_indifferent_access
6258
end
6359

6460
# Call this method after save, so the changes can be properly switched.
@@ -120,15 +116,15 @@ def remove_change(name)
120116
def setters
121117
mods = {}
122118
changes.each_pair do |name, changes|
123-
if changes
124-
old, new = changes
125-
field = fields[name]
126-
key = atomic_attribute_name(name)
127-
if field && field.resizable?
128-
field.add_atomic_changes(self, name, key, mods, new, old)
129-
else
130-
mods[key] = new unless atomic_unsets.include?(key)
131-
end
119+
next unless changes
120+
121+
old, new = changes
122+
field = fields[name]
123+
key = atomic_attribute_name(name)
124+
if field&.resizable?
125+
field.add_atomic_changes(self, name, key, mods, new, old)
126+
else
127+
mods[key] = new unless atomic_unsets.include?(key)
132128
end
133129
end
134130
mods
@@ -164,24 +160,19 @@ def saved_change_to_attribute(attr)
164160
# in an attribute during the save that triggered the callbacks to run.
165161
#
166162
# @param [ String ] attr The name of the attribute.
167-
# @param **kwargs The optional keyword arguments.
168-
#
169-
# @option **kwargs [ Object ] :from The object the attribute was changed from.
170-
# @option **kwargs [ Object ] :to The object the attribute was changed to.
163+
# @param [ Object ] from The object the attribute was changed from (optional).
164+
# @param [ Object ] to The object the attribute was changed to (optional).
171165
#
172166
# @return [ true | false ] Whether the attribute has changed during the last save.
173-
def saved_change_to_attribute?(attr, **kwargs)
167+
def saved_change_to_attribute?(attr, from: Utils::PLACEHOLDER, to: Utils::PLACEHOLDER)
174168
changes = saved_change_to_attribute(attr)
175169
return false unless changes.is_a?(Array)
176-
if kwargs.key?(:from) && kwargs.key?(:to)
177-
changes.first == kwargs[:from] && changes.last == kwargs[:to]
178-
elsif kwargs.key?(:from)
179-
changes.first == kwargs[:from]
180-
elsif kwargs.key?(:to)
181-
changes.last == kwargs[:to]
182-
else
183-
true
184-
end
170+
171+
return true if Utils.placeholder?(from) && Utils.placeholder?(to)
172+
return changes.first == from if Utils.placeholder?(to)
173+
return changes.last == to if Utils.placeholder?(from)
174+
175+
changes.first == from && changes.last == to
185176
end
186177

187178
# Returns whether this attribute change the next time we save.
@@ -227,31 +218,48 @@ def attributes_before_last_save
227218
# @return [ Array<Object> ] The old and new values.
228219
def attribute_change(attr)
229220
attr = database_field_name(attr)
230-
[changed_attributes[attr], attributes[attr]] if attribute_changed?(attr)
221+
[ changed_attributes[attr], attributes[attr] ] if attribute_changed?(attr)
222+
end
223+
224+
# A class for representing the default value that an attribute was changed
225+
# from or to.
226+
#
227+
# @api private
228+
class Anything
229+
# `Anything` objects are always equal to everything. This simplifies
230+
# the logic for asking whether an attribute has changed or not. If the
231+
# `from` or `to` value is a `Anything` (because it was not
232+
# explicitly given), any comparison with it will suggest the value has
233+
# not changed.
234+
#
235+
# @param [ Object ] _other The object being compared with this object.
236+
#
237+
# @return [ true ] Always returns true.
238+
def ==(_other)
239+
true
240+
end
231241
end
232242

243+
# a singleton object to represent an optional `to` or `from` value
244+
# that was not explicitly provided to #attribute_changed?
245+
ATTRIBUTE_UNCHANGED = Anything.new
246+
233247
# Determine if a specific attribute has changed.
234248
#
235249
# @example Has the attribute changed?
236250
# model.attribute_changed?("name")
237251
#
238252
# @param [ String ] attr The name of the attribute.
239-
# @param **kwargs The optional keyword arguments.
240-
#
241-
# @option **kwargs [ Object ] :from The object the attribute was changed from.
242-
# @option **kwargs [ Object ] :to The object the attribute was changed to.
253+
# @param [ Object ] from The object the attribute was changed from (optional).
254+
# @param [ Object ] to The object the attribute was changed to (optional).
243255
#
244256
# @return [ true | false ] Whether the attribute has changed.
245-
def attribute_changed?(attr, **kwargs)
257+
def attribute_changed?(attr, from: ATTRIBUTE_UNCHANGED, to: ATTRIBUTE_UNCHANGED)
246258
attr = database_field_name(attr)
247259
return false unless changed_attributes.key?(attr)
248260
return false if changed_attributes[attr] == attributes[attr]
249-
if kwargs.key?(:from)
250-
return false if changed_attributes[attr] != kwargs[:from]
251-
end
252-
if kwargs.key?(:to)
253-
return false if attributes[attr] != kwargs[:to]
254-
end
261+
return false if from != changed_attributes[attr]
262+
return false if to != attributes[attr]
255263

256264
true
257265
end
@@ -265,8 +273,8 @@ def attribute_changed?(attr, **kwargs)
265273
#
266274
# @return [ true | false ] If the attribute differs.
267275
def attribute_changed_from_default?(attr)
268-
field = fields[attr]
269-
return false unless field
276+
return false unless (field = fields[attr])
277+
270278
attributes[attr] != field.eval_default(self)
271279
end
272280

@@ -309,9 +317,9 @@ def attribute_previously_was(attr)
309317
#
310318
# @return [ Object ] The old value.
311319
def attribute_will_change!(attr)
312-
unless changed_attributes.key?(attr)
313-
changed_attributes[attr] = read_raw_attribute(attr).__deep_copy__
314-
end
320+
return if changed_attributes.key?(attr)
321+
322+
changed_attributes[attr] = read_raw_attribute(attr).__deep_copy__
315323
end
316324

317325
# Set the attribute back to its old value.
@@ -329,7 +337,7 @@ def reset_attribute!(attr)
329337

330338
def reset_attribute_to_default!(attr)
331339
attr = database_field_name(attr)
332-
if field = fields[attr]
340+
if (field = fields[attr])
333341
__send__("#{attr}=", field.eval_default(self))
334342
else
335343
__send__("#{attr}=", nil)
@@ -340,8 +348,8 @@ def reset_attributes_before_type_cast
340348
@attributes_before_type_cast = @attributes.dup
341349
end
342350

351+
# Class-level methods for changeable objects.
343352
module ClassMethods
344-
345353
private
346354

347355
# Generate all the dirty methods needed for the attribute.
@@ -495,7 +503,7 @@ def create_dirty_reset_to_default(name, meth)
495503
def create_dirty_previously_changed?(name, meth)
496504
generated_methods.module_eval do
497505
re_define_method("#{meth}_previously_changed?") do
498-
previous_changes.keys.include?(name)
506+
previous_changes.key?(name)
499507
end
500508
end
501509
end

0 commit comments

Comments
 (0)