Permalink
Browse files

Merge pull request #11 from robotex82/master

Added possibility to pass first, middle and last position as string.
  • Loading branch information...
2 parents b8ea21d + 6285d3e commit 78c6551bd320a12fbc4ebcdc7a70c7a85c6fabf9 Matthew Beale committed Oct 4, 2011
Showing with 44 additions and 3 deletions.
  1. +3 −3 lib/ranked-model/ranker.rb
  2. +41 −0 spec/duck-model/duck_spec.rb
@@ -94,19 +94,19 @@ def new_record?
def update_index_from_position
case position
- when :first
+ when :first, 'first'
if current_first && current_first.rank
rank_at( ( ( RankedModel::MIN_RANK_VALUE - current_first.rank ).to_f / 2 ).ceil + current_first.rank)
else
position_at :middle
end
- when :last
+ when :last, 'last'
if current_last && current_last.rank
rank_at( ( ( RankedModel::MAX_RANK_VALUE - current_last.rank ).to_f / 2 ).ceil + current_last.rank )
else
position_at :middle
end
- when :middle
+ when :middle, 'middle'
rank_at( ( ( RankedModel::MAX_RANK_VALUE - RankedModel::MIN_RANK_VALUE ).to_f / 2 ).ceil + RankedModel::MIN_RANK_VALUE )
when String
position_at position.to_i
@@ -286,6 +286,47 @@
}
end
+
+ describe "at the end with string" do
+
+ before {
+ @ordered = Duck.rank(:row).where(Duck.arel_table[:id].not_eq @ducks[:wingy].id).collect {|duck| duck.id }
+ @ducks[:wingy].update_attribute :row_position, 'last'
+ }
+
+ context {
+
+ subject { Duck.ranker(:row).with(Duck.new).current_at_position(@ducks.size - 1).instance }
+
+ its(:id) { should == @ducks[:wingy].id }
+
+ }
+
+ context {
+
+ subject { Duck.rank(:row).last }
+
+ its(:id) { should == @ducks[:wingy].id }
+
+ }
+
+ context {
+
+ subject { Duck.ranker(:row).with(Duck.new).instance_eval { current_last }.instance }
+
+ its(:id) { should == @ducks[:wingy].id }
+
+ }
+
+ context {
+
+ subject { Duck.rank(:row).collect {|duck| duck.id } }
+
+ it { subject[0..-2].should == @ordered }
+
+ }
+
+ end
end

0 comments on commit 78c6551

Please sign in to comment.