2016-03-05 22:07:32 +00:00
|
|
|
require "active_support/json"
|
2013-01-22 23:13:58 +00:00
|
|
|
|
|
|
|
module PaperTrail
|
|
|
|
module Serializers
|
2016-04-09 05:08:34 +00:00
|
|
|
# An alternate serializer for, e.g. `versions.object`.
|
2013-10-18 02:05:24 +00:00
|
|
|
module JSON
|
2013-01-31 19:49:25 +00:00
|
|
|
extend self # makes all instance methods become module methods as well
|
|
|
|
|
|
|
|
def load(string)
|
2013-01-22 23:13:58 +00:00
|
|
|
ActiveSupport::JSON.decode string
|
|
|
|
end
|
|
|
|
|
2013-01-31 19:49:25 +00:00
|
|
|
def dump(object)
|
2013-01-31 16:22:03 +00:00
|
|
|
ActiveSupport::JSON.encode object
|
2013-01-22 23:13:58 +00:00
|
|
|
end
|
2014-06-07 14:42:17 +00:00
|
|
|
|
2016-05-10 06:11:41 +00:00
|
|
|
# Returns a SQL LIKE condition to be used to match the given field and
|
|
|
|
# value in the serialized object.
|
2014-06-07 14:42:17 +00:00
|
|
|
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},%").
|
2016-03-14 00:04:08 +00:00
|
|
|
or(arel_field.matches("%\"#{field}\":#{json_value}}%"))
|
2014-06-07 14:42:17 +00:00
|
|
|
else
|
|
|
|
arel_field.matches("%\"#{field}\":#{json_value}%")
|
|
|
|
end
|
|
|
|
end
|
2015-01-07 21:57:45 +00:00
|
|
|
|
2017-09-20 07:53:51 +00:00
|
|
|
def where_object_changes_condition(*)
|
|
|
|
raise <<-STR.squish.freeze
|
2017-12-11 02:58:18 +00:00
|
|
|
where_object_changes no longer supports reading JSON from a text
|
2017-09-20 07:53:51 +00:00
|
|
|
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/airblade/paper_trail/issues/803
|
|
|
|
STR
|
2015-01-07 21:57:45 +00:00
|
|
|
end
|
2013-01-22 23:13:58 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|