2016-03-05 22:07:32 +00:00
|
|
|
require "yaml"
|
2012-07-04 03:22:52 +00:00
|
|
|
|
|
|
|
module PaperTrail
|
|
|
|
module Serializers
|
2016-04-09 05:08:34 +00:00
|
|
|
# The default serializer for, e.g. `versions.object`.
|
2013-10-18 02:05:24 +00:00
|
|
|
module YAML
|
2017-10-23 17:07:27 +00:00
|
|
|
E_WHERE_OBJ_CHANGES = <<-STR.squish.freeze
|
|
|
|
where_object_changes has a known issue. When reading YAML from a text
|
|
|
|
column, it may return more records than expected. Instead of a warning,
|
|
|
|
this method may raise an error in the future. Please join the discussion
|
|
|
|
at https://github.com/airblade/paper_trail/pull/997
|
|
|
|
STR
|
|
|
|
|
2013-01-31 19:49:25 +00:00
|
|
|
extend self # makes all instance methods become module methods as well
|
|
|
|
|
|
|
|
def load(string)
|
2013-10-18 02:05:24 +00:00
|
|
|
::YAML.load string
|
2012-07-04 03:22:52 +00:00
|
|
|
end
|
|
|
|
|
2013-01-31 19:49:25 +00:00
|
|
|
def dump(object)
|
2013-10-18 02:05:24 +00:00
|
|
|
::YAML.dump object
|
2012-07-04 03:22:52 +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)
|
|
|
|
arel_field.matches("%\n#{field}: #{value}\n%")
|
|
|
|
end
|
2015-01-07 21:57:45 +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_changes`.
|
2015-01-07 21:57:45 +00:00
|
|
|
def where_object_changes_condition(arel_field, field, value)
|
2017-10-23 17:07:27 +00:00
|
|
|
::ActiveSupport::Deprecation.warn(E_WHERE_OBJ_CHANGES)
|
|
|
|
|
2015-01-07 21:57:45 +00:00
|
|
|
# Need to check first (before) and secondary (after) fields
|
2017-04-01 02:26:41 +00:00
|
|
|
m1 = "%\n#{field}:\n- #{value}\n%"
|
|
|
|
m2 = "%\n#{field}:\n-%\n- #{value}\n%"
|
2016-03-05 22:39:00 +00:00
|
|
|
arel_field.matches(m1).or(arel_field.matches(m2))
|
2015-01-07 21:57:45 +00:00
|
|
|
end
|
2012-07-04 03:22:52 +00:00
|
|
|
end
|
|
|
|
end
|
2012-12-28 06:12:42 +00:00
|
|
|
end
|