Sha256: 27ea41d1a5e8c0757f702b65bd6f7f3d1f681e00a1ef38f65fb279bc7d67477f

Contents?: true

Size: 1.53 KB

Versions: 1

Compression:

Stored size: 1.53 KB

Contents

# frozen_string_literal: true

require "yaml"

module PaperTrail
  module Serializers
    # The default serializer for, e.g. `versions.object`.
    module YAML
      extend self # makes all instance methods become module methods as well

      def load(string)
        if use_safe_load?
          ::YAML.safe_load(
            string,
            permitted_classes: ::ActiveRecord.yaml_column_permitted_classes,
            aliases: true
          )
        elsif ::YAML.respond_to?(:unsafe_load)
          ::YAML.unsafe_load(string)
        else
          ::YAML.load(string)
        end
      end

      # @param object (Hash | HashWithIndifferentAccess) - Coming from
      # `recordable_object` `object` will be a plain `Hash`. However, due to
      # recent [memory optimizations](https://github.com/paper-trail-gem/paper_trail/pull/1189),
      # when coming from `recordable_object_changes`, it will be a `HashWithIndifferentAccess`.
      def dump(object)
        object = object.to_hash if object.is_a?(HashWithIndifferentAccess)
        ::YAML.dump object
      end

      # Returns a SQL LIKE condition to be used to match the given field and
      # value in the serialized object.
      def where_object_condition(arel_field, field, value)
        arel_field.matches("%\n#{field}: #{value}\n%")
      end

      private

      # `use_yaml_unsafe_load` was added in 7.0.3.1, will be removed in 7.1.0?
      def use_safe_load?
        defined?(ActiveRecord.use_yaml_unsafe_load) &&
          !ActiveRecord.use_yaml_unsafe_load
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
paper_trail-13.0.0 lib/paper_trail/serializers/yaml.rb