diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2011-02-25 15:23:21 -0800 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2011-02-25 15:23:21 -0800 |
commit | 2644bcec7dbe3a65277b3a6a141853484171535a (patch) | |
tree | f5e2d4ba4afd0558a05ed3c41cf2642d1bbcd1ad | |
parent | abffef945a64a32ac959ee11d41593d575bcccab (diff) | |
download | rails-2644bcec7dbe3a65277b3a6a141853484171535a.tar.gz rails-2644bcec7dbe3a65277b3a6a141853484171535a.tar.bz2 rails-2644bcec7dbe3a65277b3a6a141853484171535a.zip |
assinging nil to an offset will remove the offset from the AST
-rw-r--r-- | History.txt | 3 | ||||
-rw-r--r-- | lib/arel/select_manager.rb | 11 | ||||
-rw-r--r-- | test/test_select_manager.rb | 26 |
3 files changed, 39 insertions, 1 deletions
diff --git a/History.txt b/History.txt index a931afd0ef..fac98381a6 100644 --- a/History.txt +++ b/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 diff --git a/lib/arel/select_manager.rb b/lib/arel/select_manager.rb index 37405b4596..7f533fa91b 100644 --- a/lib/arel/select_manager.rb +++ b/lib/arel/select_manager.rb @@ -18,10 +18,19 @@ module Arel @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 diff --git a/test/test_select_manager.rb b/test/test_select_manager.rb index efea44d56f..2fe43aa982 100644 --- a/test/test_select_manager.rb +++ b/test/test_select_manager.rb @@ -180,6 +180,32 @@ module Arel 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) |