Skip to content
Browse files

Do not scope the rearrangement of ranks to exclude the current record…

…'s ID unless the current record is saved.
  • Loading branch information...
1 parent 125f974 commit 8aa4f172af849f37d9a340a653b0188fb86aa5b6 @mixonic committed
Showing with 8 additions and 6 deletions.
  1. +8 −6 lib/ranked-model/ranker.rb
View
14 lib/ranked-model/ranker.rb
@@ -137,19 +137,21 @@ def assure_unique_position
end
def rearrange_ranks
+ _scope = finder
+ unless instance.id.nil?
+ # Never update ourself, shift others around us.
+ _scope = _scope.where( instance.class.arel_table[:id].not_eq(instance.id) )
+ end
if current_first.rank && current_first.rank > RankedModel::MIN_RANK_VALUE && rank == RankedModel::MAX_RANK_VALUE
- finder.
- where( instance.class.arel_table[:id].not_eq(instance.id) ).
+ _scope.
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
- finder.
- where( instance.class.arel_table[:id].not_eq(instance.id) ).
+ _scope.
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
- finder.
- where( instance.class.arel_table[:id].not_eq(instance.id) ).
+ _scope.
where( instance.class.arel_table[ranker.column].lt(rank) ).
update_all( "#{ranker.column} = #{ranker.column} - 1" )
rank_at( rank - 1 )

0 comments on commit 8aa4f17

Please sign in to comment.
Something went wrong with that request. Please try again.