Skip to content

Commit

Permalink
If a rank value is the max value, treat it as 'last' and shit everybo…
Browse files Browse the repository at this point in the history
…dy out of the way
  • Loading branch information
mixonic committed Jun 15, 2011
1 parent beb8070 commit 0977966
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
11 changes: 8 additions & 3 deletions lib/ranked-model/ranker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,16 +128,21 @@ def assure_unique_position
end

def rearrange_ranks
if current_last.rank < (RankedModel::MAX_RANK_VALUE - 1) && rank < current_last.rank
if current_first.rank > RankedModel::MIN_RANK_VALUE && rank == RankedModel::MAX_RANK_VALUE
instance.class.
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 < (RankedModel::MAX_RANK_VALUE - 1) && rank < current_last.rank
instance.class.
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 > RankedModel::MIN_RANK_VALUE && rank > current_first.rank
elsif current_first.rank > RankedModel::MIN_RANK_VALUE + 1 && rank > current_first.rank
instance.class.
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" )
update_all( "#{ranker.column} = #{ranker.column} - 2" )
rank_at( rank - 1 )
else
rebalance_ranks
Expand Down
28 changes: 27 additions & 1 deletion spec/duck-model/lots_of_ducks_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,32 @@

end

describe 'last' do

before {
@last = Duck.last
@last.update_attribute :row_position, :last
}

subject { Duck.rank(:row).last }

its(:id) { should == @last.id }

end

describe 'first' do

before {
@last = Duck.last
@last.update_attribute :row_position, :first
}

subject { Duck.rank(:row).first }

its(:id) { should == @last.id }

end

end

describe "a rearrangement" do
Expand All @@ -55,7 +81,7 @@

subject { Duck.rank(:row).collect {|d| d.id } }

it { should == (@ordered[0..-2] + [@first.id, @second.id, @ordered[-1]]) }
it { should == (@ordered[0..-2] + [@ordered[-1], @first.id, @second.id]) }

}

Expand Down

0 comments on commit 0977966

Please sign in to comment.