-
Notifications
You must be signed in to change notification settings - Fork 901
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Query Versions Where Object Changed To Attributes
This extends to the public API to provide more targeted querying of object changes. `where_object_changes` will look for either side of the change of the attributes provided - either versions where the attribute changed __from__ the provided value, or changed __to__ the provided value. The `where_object_changes_to` addition focuses only on one side of that equation. If you want to find versions where the attribute(s) explicitly changed *to* some known value, this will only show those changes, as opposed to both *from* and *to*.
- Loading branch information
Showing
8 changed files
with
186 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
lib/paper_trail/queries/versions/where_object_changes_to.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# frozen_string_literal: true | ||
|
||
module PaperTrail | ||
module Queries | ||
module Versions | ||
# For public API documentation, see `where_object_changes_to` in | ||
# `paper_trail/version_concern.rb`. | ||
# @api private | ||
class WhereObjectChangesTo | ||
# - version_model_class - The class that VersionConcern was mixed into. | ||
# - attributes - A `Hash` of attributes and values. See the public API | ||
# documentation for details. | ||
# @api private | ||
def initialize(version_model_class, attributes) | ||
@version_model_class = version_model_class | ||
@attributes = attributes | ||
end | ||
|
||
# @api private | ||
def execute | ||
if PaperTrail.config.object_changes_adapter&.respond_to?(:where_object_changes_to) | ||
return PaperTrail.config.object_changes_adapter.where_object_changes_to( | ||
@version_model_class, @attributes | ||
) | ||
end | ||
|
||
case @version_model_class.columns_hash["object_changes"].type | ||
when :jsonb, :json | ||
json | ||
else | ||
text | ||
end | ||
end | ||
|
||
private | ||
|
||
# @api private | ||
def json | ||
predicates = [] | ||
values = [] | ||
@attributes.each do |field, value| | ||
predicates.push( | ||
"(object_changes->>? ILIKE ?)" | ||
) | ||
values.concat([field, "[%#{value.to_json}]"]) | ||
end | ||
sql = predicates.join(" and ") | ||
@version_model_class.where(sql, *values) | ||
end | ||
|
||
# @api private | ||
def text | ||
arel_field = @version_model_class.arel_table[:object_changes] | ||
|
||
where_conditions = @attributes.map do |field, value| | ||
::PaperTrail.serializer.where_object_changes_to_condition(arel_field, field, value) | ||
end | ||
|
||
where_conditions = where_conditions.reduce { |a, e| a.and(e) } | ||
@version_model_class.where(where_conditions) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters