Sha256: 50a3e88305d8fc5e0bd32dc74a8860408e56f3bdd0a1d7325165fb1ae2dd42b9

Contents?: true

Size: 1.63 KB

Versions: 14

Compression:

Stored size: 1.63 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

      def where_object_changes_condition(*)
        raise <<-STR.squish.freeze
          where_object_changes no longer supports reading JSON from a text
          column. The old implementation was inaccurate, returning more records
          than you wanted. This feature was deprecated in 7.1.0 and removed in
          8.0.0. The json and jsonb datatypes are still supported. See the
          discussion at https://github.com/paper-trail-gem/paper_trail/issues/803
        STR
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 2 rubygems

Version Path
paper_trail-11.1.0 lib/paper_trail/serializers/json.rb
mongo_trails-10.3.1 lib/mongo_trails/serializers/json.rb
paper_trail-11.0.0 lib/paper_trail/serializers/json.rb
paper_trail-10.3.1 lib/paper_trail/serializers/json.rb
paper_trail-10.3.0 lib/paper_trail/serializers/json.rb
paper_trail-10.2.1 lib/paper_trail/serializers/json.rb
paper_trail-10.2.0 lib/paper_trail/serializers/json.rb
paper_trail-10.1.0 lib/paper_trail/serializers/json.rb
paper_trail-10.0.1 lib/paper_trail/serializers/json.rb
paper_trail-10.0.0 lib/paper_trail/serializers/json.rb
paper_trail-9.2.0 lib/paper_trail/serializers/json.rb
paper_trail-9.1.1 lib/paper_trail/serializers/json.rb
paper_trail-9.1.0 lib/paper_trail/serializers/json.rb
paper_trail-9.0.2 lib/paper_trail/serializers/json.rb