-
Notifications
You must be signed in to change notification settings - Fork 895
Commit
…rn if primary key is an integer. This makes some of the changes to these scope methods that came from 6a4aba2 more flexible, in that the user can choose to compare timestamps if desired, but it defaults to comparing and sorting, via the primary key (if it is an integer). If the primary key is not an integer, it still defaults to using the PaperTrail.timestamp_field. This is my proposed fix for #314, and I also believe it should fix #317. It seems that that this issue is usually encountered when testing PaperTrail with MySQL (presumably due to lack of microsecond timestamp support).
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,22 +33,43 @@ def not_creates | |
where 'event <> ?', 'create' | ||
end | ||
|
||
# These methods accept a timestamp or a version and returns other versions that come before or after | ||
def subsequent(obj) | ||
# Expects `obj` to be an instance of `PaperTrail::Version` by default, but can accept a timestamp if | ||
# `timestamp_arg` receives `true` | ||
def subsequent(obj, timestamp_arg = false) | ||
if timestamp_arg != true && self.primary_key_is_int? | ||
return where("#{table_name}.#{self.primary_key} > ?", obj).order("#{table_name}.#{self.primary_key} ASC") | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
batter
Author
Collaborator
|
||
end | ||
|
||
obj = obj.send(PaperTrail.timestamp_field) if obj.is_a?(self) | ||
where("#{table_name}.#{PaperTrail.timestamp_field} > ?", obj). | ||
order("#{table_name}.#{PaperTrail.timestamp_field} ASC") | ||
order(self.timestamp_sort_order) | ||
end | ||
|
||
def preceding(obj) | ||
def preceding(obj, timestamp_arg = false) | ||
if timestamp_arg != true && self.primary_key_is_int? | ||
return where("#{table_name}.#{self.primary_key} < ?", obj).order("#{table_name}.#{self.primary_key} DESC") | ||
end | ||
|
||
obj = obj.send(PaperTrail.timestamp_field) if obj.is_a?(self) | ||
where("#{table_name}.#{PaperTrail.timestamp_field} < ?", obj). | ||
order("#{table_name}.#{PaperTrail.timestamp_field} DESC") | ||
order(self.timestamp_sort_order('DESC')) | ||
end | ||
|
||
|
||
def between(start_time, end_time) | ||
where("#{table_name}.#{PaperTrail.timestamp_field} > ? AND #{table_name}.#{PaperTrail.timestamp_field} < ?", | ||
start_time, end_time).order("#{table_name}.#{PaperTrail.timestamp_field} ASC") | ||
start_time, end_time).order(self.timestamp_sort_order) | ||
end | ||
|
||
# defaults to using the primary key as the secondary sort order if possible | ||
def timestamp_sort_order(order = 'ASC') | ||
self.primary_key_is_int? ? | ||
This comment has been minimized.
Sorry, something went wrong.
rposborne
Contributor
|
||
"#{table_name}.#{PaperTrail.timestamp_field} #{order}, #{table_name}.#{self.primary_key} #{order}" : | ||
"#{table_name}.#{PaperTrail.timestamp_field} #{order}" | ||
end | ||
|
||
def primary_key_is_int? | ||
@primary_key_is_int ||= columns_hash[primary_key].type == :integer | ||
end | ||
|
||
# Returns whether the `object` column is using the `json` type supported by PostgreSQL | ||
|
What would be the use case to ignore the timestamp? Or when would this return something different then the timestamp + id?
Seems like it might just be extra code.