Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docs: re: where_object_changes #801

Merged
merged 1 commit into from
May 11, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,13 +226,11 @@ version.index
# Returns the event that caused this version (create|update|destroy).
version.event

# Query versions objects by attributes.
# Query the `versions.object` column (or `object_changes` column), by
# attributes, using the SQL LIKE operator. Known issue: inconsistent results for
# numeric values due to limitations of SQL wildcard matchers against the
# serialized objects.
PaperTrail::Version.where_object(attr1: val1, attr2: val2)

# Query versions object_changes field by attributes (requires
# `object_changes` column on versions table).
# Also can't guarantee consistent query results for numeric values
# due to limitations of SQL wildcard matchers against the serialized objects.
PaperTrail::Version.where_object_changes(attr1: val1)
```

Expand Down
8 changes: 4 additions & 4 deletions lib/paper_trail/serializers/json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ def dump(object)
ActiveSupport::JSON.encode object
end

# Returns a SQL condition to be used to match the given field and value
# in the serialized object
# Returns a SQL LIKE condition to be used to match the given field and
# value in the serialized object.
def where_object_condition(arel_field, field, value)
# Convert to JSON to handle strings and nulls correctly.
json_value = value.to_json
Expand All @@ -32,8 +32,8 @@ def where_object_condition(arel_field, field, value)
end
end

# Returns a SQL condition to be used to match the given field and value
# in the serialized object_changes
# Returns a SQL LIKE condition to be used to match the given field and
# value in the serialized `object_changes`.
def where_object_changes_condition(arel_field, field, value)
# Convert to JSON to handle strings and nulls correctly.
json_value = value.to_json
Expand Down
8 changes: 4 additions & 4 deletions lib/paper_trail/serializers/yaml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ def dump(object)
::YAML.dump object
end

# Returns a SQL condition to be used to match the given field and value
# in the serialized object
# Returns a SQL LIKE condition to be used to match the given field and
# value in the serialized object.
def where_object_condition(arel_field, field, value)
arel_field.matches("%\n#{field}: #{value}\n%")
end

# Returns a SQL condition to be used to match the given field and value
# in the serialized object_changes
# Returns a SQL LIKE condition to be used to match the given field and
# value in the serialized `object_changes`.
def where_object_changes_condition(arel_field, field, value)
# Need to check first (before) and secondary (after) fields
m1 = nil
Expand Down
5 changes: 5 additions & 0 deletions lib/paper_trail/version_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,10 @@ def timestamp_sort_order(direction = "asc")
end
end

# Query the `versions.objects` column using the SQL LIKE operator.
# Performs an attribute search on the serialized object by invoking the
# identically-named method in the serializer being used.
# @api public
def where_object(args = {})
raise ArgumentError, "expected to receive a Hash" unless args.is_a?(Hash)

Expand All @@ -135,6 +137,9 @@ def where_object(args = {})
end
end

# Query the `versions.object_changes` column by attributes, using the
# SQL LIKE operator.
# @api public
def where_object_changes(args = {})
raise ArgumentError, "expected to receive a Hash" unless args.is_a?(Hash)

Expand Down