2016-03-05 17:07:32 -05:00
|
|
|
require "yaml"
|
2012-07-03 23:22:52 -04:00
|
|
|
|
|
|
|
module PaperTrail
|
|
|
|
module Serializers
|
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
|
|
|
|
2015-01-07 16:57:45 -05:00
|
|
|
# Returns a SQL 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
|
|
|
|
|
|
|
# Returns a SQL condition to be used to match the given field and value
|
|
|
|
# in the serialized object_changes
|
|
|
|
def where_object_changes_condition(arel_field, field, value)
|
|
|
|
# Need to check first (before) and secondary (after) fields
|
2016-03-05 17:07:32 -05:00
|
|
|
if (defined?(::YAML::ENGINE) && ::YAML::ENGINE.yamler == "psych") ||
|
2016-03-05 17:30:53 -05:00
|
|
|
(defined?(::Psych) && ::YAML == ::Psych)
|
2015-01-14 14:19:37 -05:00
|
|
|
arel_field.matches("%\n#{field}:\n- #{value}\n%").
|
2015-01-14 14:53:45 -05:00
|
|
|
or(arel_field.matches("%\n#{field}:\n-%\n- #{value}\n%"))
|
2015-01-14 14:19:37 -05:00
|
|
|
else # Syck adds extra spaces into array dumps
|
2015-01-14 15:52:07 -05:00
|
|
|
arel_field.matches("%\n#{field}: \n%- #{value}\n%").
|
2015-01-14 14:53:45 -05:00
|
|
|
or(arel_field.matches("%\n#{field}: \n-%\n- #{value}\n%"))
|
2015-01-14 14:19:37 -05:00
|
|
|
end
|
2015-01-07 16:57:45 -05:00
|
|
|
end
|
2012-07-03 23:22:52 -04:00
|
|
|
end
|
|
|
|
end
|
2012-12-28 01:12:42 -05:00
|
|
|
end
|