From 73360b7d07c52ecd50c5f4a5595b6e43e31c7912 Mon Sep 17 00:00:00 2001 From: Matthew Beale Date: Sun, 3 Apr 2011 09:23:22 -0400 Subject: [PATCH] Use the whole range of signed integers on MEDIUMINT --- lib/ranked-model.rb | 1 + lib/ranked-model/ranker.rb | 8 ++++---- spec/duck-model/lots_of_ducks_spec.rb | 6 +++--- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/lib/ranked-model.rb b/lib/ranked-model.rb index f6a0383..b46ae2c 100644 --- a/lib/ranked-model.rb +++ b/lib/ranked-model.rb @@ -6,6 +6,7 @@ module RankedModel # Signed MEDIUMINT in MySQL # MAX_RANK_VALUE = 8388607 + MIN_RANK_VALUE = -8388607 def self.included base diff --git a/lib/ranked-model/ranker.rb b/lib/ranked-model/ranker.rb index 53bc5eb..85a92fa 100644 --- a/lib/ranked-model/ranker.rb +++ b/lib/ranked-model/ranker.rb @@ -87,7 +87,7 @@ def update_index_from_position case position when :first if current_first && current_first.rank - rank_at( ( current_first.rank.to_f / 2 ).ceil ) + rank_at( ( RankedModel::MIN_RANK_VALUE + current_first.rank.to_f / 2 ).ceil ) else position_at :middle end @@ -107,7 +107,7 @@ def update_index_from_position current = current_at_position(position) if current previous = position > 0 ? current_at_position(position-1) : nil - rank_at( ( ( (previous ? previous.rank : 0) + current.rank ).to_f / 2 ).ceil ) + rank_at( ( ( (previous ? previous.rank : RankedModel::MIN_RANK_VALUE) + current.rank ).to_f / 2 ).ceil ) else position_at :last end @@ -136,7 +136,7 @@ def rearrange_ranks 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 > 0 && rank > current_first.rank + elsif current_first.rank > RankedModel::MIN_RANK_VALUE && 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) ). @@ -152,7 +152,7 @@ def rebalance_ranks has_set_self = false total.times do |index| next if index == 0 || index == total - rank_value = RankedModel::MAX_RANK_VALUE / total * index + rank_value = ((((RankedModel::MAX_RANK_VALUE - RankedModel::MIN_RANK_VALUE).to_f / total) * index ).ceil + RankedModel::MIN_RANK_VALUE) index = index - 1 if has_set_self index = index - 1 diff --git a/spec/duck-model/lots_of_ducks_spec.rb b/spec/duck-model/lots_of_ducks_spec.rb index ab6acd2..865c4c8 100644 --- a/spec/duck-model/lots_of_ducks_spec.rb +++ b/spec/duck-model/lots_of_ducks_spec.rb @@ -67,8 +67,8 @@ @first = Duck.first @second = Duck.offset(1).first @ordered = Duck.rank(:row).where(Duck.arel_table[:id].not_in([@first.id, @second.id])).collect {|d| d.id } - @first.update_attribute :row, 0 - @second.update_attribute :row, 0 + @first.update_attribute :row, RankedModel::MIN_RANK_VALUE + @second.update_attribute :row, RankedModel::MIN_RANK_VALUE } context { @@ -96,7 +96,7 @@ where(Duck.arel_table[:id].not_in([@first.id, @second.id, @third.id, @fourth.id])). where(Duck.arel_table[:row].gteq(RankedModel::MAX_RANK_VALUE / 2)). collect {|d| d.id } - @first.update_attribute :row, 0 + @first.update_attribute :row, RankedModel::MIN_RANK_VALUE @second.update_attribute :row, RankedModel::MAX_RANK_VALUE @third.update_attribute :row, (RankedModel::MAX_RANK_VALUE / 2) @fourth.update_attribute :row, @third.row