Skip to content

Commit

Permalink
Do not scope the rearrangement of ranks to exclude the current record…
Browse files Browse the repository at this point in the history
…'s ID unless the current record is saved.
  • Loading branch information
mixonic committed Jun 25, 2012
1 parent 125f974 commit 8aa4f17
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions lib/ranked-model/ranker.rb
Expand Up @@ -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 )
Expand Down

0 comments on commit 8aa4f17

Please sign in to comment.