diff options
-rw-r--r-- | lib/arel/algebra/relations/operations/take.rb | 3 | ||||
-rw-r--r-- | spec/shared/relation_spec.rb | 27 |
2 files changed, 29 insertions, 1 deletions
diff --git a/lib/arel/algebra/relations/operations/take.rb b/lib/arel/algebra/relations/operations/take.rb index eb32ec492e..cc636642b1 100644 --- a/lib/arel/algebra/relations/operations/take.rb +++ b/lib/arel/algebra/relations/operations/take.rb @@ -1,7 +1,8 @@ module Arel class Take < Compound attributes :relation, :taken - deriving :initialize, :== + deriving :initialize, :== + requires :limiting def externalizable? true diff --git a/spec/shared/relation_spec.rb b/spec/shared/relation_spec.rb index c492ca847e..aa03ab6c0a 100644 --- a/spec/shared/relation_spec.rb +++ b/spec/shared/relation_spec.rb @@ -14,6 +14,10 @@ share_examples_for 'A Relation' do raise "@expected needs to have at least 6 items" unless @expected.length >= 6 end + before :each do + @expected = @expected.dup + end + describe "#each" do it "iterates over the rows in any order" do @relation.should have_rows(@expected) @@ -89,4 +93,27 @@ share_examples_for 'A Relation' do it "works" end end + + describe "#take" do + it "returns a relation" do + @relation.take(3).should be_a(Arel::Relation) + end + + it "returns X items from the collection" do + length = @expected.length + + @relation.take(3).each do |resource| + @expected.delete_if { |r| r.tuple == resource.tuple } + end + + @expected.length.should == length - 3 + end + + it "works with ordering" do + expected = @expected.sort_by { |r| [r[@relation[:age]], r[@relation[:id]]] }.map { |r| r[@relation[:id]] } + actual = @relation.order(@relation[:age].asc, @relation[:id].asc).take(3).map { |r| r[@relation[:id]] } + + actual.should == expected[0,3] + end + end end
\ No newline at end of file |