Skip to content

Commit

Permalink
Fix linter warnings (#5679)
Browse files Browse the repository at this point in the history
  • Loading branch information
comandeo-mongo authored Aug 21, 2023
1 parent a7edd84 commit f833230
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 19 deletions.
18 changes: 10 additions & 8 deletions lib/mongoid/association/macros.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -36,7 +34,7 @@ module Macros
# @api private
class_attribute :aliased_associations

# @return [ Set<String> ] The set of associations that are configured
# @return [ Set<String> ] The set of associations that are configured
# with :store_as parameter.
class_attribute :stored_as_associations

Expand All @@ -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
Expand Down Expand Up @@ -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.
#
Expand Down Expand Up @@ -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
Expand Down
21 changes: 10 additions & 11 deletions lib/mongoid/attributes/processing.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit f833230

Please sign in to comment.