Skip to content

Commit

Permalink
fix problem with interpreting nested attribute data
Browse files Browse the repository at this point in the history
  • Loading branch information
jamis committed Jul 29, 2024
1 parent e58aed9 commit 2bad8fd
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
15 changes: 14 additions & 1 deletion lib/mongoid/association/nested/one.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,25 @@ def initialize(association, attributes, options)
@attributes = attributes.with_indifferent_access
@association = association
@options = options
@class_name = options[:class_name] ? options[:class_name].constantize : association.klass
@class_name = class_from(options[:class_name])
@destroy = @attributes.delete(:_destroy)
end

private

# Coerces the argument into a class, or defaults to the association's class.
#
# @param [ String | Mongoid::Document | nil ] name_or_class the value to coerce
#
# @return [ Mongoid::Document ] the resulting class
def class_from(name_or_class)
case name_or_class
when nil, false then association.klass
when String then name_or_class.constantize
else name_or_class
end
end

# Extracts and converts the id to the expected type.
#
# @return [ BSON::ObjectId | String | Object | nil ] The converted id,
Expand Down
1 change: 1 addition & 0 deletions spec/mongoid/attributes/nested_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# rubocop:todo all

require "spec_helper"
require 'support/models/sandwich'
require_relative '../association/referenced/has_many_models'
require_relative '../association/referenced/has_and_belongs_to_many_models'
require_relative './nested_spec_models'
Expand Down

0 comments on commit 2bad8fd

Please sign in to comment.