Skip to content

Commit

Permalink
Fixed incorrect call for .map on a hash
Browse files Browse the repository at this point in the history
There was a mistake in the method implementation where coder.map is called instead of directly using coder.key?("value")
  • Loading branch information
Kaakati committed May 20, 2024
1 parent cd31b16 commit 94c9457
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions activemodel/lib/active_model/attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ def uninitialized(name, type)

attr_reader :name, :value_before_type_cast, :type

# This constant is used to prebuild
EMPTY_HASH = {}.freeze

# This method should not be called directly.
# Use #from_database or #from_user
def initialize(name, value_before_type_cast, type, original_attribute = nil, value = nil)
Expand Down Expand Up @@ -124,15 +127,15 @@ def hash
[self.class, name, value_before_type_cast, type].hash
end

def init_with(coder)
def init_with(coder = EMPTY_HASH)
@name = coder["name"]
@value_before_type_cast = coder["value_before_type_cast"]
@type = coder["type"]
@original_attribute = coder["original_attribute"]
@value = coder["value"] if coder.map.key?("value")
@value = coder["value"] if coder.key?("value")
end

def encode_with(coder)
def encode_with(coder = EMPTY_HASH)
coder["name"] = name
coder["value_before_type_cast"] = value_before_type_cast unless value_before_type_cast.nil?
coder["type"] = type if type
Expand All @@ -153,7 +156,9 @@ def original_value_for_database
alias :assigned? :original_attribute

def initialize_dup(other)
if @value&.duplicable?
# Using `defined?(@value)` ensures that it only proceed if `@value` is actually set
# Ensures no unnecessary memory allocation by checking if `@value` is set and duplicable.
if defined?(@value) && @value&.duplicable?
@value = @value.dup
end
end
Expand Down Expand Up @@ -240,7 +245,7 @@ def with_value_from_database(value)
end

class Uninitialized < Attribute # :nodoc:
UNINITIALIZED_ORIGINAL_VALUE = Object.new
UNINITIALIZED_ORIGINAL_VALUE = Object.new.freeze

def initialize(name, type)
super(name, nil, type)
Expand Down

0 comments on commit 94c9457

Please sign in to comment.