Skip to content

Commit

Permalink
Normalize AttributeSerializers model_class refs
Browse files Browse the repository at this point in the history
  • Loading branch information
sirwolfgang committed Aug 18, 2024
1 parent be31c66 commit 128ee1a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ module AttributeSerializers
module AttributeSerializerFactory
class << self
# @api private
def for(klass, attr)
active_record_serializer = klass.type_for_attribute(attr)
def for(model_class, attr)
active_record_serializer = model_class.type_for_attribute(attr)
if ar_pg_array?(active_record_serializer)
TypeSerializers::PostgresArraySerializer.new(
active_record_serializer.subtype,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ module AttributeSerializers
# example, the string "1.99" serializes into the integer `1` when assigned
# to an attribute of type `ActiveRecord::Type::Integer`.
class CastAttributeSerializer
def initialize(klass)
@klass = klass
def initialize(model_class)
@model_class = model_class
end

private
Expand All @@ -25,7 +25,7 @@ def initialize(klass)
# ActiveRecord::Enum was added in AR 4.1
# http://edgeguides.rubyonrails.org/4_1_release_notes.html#active-record-enums
def defined_enums
@defined_enums ||= (@klass.respond_to?(:defined_enums) ? @klass.defined_enums : {})
@defined_enums ||= (@model_class.respond_to?(:defined_enums) ? @model_class.defined_enums : {})
end

def deserialize(attr, val)
Expand All @@ -39,12 +39,12 @@ def deserialize(attr, val)
# https://github.com/rails/rails/issues/43966
val.instance_variable_get(:@time)
else
AttributeSerializerFactory.for(@klass, attr).deserialize(val)
AttributeSerializerFactory.for(@model_class, attr).deserialize(val)
end
end

def serialize(attr, val)
AttributeSerializerFactory.for(@klass, attr).serialize(val)
AttributeSerializerFactory.for(@model_class, attr).serialize(val)
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ module PaperTrail
module AttributeSerializers
# Serialize or deserialize the `version.object_changes` column.
class ObjectChangesAttribute
def initialize(item_class)
@item_class = item_class
def initialize(model_class)
@model_class = model_class

# ActiveRecord since 7.0 has a built-in encryption mechanism
@encrypted_attributes =
if PaperTrail.active_record_gte_7_0?
@item_class.encrypted_attributes&.map(&:to_s)
@model_class.encrypted_attributes&.map(&:to_s)
end
end

Expand All @@ -35,7 +35,7 @@ def alter(changes, serialization_method)
object_changes_col_is_json? ? changes.slice(*@encrypted_attributes) : changes.clone
return changes if changes_to_serialize.blank?

serializer = CastAttributeSerializer.new(@item_class)
serializer = CastAttributeSerializer.new(@model_class)
changes_to_serialize.each do |key, change|
# `change` is an Array with two elements, representing before and after.
changes[key] = Array(change).map do |value|
Expand All @@ -47,7 +47,7 @@ def alter(changes, serialization_method)
end

def object_changes_col_is_json?
@item_class.paper_trail.version_class.object_changes_col_is_json?
@model_class.paper_trail.version_class.object_changes_col_is_json?
end
end
end
Expand Down

0 comments on commit 128ee1a

Please sign in to comment.