2013-12-03 16:00:04 -05:00
|
|
|
require 'active_support/concern'
|
|
|
|
|
|
|
|
module PaperTrail
|
|
|
|
module VersionConcern
|
|
|
|
extend ::ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
|
|
|
belongs_to :item, :polymorphic => true
|
2015-01-20 15:25:06 -05:00
|
|
|
|
2015-03-19 23:27:43 -04:00
|
|
|
# Since the test suite has test coverage for this, we want to declare the
|
|
|
|
# association when the test suite is running. This makes it pass
|
|
|
|
# when DB is not initialized prior to test runs such as when we run on
|
|
|
|
# Travis CI (there won't be a db in `test/dummy/db/`)
|
2015-03-25 19:17:22 -04:00
|
|
|
if PaperTrail.config.track_associations?
|
2015-01-20 15:25:06 -05:00
|
|
|
has_many :version_associations, :dependent => :destroy
|
|
|
|
end
|
2014-02-25 09:02:36 -05:00
|
|
|
|
2013-12-03 16:00:04 -05:00
|
|
|
validates_presence_of :event
|
2014-10-20 13:04:51 -04:00
|
|
|
|
2015-01-20 16:07:33 -05:00
|
|
|
if PaperTrail.active_record_protected_attributes?
|
|
|
|
attr_accessible :item_type, :item_id, :event, :whodunnit, :object, :object_changes, :transaction_id, :created_at
|
|
|
|
end
|
2013-12-03 16:00:04 -05:00
|
|
|
|
|
|
|
after_create :enforce_version_limit!
|
2014-02-25 09:02:36 -05:00
|
|
|
|
|
|
|
scope :within_transaction, lambda { |id| where :transaction_id => id }
|
2013-12-03 16:00:04 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
module ClassMethods
|
|
|
|
def with_item_keys(item_type, item_id)
|
|
|
|
where :item_type => item_type, :item_id => item_id
|
|
|
|
end
|
|
|
|
|
|
|
|
def creates
|
|
|
|
where :event => 'create'
|
|
|
|
end
|
|
|
|
|
|
|
|
def updates
|
|
|
|
where :event => 'update'
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroys
|
|
|
|
where :event => 'destroy'
|
|
|
|
end
|
|
|
|
|
|
|
|
def not_creates
|
|
|
|
where 'event <> ?', 'create'
|
|
|
|
end
|
|
|
|
|
2014-04-01 13:38:53 -04:00
|
|
|
# Expects `obj` to be an instance of `PaperTrail::Version` by default, but can accept a timestamp if
|
|
|
|
# `timestamp_arg` receives `true`
|
|
|
|
def subsequent(obj, timestamp_arg = false)
|
|
|
|
if timestamp_arg != true && self.primary_key_is_int?
|
2014-05-28 19:18:53 -04:00
|
|
|
return where(arel_table[primary_key].gt(obj.id)).order(arel_table[primary_key].asc)
|
2014-04-01 13:38:53 -04:00
|
|
|
end
|
|
|
|
|
2013-12-03 16:00:04 -05:00
|
|
|
obj = obj.send(PaperTrail.timestamp_field) if obj.is_a?(self)
|
2014-05-28 19:18:53 -04:00
|
|
|
where(arel_table[PaperTrail.timestamp_field].gt(obj)).order(self.timestamp_sort_order)
|
2013-12-03 16:00:04 -05:00
|
|
|
end
|
|
|
|
|
2014-04-01 13:38:53 -04:00
|
|
|
def preceding(obj, timestamp_arg = false)
|
|
|
|
if timestamp_arg != true && self.primary_key_is_int?
|
2014-05-28 19:18:53 -04:00
|
|
|
return where(arel_table[primary_key].lt(obj.id)).order(arel_table[primary_key].desc)
|
2014-04-01 13:38:53 -04:00
|
|
|
end
|
|
|
|
|
2013-12-03 16:00:04 -05:00
|
|
|
obj = obj.send(PaperTrail.timestamp_field) if obj.is_a?(self)
|
2014-05-28 19:18:53 -04:00
|
|
|
where(arel_table[PaperTrail.timestamp_field].lt(obj)).order(self.timestamp_sort_order('desc'))
|
2013-12-03 16:00:04 -05:00
|
|
|
end
|
|
|
|
|
2014-04-01 13:38:53 -04:00
|
|
|
|
2013-12-03 16:00:04 -05:00
|
|
|
def between(start_time, end_time)
|
2014-05-28 19:18:53 -04:00
|
|
|
where(
|
|
|
|
arel_table[PaperTrail.timestamp_field].gt(start_time).
|
|
|
|
and(arel_table[PaperTrail.timestamp_field].lt(end_time))
|
|
|
|
).order(self.timestamp_sort_order)
|
2014-04-01 13:38:53 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# defaults to using the primary key as the secondary sort order if possible
|
2014-05-28 19:18:53 -04:00
|
|
|
def timestamp_sort_order(direction = 'asc')
|
|
|
|
[arel_table[PaperTrail.timestamp_field].send(direction.downcase)].tap do |array|
|
|
|
|
array << arel_table[primary_key].send(direction.downcase) if self.primary_key_is_int?
|
2014-05-09 11:01:33 -04:00
|
|
|
end
|
2014-04-01 13:38:53 -04:00
|
|
|
end
|
|
|
|
|
2014-06-07 10:42:17 -04:00
|
|
|
# Performs an attribute search on the serialized object by invoking the
|
|
|
|
# identically-named method in the serializer being used.
|
2014-06-26 16:11:22 -04:00
|
|
|
def where_object(args = {})
|
|
|
|
raise ArgumentError, 'expected to receive a Hash' unless args.is_a?(Hash)
|
2014-06-07 10:42:17 -04:00
|
|
|
|
2015-04-09 13:49:12 -04:00
|
|
|
if columns_hash['object'].type == :jsonb
|
|
|
|
where_conditions = "object @> '#{args.to_json}'::jsonb"
|
2015-04-09 14:24:09 -04:00
|
|
|
elsif columns_hash['object'].type == :json
|
|
|
|
where_conditions = args.map do |field, value|
|
|
|
|
"object->>'#{field}' = '#{value}'"
|
|
|
|
end
|
|
|
|
where_conditions = where_conditions.join(" AND ")
|
2015-04-06 13:06:14 -04:00
|
|
|
else
|
|
|
|
arel_field = arel_table[:object]
|
|
|
|
|
|
|
|
where_conditions = args.map do |field, value|
|
|
|
|
PaperTrail.serializer.where_object_condition(arel_field, field, value)
|
|
|
|
end.reduce do |condition1, condition2|
|
|
|
|
condition1.and(condition2)
|
|
|
|
end
|
2014-06-07 10:42:17 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
where(where_conditions)
|
|
|
|
end
|
|
|
|
|
2015-01-07 16:57:45 -05:00
|
|
|
def where_object_changes(args = {})
|
|
|
|
raise ArgumentError, 'expected to receive a Hash' unless args.is_a?(Hash)
|
|
|
|
|
2015-04-09 14:56:26 -04:00
|
|
|
if columns_hash['object_changes'].type == :jsonb
|
|
|
|
args.each { |field, value| args[field] = [value] }
|
2015-04-09 13:49:12 -04:00
|
|
|
where_conditions = "object_changes @> '#{args.to_json}'::jsonb"
|
2015-04-09 15:27:15 -04:00
|
|
|
elsif columns_hash['object'].type == :json
|
|
|
|
where_conditions = args.map do |field, value|
|
|
|
|
"((object_changes->>'#{field}' ILIKE '[#{value.to_json},%') OR (object_changes->>'#{field}' ILIKE '[%,#{value.to_json}]%'))"
|
|
|
|
end
|
|
|
|
where_conditions = where_conditions.join(" AND ")
|
2015-04-06 13:06:14 -04:00
|
|
|
else
|
|
|
|
arel_field = arel_table[:object_changes]
|
|
|
|
|
|
|
|
where_conditions = args.map do |field, value|
|
|
|
|
PaperTrail.serializer.where_object_changes_condition(arel_field, field, value)
|
|
|
|
end.reduce do |condition1, condition2|
|
|
|
|
condition1.and(condition2)
|
|
|
|
end
|
2015-01-07 16:57:45 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
where(where_conditions)
|
|
|
|
end
|
|
|
|
|
2014-04-01 13:38:53 -04:00
|
|
|
def primary_key_is_int?
|
|
|
|
@primary_key_is_int ||= columns_hash[primary_key].type == :integer
|
2014-05-09 11:43:30 -04:00
|
|
|
rescue
|
|
|
|
true
|
2013-12-03 16:00:04 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Returns whether the `object` column is using the `json` type supported by PostgreSQL
|
|
|
|
def object_col_is_json?
|
2015-04-09 13:26:24 -04:00
|
|
|
[:json, :jsonb].include?(columns_hash['object'].type)
|
2013-12-03 16:00:04 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Returns whether the `object_changes` column is using the `json` type supported by PostgreSQL
|
|
|
|
def object_changes_col_is_json?
|
2015-04-09 13:26:24 -04:00
|
|
|
[:json, :jsonb].include?(columns_hash['object_changes'].try(:type))
|
2013-12-03 16:00:04 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Restore the item from this version.
|
|
|
|
#
|
2014-11-02 22:05:00 -05:00
|
|
|
# Optionally this can also restore all :has_one and :has_many (including has_many :through) associations as
|
|
|
|
# they were "at the time", if they are also being versioned by PaperTrail.
|
2013-12-03 16:00:04 -05:00
|
|
|
#
|
|
|
|
# Options:
|
2014-11-02 22:05:00 -05:00
|
|
|
# :has_one set to `true` to also reify has_one associations. Default is `false`.
|
|
|
|
# :has_many set to `true` to also reify has_many and has_many :through associations.
|
|
|
|
# Default is `false`.
|
|
|
|
# :mark_for_destruction set to `true` to mark the has_one/has_many associations that did not exist in the
|
|
|
|
# reified version for destruction, instead of remove them. Default is `false`.
|
|
|
|
# This option is handy for people who want to persist the reified version.
|
|
|
|
# :dup `false` default behavior
|
|
|
|
# `true` it always create a new object instance. It is useful for comparing two versions of the same object
|
2013-12-03 16:00:04 -05:00
|
|
|
def reify(options = {})
|
|
|
|
return nil if object.nil?
|
|
|
|
|
|
|
|
without_identity_map do
|
2014-02-25 09:02:36 -05:00
|
|
|
options.reverse_merge!(
|
|
|
|
:version_at => created_at,
|
2014-11-02 22:05:00 -05:00
|
|
|
:mark_for_destruction => false,
|
2014-02-25 09:02:36 -05:00
|
|
|
:has_one => false,
|
|
|
|
:has_many => false
|
|
|
|
)
|
2013-12-03 16:00:04 -05:00
|
|
|
|
|
|
|
attrs = self.class.object_col_is_json? ? object : PaperTrail.serializer.load(object)
|
|
|
|
|
|
|
|
# Normally a polymorphic belongs_to relationship allows us
|
|
|
|
# to get the object we belong to by calling, in this case,
|
|
|
|
# `item`. However this returns nil if `item` has been
|
|
|
|
# destroyed, and we need to be able to retrieve destroyed
|
|
|
|
# objects.
|
|
|
|
#
|
|
|
|
# In this situation we constantize the `item_type` to get hold of
|
|
|
|
# the class...except when the stored object's attributes
|
|
|
|
# include a `type` key. If this is the case, the object
|
|
|
|
# we belong to is using single table inheritance and the
|
|
|
|
# `item_type` will be the base class, not the actual subclass.
|
|
|
|
# If `type` is present but empty, the class is the base class.
|
|
|
|
|
2015-02-03 15:37:11 -05:00
|
|
|
if options[:dup] != true && item
|
2013-12-03 16:00:04 -05:00
|
|
|
model = item
|
|
|
|
# Look for attributes that exist in the model and not in this version. These attributes should be set to nil.
|
|
|
|
(model.attribute_names - attrs.keys).each { |k| attrs[k] = nil }
|
|
|
|
else
|
|
|
|
inheritance_column_name = item_type.constantize.inheritance_column
|
|
|
|
class_name = attrs[inheritance_column_name].blank? ? item_type : attrs[inheritance_column_name]
|
|
|
|
klass = class_name.constantize
|
2015-05-08 12:27:32 -04:00
|
|
|
# the `dup` option always returns a new object, otherwise we should attempt
|
|
|
|
# to look for the item outside of default scope(s)
|
|
|
|
if options[:dup] || (_item = klass.unscoped.find_by_id(item_id)).nil?
|
|
|
|
model = klass.new
|
|
|
|
else
|
|
|
|
model = _item
|
|
|
|
# Look for attributes that exist in the model and not in this version. These attributes should be set to nil.
|
|
|
|
(model.attribute_names - attrs.keys).each { |k| attrs[k] = nil }
|
|
|
|
end
|
2013-12-03 16:00:04 -05:00
|
|
|
end
|
|
|
|
|
2014-12-29 14:38:32 -05:00
|
|
|
if PaperTrail.serialized_attributes?
|
2015-05-08 10:42:22 -04:00
|
|
|
model.class.unserialize_attributes_for_paper_trail! attrs
|
2014-12-29 14:38:32 -05:00
|
|
|
end
|
2013-12-03 16:00:04 -05:00
|
|
|
|
|
|
|
# Set all the attributes in this version on the model
|
|
|
|
attrs.each do |k, v|
|
2014-09-29 03:24:46 -04:00
|
|
|
if model.has_attribute?(k)
|
2013-12-03 16:00:04 -05:00
|
|
|
model[k.to_sym] = v
|
2015-04-27 15:35:48 -04:00
|
|
|
elsif model.respond_to?("#{k}=")
|
|
|
|
model.send("#{k}=", v)
|
2013-12-03 16:00:04 -05:00
|
|
|
else
|
|
|
|
logger.warn "Attribute #{k} does not exist on #{item_type} (Version id: #{id})."
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
model.send "#{model.class.version_association_name}=", self
|
|
|
|
|
|
|
|
unless options[:has_one] == false
|
2014-02-25 09:02:36 -05:00
|
|
|
reify_has_ones model, options
|
|
|
|
end
|
|
|
|
|
|
|
|
unless options[:has_many] == false
|
|
|
|
reify_has_manys model, options
|
2013-12-03 16:00:04 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
model
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-10-21 15:05:16 -04:00
|
|
|
# Returns what changed in this version of the item. `ActiveModel::Dirty#changes`.
|
|
|
|
# returns `nil` if your `versions` table does not have an `object_changes` text column.
|
2013-12-03 16:00:04 -05:00
|
|
|
def changeset
|
|
|
|
return nil unless self.class.column_names.include? 'object_changes'
|
|
|
|
|
|
|
|
_changes = self.class.object_changes_col_is_json? ? object_changes : PaperTrail.serializer.load(object_changes)
|
|
|
|
@changeset ||= HashWithIndifferentAccess.new(_changes).tap do |changes|
|
2014-12-29 14:38:32 -05:00
|
|
|
if PaperTrail.serialized_attributes?
|
2015-05-08 10:42:22 -04:00
|
|
|
item_type.constantize.unserialize_attribute_changes_for_paper_trail!(changes)
|
2014-12-29 14:38:32 -05:00
|
|
|
end
|
2013-12-03 16:00:04 -05:00
|
|
|
end
|
|
|
|
rescue
|
|
|
|
{}
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns who put the item into the state stored in this version.
|
2015-05-08 13:28:47 -04:00
|
|
|
def paper_trail_originator
|
|
|
|
@paper_trail_originator ||= previous.whodunnit rescue nil
|
|
|
|
end
|
|
|
|
|
2013-12-03 16:00:04 -05:00
|
|
|
def originator
|
2015-05-08 13:28:47 -04:00
|
|
|
warn "DEPRECATED: use `paper_trail_originator` instead of `originator`. Support for `originator` will be removed in PaperTrail 4.0"
|
|
|
|
self.paper_trail_originator
|
2013-12-03 16:00:04 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Returns who changed the item from the state it had in this version.
|
|
|
|
# This is an alias for `whodunnit`.
|
|
|
|
def terminator
|
|
|
|
@terminator ||= whodunnit
|
|
|
|
end
|
|
|
|
alias_method :version_author, :terminator
|
|
|
|
|
|
|
|
def sibling_versions(reload = false)
|
|
|
|
@sibling_versions = nil if reload == true
|
|
|
|
@sibling_versions ||= self.class.with_item_keys(item_type, item_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def next
|
|
|
|
@next ||= sibling_versions.subsequent(self).first
|
|
|
|
end
|
|
|
|
|
|
|
|
def previous
|
|
|
|
@previous ||= sibling_versions.preceding(self).first
|
|
|
|
end
|
|
|
|
|
|
|
|
def index
|
2014-05-28 19:18:53 -04:00
|
|
|
table = self.class.arel_table unless @index
|
2014-06-07 10:42:17 -04:00
|
|
|
@index ||=
|
2014-05-28 19:18:53 -04:00
|
|
|
if self.class.primary_key_is_int?
|
|
|
|
sibling_versions.select(table[self.class.primary_key]).order(table[self.class.primary_key].asc).index(self)
|
|
|
|
else
|
|
|
|
sibling_versions.select([table[PaperTrail.timestamp_field], table[self.class.primary_key]]).
|
|
|
|
order(self.class.timestamp_sort_order).index(self)
|
|
|
|
end
|
2013-12-03 16:00:04 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# In Rails 3.1+, calling reify on a previous version confuses the
|
|
|
|
# IdentityMap, if enabled. This prevents insertion into the map.
|
|
|
|
def without_identity_map(&block)
|
|
|
|
if defined?(::ActiveRecord::IdentityMap) && ::ActiveRecord::IdentityMap.respond_to?(:without)
|
|
|
|
::ActiveRecord::IdentityMap.without(&block)
|
|
|
|
else
|
|
|
|
block.call
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Restore the `model`'s has_one associations as they were when this version was
|
|
|
|
# superseded by the next (because that's what the user was looking at when they
|
|
|
|
# made the change).
|
2014-02-25 09:02:36 -05:00
|
|
|
def reify_has_ones(model, options = {})
|
|
|
|
version_table_name = model.class.paper_trail_version_class.table_name
|
2013-12-03 16:00:04 -05:00
|
|
|
model.class.reflect_on_all_associations(:has_one).each do |assoc|
|
2014-12-04 07:32:28 -05:00
|
|
|
if assoc.klass.paper_trail_enabled_for_model?
|
|
|
|
version = model.class.paper_trail_version_class.joins(:version_associations).
|
|
|
|
where("version_associations.foreign_key_name = ?", assoc.foreign_key).
|
|
|
|
where("version_associations.foreign_key_id = ?", model.id).
|
|
|
|
where("#{version_table_name}.item_type = ?", assoc.class_name).
|
|
|
|
where("created_at >= ? OR transaction_id = ?", options[:version_at], transaction_id).
|
|
|
|
order("#{version_table_name}.id ASC").first
|
|
|
|
if version
|
|
|
|
if version.event == 'create'
|
|
|
|
if options[:mark_for_destruction]
|
|
|
|
model.send(assoc.name).mark_for_destruction if model.send(assoc.name, true)
|
|
|
|
else
|
2014-12-10 22:13:24 -05:00
|
|
|
model.appear_as_new_record do
|
|
|
|
model.send "#{assoc.name}=", nil
|
|
|
|
end
|
2014-12-04 07:32:28 -05:00
|
|
|
end
|
2014-11-02 22:05:00 -05:00
|
|
|
else
|
2014-12-04 07:32:28 -05:00
|
|
|
child = version.reify(options.merge(:has_many => false, :has_one => false))
|
|
|
|
model.appear_as_new_record do
|
|
|
|
model.send "#{assoc.name}=", child
|
|
|
|
end
|
2014-02-25 09:02:36 -05:00
|
|
|
end
|
2013-12-03 16:00:04 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-02-25 09:02:36 -05:00
|
|
|
# Restore the `model`'s has_many associations as they were at version_at timestamp
|
|
|
|
# We lookup the first child versions after version_at timestamp or in same transaction.
|
|
|
|
def reify_has_manys(model, options = {})
|
2014-11-02 22:05:00 -05:00
|
|
|
assoc_has_many_through, assoc_has_many_directly =
|
2015-01-14 13:10:13 -05:00
|
|
|
model.class.reflect_on_all_associations(:has_many).
|
|
|
|
partition { |assoc| assoc.options[:through] }
|
2014-11-02 22:05:00 -05:00
|
|
|
reify_has_many_directly(assoc_has_many_directly, model, options)
|
|
|
|
reify_has_many_through(assoc_has_many_through, model, options)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Restore the `model`'s has_many associations not associated through another association
|
|
|
|
def reify_has_many_directly(associations, model, options = {})
|
2014-02-25 09:02:36 -05:00
|
|
|
version_table_name = model.class.paper_trail_version_class.table_name
|
2014-11-02 22:05:00 -05:00
|
|
|
associations.each do |assoc|
|
2014-12-04 07:32:28 -05:00
|
|
|
next unless assoc.klass.paper_trail_enabled_for_model?
|
2014-02-25 09:02:36 -05:00
|
|
|
version_id_subquery = PaperTrail::VersionAssociation.joins(model.class.version_association_name).
|
2015-01-14 13:10:13 -05:00
|
|
|
select("MIN(version_id)").
|
|
|
|
where("foreign_key_name = ?", assoc.foreign_key).
|
|
|
|
where("foreign_key_id = ?", model.id).
|
|
|
|
where("#{version_table_name}.item_type = ?", assoc.class_name).
|
|
|
|
where("created_at >= ? OR transaction_id = ?", options[:version_at], transaction_id).
|
|
|
|
group("item_id").to_sql
|
2014-11-02 22:05:00 -05:00
|
|
|
versions = model.class.paper_trail_version_class.where("id IN (#{version_id_subquery})").inject({}) do |acc, v|
|
|
|
|
acc.merge!(v.item_id => v)
|
|
|
|
end
|
2014-02-25 09:02:36 -05:00
|
|
|
|
|
|
|
# Pass true to force the model to load
|
|
|
|
collection = Array.new model.send(assoc.name, true)
|
|
|
|
|
2014-11-02 22:05:00 -05:00
|
|
|
# Iterate each child to replace it with the previous value if there is a version after the timestamp
|
|
|
|
collection.map! do |c|
|
|
|
|
if (version = versions.delete(c.id)).nil?
|
|
|
|
c
|
|
|
|
elsif version.event == 'create'
|
|
|
|
options[:mark_for_destruction] ? c.tap { |r| r.mark_for_destruction } : nil
|
|
|
|
else
|
2014-11-03 20:20:37 -05:00
|
|
|
version.reify(options.merge(:has_many => false, :has_one => false))
|
2013-12-03 16:00:04 -05:00
|
|
|
end
|
|
|
|
end
|
2014-02-25 09:02:36 -05:00
|
|
|
|
2014-11-03 20:20:37 -05:00
|
|
|
# Reify the rest of the versions and add them to the collection, these versions are for those that
|
|
|
|
# have been removed from the live associations
|
|
|
|
collection += versions.values.map { |version| version.reify(options.merge(:has_many => false, :has_one => false)) }
|
2014-11-02 22:05:00 -05:00
|
|
|
|
|
|
|
model.send(assoc.name).proxy_association.target = collection.compact
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Restore the `model`'s has_many associations through another association
|
|
|
|
# This must be called after the direct has_manys have been reified (reify_has_many_directly)
|
|
|
|
def reify_has_many_through(associations, model, options = {})
|
|
|
|
associations.each do |assoc|
|
2014-11-03 23:52:31 -05:00
|
|
|
next unless assoc.klass.paper_trail_enabled_for_model?
|
2014-11-02 22:05:00 -05:00
|
|
|
through_collection = model.send(assoc.options[:through])
|
|
|
|
collection_keys = through_collection.map { |through_model| through_model.send(assoc.foreign_key) }
|
|
|
|
|
|
|
|
version_id_subquery = assoc.klass.paper_trail_version_class.
|
2015-01-14 13:10:13 -05:00
|
|
|
select("MIN(id)").
|
|
|
|
where("item_type = ?", assoc.class_name).
|
|
|
|
where("item_id IN (?)", collection_keys).
|
|
|
|
where("created_at >= ? OR transaction_id = ?", options[:version_at], transaction_id).
|
|
|
|
group("item_id").to_sql
|
2014-11-02 22:05:00 -05:00
|
|
|
versions = assoc.klass.paper_trail_version_class.where("id IN (#{version_id_subquery})").inject({}) do |acc, v|
|
|
|
|
acc.merge!(v.item_id => v)
|
|
|
|
end
|
|
|
|
|
|
|
|
collection = Array.new assoc.klass.where(assoc.klass.primary_key => collection_keys)
|
|
|
|
|
|
|
|
# Iterate each child to replace it with the previous value if there is a version after the timestamp
|
|
|
|
collection.map! do |c|
|
2014-11-09 21:06:29 -05:00
|
|
|
if (version = versions.delete(c.id)).nil?
|
2014-11-02 22:05:00 -05:00
|
|
|
c
|
|
|
|
elsif version.event == 'create'
|
|
|
|
options[:mark_for_destruction] ? c.tap { |r| r.mark_for_destruction } : nil
|
|
|
|
else
|
2014-11-03 20:20:37 -05:00
|
|
|
version.reify(options.merge(:has_many => false, :has_one => false))
|
2014-11-02 22:05:00 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-11-09 21:06:29 -05:00
|
|
|
# Reify the rest of the versions and add them to the collection, these versions are for those that
|
|
|
|
# have been removed from the live associations
|
|
|
|
collection += versions.values.map { |version| version.reify(options.merge(:has_many => false, :has_one => false)) }
|
|
|
|
|
2014-11-02 22:05:00 -05:00
|
|
|
model.send(assoc.name).proxy_association.target = collection.compact
|
2013-12-03 16:00:04 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# checks to see if a value has been set for the `version_limit` config option, and if so enforces it
|
|
|
|
def enforce_version_limit!
|
|
|
|
return unless PaperTrail.config.version_limit.is_a? Numeric
|
|
|
|
previous_versions = sibling_versions.not_creates
|
|
|
|
return unless previous_versions.size > PaperTrail.config.version_limit
|
|
|
|
excess_previous_versions = previous_versions - previous_versions.last(PaperTrail.config.version_limit)
|
|
|
|
excess_previous_versions.map(&:destroy)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|