Sha256: ade373579c4ba730a3d4710a167ec91b5dec1f17581b8d68dba3b7601f617893

Contents?: true

Size: 1.14 KB

Versions: 11

Compression:

Stored size: 1.14 KB

Contents

# frozen_string_literal: true

module PaperTrail
  module Serializers
    # An alternate serializer for, e.g. `versions.object`.
    module JSON
      extend self # makes all instance methods become module methods as well

      def load(string)
        ActiveSupport::JSON.decode string
      end

      def dump(object)
        ActiveSupport::JSON.encode 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)
        # Convert to JSON to handle strings and nulls correctly.
        json_value = value.to_json

        # If the value is a number, we need to ensure that we find the next
        # character too, which is either `,` or `}`, to ensure that searching
        # for the value 12 doesn't yield false positives when the value is
        # 123.
        if value.is_a? Numeric
          arel_field.matches("%\"#{field}\":#{json_value},%").
            or(arel_field.matches("%\"#{field}\":#{json_value}}%"))
        else
          arel_field.matches("%\"#{field}\":#{json_value}%")
        end
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 3 rubygems

Version Path
trusty-cms-7.0.9.1 vendor/bundle/ruby/3.3.0/gems/paper_trail-16.0.0/lib/paper_trail/serializers/json.rb
paper_trail-16.0.0 lib/paper_trail/serializers/json.rb
paper_trail-15.2.0 lib/paper_trail/serializers/json.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/paper_trail-12.3.0/lib/paper_trail/serializers/json.rb
paper_trail-15.1.0 lib/paper_trail/serializers/json.rb
paper_trail-15.0.0 lib/paper_trail/serializers/json.rb
paper_trail-14.0.0 lib/paper_trail/serializers/json.rb
paper_trail-13.0.0 lib/paper_trail/serializers/json.rb
paper_trail-12.3.0 lib/paper_trail/serializers/json.rb
paper_trail-12.2.0 lib/paper_trail/serializers/json.rb
paper_trail-12.1.0 lib/paper_trail/serializers/json.rb