Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

assinging nil to an offset will remove the offset from the AST

  • Loading branch information...
commit 2644bcec7dbe3a65277b3a6a141853484171535a 1 parent abffef9
@tenderlove tenderlove authored
View
3  History.txt
@@ -7,11 +7,14 @@
* SQL Literals may be used as Attribute names
* Added Arel::Nodes::NamedFunction for representing generic SQL functions
* Add Arel::SelectManager#limit=
+ * Add Arel::SelectManager#offset
+ * Add Arel::SelectManager#offset=
* Bug fixes
* MSSQL adds TOP to sub selects
* Assigning nil to take() removes LIMIT from statement.
+ * Assigning nil to offset() removes OFFSET from statement.
* Deprecations
View
11 lib/arel/select_manager.rb
@@ -18,10 +18,19 @@ def constraints
@ctx.wheres
end
+ def offset
+ @ast.offset && @ast.offset.expr
+ end
+
def skip amount
- @ast.offset = Nodes::Offset.new(amount)
+ if amount
+ @ast.offset = Nodes::Offset.new(amount)
+ else
+ @ast.offset = nil
+ end
self
end
+ alias :offset= :skip
###
# Produces an Arel::Nodes::Exists node
View
26 test/test_select_manager.rb
@@ -180,6 +180,32 @@ def test_join_sources
end
end
+ describe 'offset' do
+ it 'should add an offset' do
+ table = Table.new :users
+ mgr = table.from table
+ mgr.offset = 10
+ mgr.to_sql.must_be_like %{ SELECT FROM "users" OFFSET 10 }
+ end
+
+ it 'should remove an offset' do
+ table = Table.new :users
+ mgr = table.from table
+ mgr.offset = 10
+ mgr.to_sql.must_be_like %{ SELECT FROM "users" OFFSET 10 }
+
+ mgr.offset = nil
+ mgr.to_sql.must_be_like %{ SELECT FROM "users" }
+ end
+
+ it 'should return the offset' do
+ table = Table.new :users
+ mgr = table.from table
+ mgr.offset = 10
+ assert_equal 10, mgr.offset
+ end
+ end
+
describe 'exists' do
it 'should create an exists clause' do
table = Table.new(:users)
Please sign in to comment.
Something went wrong with that request. Please try again.