2016-03-05 17:07:32 -05:00
|
|
|
require "yaml"
|
2012-07-03 23:22:52 -04:00
|
|
|
|
|
|
|
module PaperTrail
|
|
|
|
module Serializers
|
2016-04-09 01:08:34 -04:00
|
|
|
# The default serializer for, e.g. `versions.object`.
|
2013-10-17 22:05:24 -04:00
|
|
|
module YAML
|
2013-01-31 14:49:25 -05:00
|
|
|
extend self # makes all instance methods become module methods as well
|
|
|
|
|
|
|
|
def load(string)
|
2013-10-17 22:05:24 -04:00
|
|
|
::YAML.load string
|
2012-07-03 23:22:52 -04:00
|
|
|
end
|
|
|
|
|
2013-01-31 14:49:25 -05:00
|
|
|
def dump(object)
|
2013-10-17 22:05:24 -04:00
|
|
|
::YAML.dump object
|
2012-07-03 23:22:52 -04:00
|
|
|
end
|
2014-06-07 10:42:17 -04:00
|
|
|
|
2016-05-10 02:11:41 -04:00
|
|
|
# Returns a SQL LIKE condition to be used to match the given field and
|
|
|
|
# value in the serialized object.
|
2014-06-07 10:42:17 -04:00
|
|
|
def where_object_condition(arel_field, field, value)
|
|
|
|
arel_field.matches("%\n#{field}: #{value}\n%")
|
|
|
|
end
|
2015-01-07 16:57:45 -05:00
|
|
|
|
2016-05-10 02:11:41 -04:00
|
|
|
# Returns a SQL LIKE condition to be used to match the given field and
|
|
|
|
# value in the serialized `object_changes`.
|
2015-01-07 16:57:45 -05:00
|
|
|
def where_object_changes_condition(arel_field, field, value)
|
|
|
|
# Need to check first (before) and secondary (after) fields
|
2016-03-05 17:39:00 -05:00
|
|
|
m1 = nil
|
|
|
|
m2 = nil
|
2016-03-05 17:48:20 -05:00
|
|
|
case yaml_engine_id
|
|
|
|
when :psych
|
2016-03-05 17:39:00 -05:00
|
|
|
m1 = "%\n#{field}:\n- #{value}\n%"
|
|
|
|
m2 = "%\n#{field}:\n-%\n- #{value}\n%"
|
2016-03-05 17:48:20 -05:00
|
|
|
when :syck
|
2016-03-05 17:39:00 -05:00
|
|
|
# Syck adds extra spaces into array dumps
|
|
|
|
m1 = "%\n#{field}: \n%- #{value}\n%"
|
|
|
|
m2 = "%\n#{field}: \n-%\n- #{value}\n%"
|
2016-03-05 17:48:20 -05:00
|
|
|
else
|
|
|
|
raise "Unknown yaml engine"
|
2015-01-14 14:19:37 -05:00
|
|
|
end
|
2016-03-05 17:39:00 -05:00
|
|
|
arel_field.matches(m1).or(arel_field.matches(m2))
|
2015-01-07 16:57:45 -05:00
|
|
|
end
|
2016-03-05 17:48:20 -05:00
|
|
|
|
|
|
|
# Returns a symbol identifying the YAML engine. Syck was removed from
|
|
|
|
# the ruby stdlib in ruby 2.0, but is still available as a gem.
|
|
|
|
# @api private
|
|
|
|
def yaml_engine_id
|
|
|
|
if (defined?(::YAML::ENGINE) && ::YAML::ENGINE.yamler == "psych") ||
|
|
|
|
(defined?(::Psych) && ::YAML == ::Psych)
|
|
|
|
:psych
|
|
|
|
else
|
|
|
|
:syck
|
|
|
|
end
|
|
|
|
end
|
2012-07-03 23:22:52 -04:00
|
|
|
end
|
|
|
|
end
|
2012-12-28 01:12:42 -05:00
|
|
|
end
|