Skip to content

Commit

Permalink
Some refactoring to remove property and extend changeset rather t…
Browse files Browse the repository at this point in the history
…han `load_changeset`
  • Loading branch information
Jeremy Anderson committed Jan 4, 2021
1 parent 3d2014d commit 1ea31db
Showing 1 changed file with 35 additions and 21 deletions.
56 changes: 35 additions & 21 deletions lib/paper_trail_association_tracking/version_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,37 +35,51 @@ module VersionConcern
#end

module ClassMethods
private
def changeset(options = {})
super()
return @changeset unless ::PaperTrail.config.track_associations?

def load_changeset
changes = super
return changes unless ::PaperTrail.config.track_associations?
@changeset = load_changeset_has_many_through(@changeset) if options[:has_many_through]
@changeset
end

item_type.to_s.classify.constantize.reflect_on_all_associations(:has_many).reduce(changes) do |acc, assoc|
if paper_trail_enabled?(assoc)
if assoc.through_reflection?
assoc_changes = fetch_has_many_through(assoc, :name)
private

def load_changeset_has_many_through(changes)
has_many_through_assocs
.reduce(changes) do |acc, assoc|
assoc_changes = has_many_through_changes(assoc)
return acc.merge(assoc_changes) if assoc_changes.any?
end

acc
end

acc
end
changes
end

def has_many_assocs
item_type.to_s.classify.constantize
.reflect_on_all_associations(:has_many)
end

def has_many_through_assocs
has_many_assocs
.select { |assoc| assoc.through_reflection? }
end

def paper_trail_enabled?(assoc)
::PaperTrail.request.enabled_for_model?(assoc.klass) ||
(assoc.through_reflection? && ::PaperTrail.request.enabled_for_model?(assoc.through_reflection.klass))
end

def fetch_has_many_through(assoc, property)
def has_many_through_changes(assoc)
return {} unless object_changes.present?

versions = find_associated_versions(assoc.through_reflection)

return { "#{assoc.name}": updated_changes(assoc, versions, property) } if updated_changes(assoc, versions, property).any?
return { "#{assoc.name}": updated_changes(assoc, versions) } if updated_changes(assoc, versions).any?

{ "#{assoc.name}": [removed_changes(assoc, versions, property), added_changes(assoc, versions, property)] }
{ "#{assoc.name}": [removed_changes(assoc, versions), added_changes(assoc, versions)] }
end

def find_associated_versions(assoc)
Expand All @@ -75,30 +89,30 @@ def find_associated_versions(assoc)
.order('versions.created_at desc')
end

def updated_changes(assoc, versions, property)
def updated_changes(assoc, versions)
versions.select { |version| version.event == "update" }
.map(&:changeset)
.pluck(assoc.foreign_key)
.flat_map do |change|
[
assoc.klass.where(id: change.first).pick(property),
assoc.klass.where(id: change.second).pick(property)
assoc.klass.find_by(id: change.first),
assoc.klass.find_by(id: change.second)
]
end
end

def added_changes(assoc, versions, property)
def added_changes(assoc, versions)
versions.select { |version| version.event == "create" }
.map(&:changeset)
.pluck(assoc.foreign_key)
.map { |change| assoc.klass.where(id: change.second).pick(property) }
.map { |change| assoc.klass.find_by(id: change.second) }
end

def removed_changes(assoc, versions, property)
def removed_changes(assoc, versions)
versions.select { |version| version.event == "destroy" }
.map(&:changeset)
.pluck(assoc.foreign_key)
.map { |change| assoc.klass.where(id: change.first).pick(property) }
.map { |change| assoc.klass.find_by(id: change.first) }
end
end
end
Expand Down

0 comments on commit 1ea31db

Please sign in to comment.