From f83323094632a3604fcd30f40b8c86358a397fe3 Mon Sep 17 00:00:00 2001 From: Dmitry Rybakov Date: Mon, 21 Aug 2023 09:42:25 +0200 Subject: [PATCH] Fix linter warnings (#5679) --- lib/mongoid/association/macros.rb | 18 ++++++++++-------- lib/mongoid/attributes/processing.rb | 21 ++++++++++----------- 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/lib/mongoid/association/macros.rb b/lib/mongoid/association/macros.rb index 6ab0f9505a..c06663db30 100644 --- a/lib/mongoid/association/macros.rb +++ b/lib/mongoid/association/macros.rb @@ -1,9 +1,7 @@ # frozen_string_literal: true -# rubocop:todo all module Mongoid module Association - # This module contains the core macros for defining associations between # documents. They can be either embedded or referenced. module Macros @@ -36,7 +34,7 @@ module Macros # @api private class_attribute :aliased_associations - # @return [ Set ] The set of associations that are configured + # @return [ Set ] The set of associations that are configured # with :store_as parameter. class_attribute :stored_as_associations @@ -54,11 +52,11 @@ module Macros # # @return [ Hash ] The associations. def associations - self.relations + relations end + # Class methods for associations. module ClassMethods - # Adds the association back to the parent document. This macro is # necessary to set the references from the child back to the parent # document. If a child does not define this association calling @@ -151,6 +149,8 @@ def belongs_to(name, options = {}, &block) define_association!(__method__, name, options, &block) end + # rubocop:disable Naming/PredicateName + # Adds a referenced association from a parent Document to many # Documents in another database or collection. # @@ -217,15 +217,17 @@ def has_one(name, options = {}, &block) define_association!(__method__, name, options, &block) end + # rubocop:enable Naming/PredicateName + private def define_association!(macro_name, name, options = {}, &block) Association::MACRO_MAPPING[macro_name].new(self, name, options, &block).tap do |assoc| assoc.setup! - self.relations = self.relations.merge(name => assoc) + self.relations = relations.merge(name => assoc) if assoc.embedded? && assoc.respond_to?(:store_as) && assoc.store_as != name - self.aliased_associations[assoc.store_as] = name - self.stored_as_associations << assoc.store_as + aliased_associations[assoc.store_as] = name + stored_as_associations << assoc.store_as end end end diff --git a/lib/mongoid/attributes/processing.rb b/lib/mongoid/attributes/processing.rb index b52884d57e..0fc883776a 100644 --- a/lib/mongoid/attributes/processing.rb +++ b/lib/mongoid/attributes/processing.rb @@ -1,12 +1,9 @@ # frozen_string_literal: true -# rubocop:todo all module Mongoid module Attributes - # This module contains the behavior for processing attributes. module Processing - # Process the provided attributes casting them to their proper values if a # field exists for them on the document. This will be limited to only the # attributes provided in the supplied +Hash+ so that no extra nil values get @@ -18,10 +15,11 @@ module Processing # @param [ Hash ] attrs The attributes to set. def process_attributes(attrs = nil) attrs ||= {} - if !attrs.empty? + unless attrs.empty? attrs = sanitize_for_mass_assignment(attrs) attrs.each_pair do |key, value| next if pending_attribute?(key, value) + process_attribute(key, value) end end @@ -45,15 +43,15 @@ def process_attributes(attrs = nil) def pending_attribute?(key, value) name = key.to_s aliased = if aliased_associations.key?(name) - aliased_associations[name] - else - name - end - if relations.has_key?(aliased) + aliased_associations[name] + else + name + end + if relations.key?(aliased) set_pending_relation(name, aliased, value) return true end - if nested_attributes.has_key?(aliased) + if nested_attributes.key?(aliased) set_pending_nested(name, aliased, value) return true end @@ -115,11 +113,12 @@ def pending_nested # @param [ Symbol ] name The name of the field. # @param [ Object ] value The value of the field. def process_attribute(name, value) - if !respond_to?("#{name}=", true) && store_as = aliased_fields.invert[name.to_s] + if !respond_to?("#{name}=", true) && (store_as = aliased_fields.invert[name.to_s]) name = store_as end responds = respond_to?("#{name}=", true) raise Errors::UnknownAttribute.new(self.class, name) unless responds + send("#{name}=", value) end