:versions association should default to sorting by the VersionConcern#timestamp_order method to match the scope methods

This commit is contained in:
Ben Atkins 2014-05-09 11:01:33 -04:00
parent 1a13748ab1
commit 0b05a58763
3 changed files with 11 additions and 11 deletions

View File

@ -61,13 +61,13 @@ module PaperTrail
if ::ActiveRecord::VERSION::MAJOR >= 4 # `has_many` syntax for specifying order uses a lambda in Rails 4
has_many self.versions_association_name,
lambda { order("#{PaperTrail.timestamp_field} ASC") },
lambda { |model| order(model.version_class_name.constantize.timestamp_sort_order) },
:class_name => self.version_class_name, :as => :item
else
has_many self.versions_association_name,
:class_name => self.version_class_name,
:as => :item,
:order => "#{PaperTrail.timestamp_field} ASC"
:order => self.paper_trail_version_class.timestamp_sort_order
end
options_on = Array(options[:on]) # so that a single symbol can be passed in without wrapping it in an `Array`

View File

@ -41,8 +41,7 @@ module PaperTrail
end
obj = obj.send(PaperTrail.timestamp_field) if obj.is_a?(self)
where("#{table_name}.#{PaperTrail.timestamp_field} > ?", obj).
order(self.timestamp_sort_order)
where("#{table_name}.#{PaperTrail.timestamp_field} > ?", obj).order(self.timestamp_sort_order)
end
def preceding(obj, timestamp_arg = false)
@ -51,8 +50,7 @@ module PaperTrail
end
obj = obj.send(PaperTrail.timestamp_field) if obj.is_a?(self)
where("#{table_name}.#{PaperTrail.timestamp_field} < ?", obj).
order(self.timestamp_sort_order('DESC'))
where("#{table_name}.#{PaperTrail.timestamp_field} < ?", obj).order(self.timestamp_sort_order('DESC'))
end
@ -63,9 +61,11 @@ module PaperTrail
# defaults to using the primary key as the secondary sort order if possible
def timestamp_sort_order(order = 'ASC')
self.primary_key_is_int? ?
"#{table_name}.#{PaperTrail.timestamp_field} #{order}, #{table_name}.#{self.primary_key} #{order}" :
if self.primary_key_is_int?
"#{table_name}.#{PaperTrail.timestamp_field} #{order}, #{table_name}.#{self.primary_key} #{order}"
else
"#{table_name}.#{PaperTrail.timestamp_field} #{order}"
end
end
def primary_key_is_int?

View File

@ -65,7 +65,7 @@ class PaperTrail::VersionTest < ActiveSupport::TestCase
setup { 2.times { @animal.update_attributes(:name => Faker::Lorem.word) } }
context "receiving a TimeStamp" do
should "return all versions that were created before the Timestamp; descendingly by order of the `PaperTrail.timestamp_field`" do
should "return all versions that were created before the Timestamp" do
value = PaperTrail::Version.subsequent(1.hour.ago, true)
assert_equal value, @animal.versions.to_a
assert_not_nil value.to_sql.match(/ORDER BY versions.created_at ASC/)
@ -84,8 +84,8 @@ class PaperTrail::VersionTest < ActiveSupport::TestCase
setup { 2.times { @animal.update_attributes(:name => Faker::Lorem.word) } }
context "receiving a TimeStamp" do
should "return all versions that were created before the Timestamp; descendingly by order of the `PaperTrail.timestamp_field`" do
value = PaperTrail::Version.preceding(Time.now, true)
should "return all versions that were created before the Timestamp" do
value = PaperTrail::Version.preceding(5.seconds.from_now, true)
assert_equal value, @animal.versions.reverse
assert_not_nil value.to_sql.match(/ORDER BY versions.created_at DESC/)
end