Module: PaperTrail::Serializers::JSON

Extended by:
JSON
Included in:
JSON
Defined in:
lib/mongo_trails/serializers/json.rb

Overview

An alternate serializer for, e.g. ‘versions.object`.

Instance Method Summary collapse

Instance Method Details

#dump(object) ⇒ Object



13
14
15
# File 'lib/mongo_trails/serializers/json.rb', line 13

def dump(object)
  ActiveSupport::JSON.encode object
end

#load(string) ⇒ Object



9
10
11
# File 'lib/mongo_trails/serializers/json.rb', line 9

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

#where_object_changes_conditionObject



35
36
37
38
39
40
41
42
43
# File 'lib/mongo_trails/serializers/json.rb', line 35

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

#where_object_condition(arel_field, field, value) ⇒ Object

Returns a SQL LIKE condition to be used to match the given field and value in the serialized object.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/mongo_trails/serializers/json.rb', line 19

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