Skip to content

Commit

Permalink
Use the whole range of signed integers on MEDIUMINT
Browse files Browse the repository at this point in the history
  • Loading branch information
mixonic committed Apr 3, 2011
1 parent aff78e6 commit 73360b7
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
1 change: 1 addition & 0 deletions lib/ranked-model.rb
Expand Up @@ -6,6 +6,7 @@ module RankedModel
# Signed MEDIUMINT in MySQL
#
MAX_RANK_VALUE = 8388607
MIN_RANK_VALUE = -8388607

def self.included base

Expand Down
8 changes: 4 additions & 4 deletions lib/ranked-model/ranker.rb
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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) ).
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions spec/duck-model/lots_of_ducks_spec.rb
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 73360b7

Please sign in to comment.