Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

A similar fix to the bug found in #23 from @quake - rearrange ranks n…

…eeds to use finder
  • Loading branch information...
commit 125f9742ecaa096eaee85e10b7714c9b8b53a033 1 parent 43ba9b5
@mixonic authored
View
2  lib/ranked-model.rb
@@ -18,7 +18,7 @@ def self.included base
before_save :handle_ranking
scope :rank, lambda { |name|
- order arel_table[ ranker(name.to_sym).column ]
+ order ranker(name.to_sym).column
}
end
View
6 lib/ranked-model/ranker.rb
@@ -138,17 +138,17 @@ def assure_unique_position
def rearrange_ranks
if current_first.rank && current_first.rank > RankedModel::MIN_RANK_VALUE && rank == RankedModel::MAX_RANK_VALUE
- instance.class.
+ finder.
where( instance.class.arel_table[:id].not_eq(instance.id) ).
where( instance.class.arel_table[ranker.column].lteq(rank) ).
update_all( "#{ranker.column} = #{ranker.column} - 1" )
elsif current_last.rank && current_last.rank < (RankedModel::MAX_RANK_VALUE - 1) && rank < current_last.rank
- instance.class.
+ finder.
where( instance.class.arel_table[:id].not_eq(instance.id) ).
where( instance.class.arel_table[ranker.column].gteq(rank) ).
update_all( "#{ranker.column} = #{ranker.column} + 1" )
elsif current_first.rank && current_first.rank > RankedModel::MIN_RANK_VALUE && rank > current_first.rank
- instance.class.
+ finder.
where( instance.class.arel_table[:id].not_eq(instance.id) ).
where( instance.class.arel_table[ranker.column].lt(rank) ).
update_all( "#{ranker.column} = #{ranker.column} - 1" )
View
26 spec/duck-model/lots_of_ducks_spec.rb
@@ -87,6 +87,32 @@
end
+ describe "with max value and with_same pond" do
+
+ before {
+ Duck.first(50).each_with_index do |d, index|
+ d.update_attributes :age => index % 10, :pond => "Pond #{index / 10}"
+ end
+ @duck_11 = Duck.offset(10).first
+ @duck_12 = Duck.offset(11).first
+ @ordered = Duck.where(:pond => 'Pond 1').rank(:age).where(Duck.arel_table[:id].not_in([@duck_11.id, @duck_12.id])).collect {|d| d.id }
+ @duck_11.update_attribute :age, RankedModel::MAX_RANK_VALUE
+ @duck_12.update_attribute :age, RankedModel::MAX_RANK_VALUE
+ }
+
+ context {
+ subject { Duck.where(:pond => 'Pond 1').rank(:age).collect {|d| d.id } }
+
+ it { should == (@ordered[0..-2] + [@ordered[-1], @duck_11.id, @duck_12.id]) }
+ }
+
+ context {
+ subject { Duck.first.age }
+ it { should == 0}
+ }
+
+ end
+
describe "with min value" do
before {
Please sign in to comment.
Something went wrong with that request. Please try again.