Extract private method load_versions_for_hm_association
This commit is contained in:
parent
58b46f05f6
commit
6d076d7cb2
|
@ -2,7 +2,7 @@
|
||||||
# one by one as the offenses are removed from the code base.
|
# one by one as the offenses are removed from the code base.
|
||||||
|
|
||||||
Metrics/AbcSize:
|
Metrics/AbcSize:
|
||||||
Max: 25 # Goal: 15
|
Max: 24 # Goal: 15
|
||||||
|
|
||||||
Metrics/CyclomaticComplexity:
|
Metrics/CyclomaticComplexity:
|
||||||
Max: 8 # Goal: 6
|
Max: 8 # Goal: 6
|
||||||
|
|
|
@ -201,6 +201,22 @@ module PaperTrail
|
||||||
first
|
first
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Given a `has_many` association on `model`, return the version records
|
||||||
|
# from the point in time identified by `tx_id` or `version_at`.
|
||||||
|
# @api private
|
||||||
|
def load_versions_for_hm_association(assoc, model, version_table, tx_id, version_at)
|
||||||
|
version_id_subquery = ::PaperTrail::VersionAssociation.
|
||||||
|
joins(model.class.version_association_name).
|
||||||
|
select("MIN(version_id)").
|
||||||
|
where("foreign_key_name = ?", assoc.foreign_key).
|
||||||
|
where("foreign_key_id = ?", model.id).
|
||||||
|
where("#{version_table}.item_type = ?", assoc.class_name).
|
||||||
|
where("created_at >= ? OR transaction_id = ?", version_at, tx_id).
|
||||||
|
group("item_id").
|
||||||
|
to_sql
|
||||||
|
versions_by_id(model.class, version_id_subquery)
|
||||||
|
end
|
||||||
|
|
||||||
# Set all the attributes in this version on the model.
|
# Set all the attributes in this version on the model.
|
||||||
def reify_attributes(model, version, attrs)
|
def reify_attributes(model, version, attrs)
|
||||||
enums = model.class.respond_to?(:defined_enums) ? model.class.defined_enums : {}
|
enums = model.class.respond_to?(:defined_enums) ? model.class.defined_enums : {}
|
||||||
|
@ -366,16 +382,13 @@ module PaperTrail
|
||||||
# Reify a single, direct (not `through`) `has_many` association of `model`.
|
# Reify a single, direct (not `through`) `has_many` association of `model`.
|
||||||
# @api private
|
# @api private
|
||||||
def reify_has_many_association(assoc, model, options, transaction_id, version_table_name)
|
def reify_has_many_association(assoc, model, options, transaction_id, version_table_name)
|
||||||
version_id_subquery = PaperTrail::VersionAssociation.
|
versions = load_versions_for_hm_association(
|
||||||
joins(model.class.version_association_name).
|
assoc,
|
||||||
select("MIN(version_id)").
|
model,
|
||||||
where("foreign_key_name = ?", assoc.foreign_key).
|
version_table_name,
|
||||||
where("foreign_key_id = ?", model.id).
|
transaction_id,
|
||||||
where("#{version_table_name}.item_type = ?", assoc.class_name).
|
options[:version_at]
|
||||||
where("created_at >= ? OR transaction_id = ?", options[:version_at], transaction_id).
|
)
|
||||||
group("item_id").
|
|
||||||
to_sql
|
|
||||||
versions = versions_by_id(model.class, version_id_subquery)
|
|
||||||
collection = Array.new model.send(assoc.name).reload # to avoid cache
|
collection = Array.new model.send(assoc.name).reload # to avoid cache
|
||||||
prepare_array_for_has_many(collection, options, versions)
|
prepare_array_for_has_many(collection, options, versions)
|
||||||
model.send(assoc.name).proxy_association.target = collection
|
model.send(assoc.name).proxy_association.target = collection
|
||||||
|
|
Loading…
Reference in New Issue