diff options
-rw-r--r-- | History.txt | 2 | ||||
-rw-r--r-- | lib/arel/select_manager.rb | 10 | ||||
-rw-r--r-- | test/test_select_manager.rb | 9 |
3 files changed, 19 insertions, 2 deletions
diff --git a/History.txt b/History.txt index aa5bbd77e2..a931afd0ef 100644 --- a/History.txt +++ b/History.txt @@ -6,10 +6,12 @@ * AND nodes are now n-ary nodes * SQL Literals may be used as Attribute names * Added Arel::Nodes::NamedFunction for representing generic SQL functions + * Add Arel::SelectManager#limit= * Bug fixes * MSSQL adds TOP to sub selects + * Assigning nil to take() removes LIMIT from statement. * Deprecations diff --git a/lib/arel/select_manager.rb b/lib/arel/select_manager.rb index adffcca764..37405b4596 100644 --- a/lib/arel/select_manager.rb +++ b/lib/arel/select_manager.rb @@ -172,10 +172,16 @@ module Arel end def take limit - @ast.limit = Nodes::Limit.new(limit) - @ctx.top = Nodes::Top.new(limit) + if limit + @ast.limit = Nodes::Limit.new(limit) + @ctx.top = Nodes::Top.new(limit) + else + @ast.limit = nil + @ctx.top = nil + end self end + alias limit= take def join_sql return nil if @ctx.source.right.empty? diff --git a/test/test_select_manager.rb b/test/test_select_manager.rb index 4b4733db97..efea44d56f 100644 --- a/test/test_select_manager.rb +++ b/test/test_select_manager.rb @@ -743,6 +743,15 @@ module Arel manager = Arel::SelectManager.new Table.engine manager.take(1).must_equal manager end + + it 'removes LIMIT when nil is passed' do + manager = Arel::SelectManager.new Table.engine + manager.limit = 10 + assert_match('LIMIT', manager.to_sql) + + manager.limit = nil + refute_match('LIMIT', manager.to_sql) + end end describe 'where' do |