Skip to content

Commit

Permalink
add: modelconfig - object_changes_adaptor
Browse files Browse the repository at this point in the history
  • Loading branch information
luke.ib committed Dec 14, 2024
1 parent 94e9c0d commit 68c53bd
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/paper_trail/events/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,8 @@ def recordable_object_changes(changes)
# `object_changes_adapter` expects a plain `Hash`, not a
# `HashWithIndifferentAccess`.
changes = PaperTrail.config.object_changes_adapter.diff(changes.to_hash)
elsif @record.class.paper_trail.object_changes_adapter&.respond_to?(:diff)
changes = @record.class.paper_trail.object_changes_adapter.diff(changes.to_hash)
end

if @record.class.paper_trail.version_class.object_changes_col_is_json?
Expand Down
1 change: 1 addition & 0 deletions lib/paper_trail/has_paper_trail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ module ClassMethods
# sets the `created_at` field for a new Version equal to the `updated_at`
# column of the model being updated. If you instead want `created_at` to
# populate with the current timestamp, set this option to `false`.
# - :object_changes_adapter - uses a custom object_changes.
#
# Plugins like the experimental `paper_trail-association_tracking` gem
# may accept additional options.
Expand Down
11 changes: 11 additions & 0 deletions lib/paper_trail/model_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ def version_class
@version_class ||= @model_class.version_class_name.constantize
end

def object_changes_adapter
@model_class.object_changes_adapter
end

private

# @api private
Expand Down Expand Up @@ -253,5 +257,12 @@ def setup_options(options)
end
@model_class.paper_trail_options[:meta] ||= {}
end

def setup_custom_object_changes_adapter(options)
@model_class.class_attribute :object_changes_adapter
@model_class.object_changes_adapter = options[:object_changes_adapter]

@model_class.send :attr_accessor, @model_class.object_changes_adapter
end
end
end

0 comments on commit 68c53bd

Please sign in to comment.