Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

fix case where all results are desired

  • Loading branch information...
commit 839b4bcb8c380fc7129e2bd5ca51e06b6d8d7086 1 parent 644abc9
@jasondew authored
View
2  Gemfile
@@ -1,5 +1,3 @@
source "http://rubygems.org"
-gem "will_paginate"
-
gemspec
View
2  Gemfile.lock
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
- data_table (0.1.2)
+ data_table (0.1.6)
rails (~> 3.0.0)
will_paginate (~> 3.0.pre2)
View
2  lib/data_table/active_record.rb
@@ -4,7 +4,7 @@ module ClassMethods
def _find_objects params, fields, search_fields
self.where(_where_conditions params[:sSearch], search_fields).
order(_order_fields params, fields).
- paginate :page => _page(params), :per_page => params[:iDisplayLength]
+ paginate :page => _page(params), :per_page => _per_page(params)
end
def _where_conditions query, search_fields
View
8 lib/data_table/base.rb
@@ -37,6 +37,14 @@ def _yield_and_render_array controller, objects, block
def _page params
params[:iDisplayStart].to_i / params[:iDisplayLength].to_i + 1
end
+
+ def _per_page params
+ case (display_length = params[:iDisplayLength].to_i)
+ when -1 then self.count
+ when 0 then 25
+ else display_length
+ end
+ end
end
end
View
2  lib/data_table/mongoid.rb
@@ -4,7 +4,7 @@ module ClassMethods
def _find_objects params, fields, search_fields
self.where(_where_conditions params[:sSearch], search_fields).
order_by(_order_by_fields params, fields).
- paginate :page => _page(params), :per_page => params[:iDisplayLength]
+ paginate :page => _page(params), :per_page => _per_page(params)
end
def _where_conditions raw_query, search_fields
View
3  spec/active_record_data_table_spec.rb
@@ -12,8 +12,9 @@
mock(self)._where_conditions("answer", %w(foo bar)) { "where clause" }
mock(self)._order_fields(params, %w(foo bar baz)) { "order" }
- mock(self).where("where clause") { mock!.order("order") { mock!.paginate({:page => :page, :per_page => 10}) { :answer } } }
+ mock(self).where("where clause") { mock!.order("order") { mock!.paginate({:page => :page, :per_page => :per_page}) { :answer } } }
mock(self)._page(params) { :page }
+ mock(self)._per_page(params) { :per_page }
_find_objects(params, %w(foo bar baz), %w(foo bar)).should == :answer
end
View
17 spec/data_table_spec.rb
@@ -63,4 +63,21 @@
end
+ context "#_per_page" do
+
+ it "should return 10 given an iDisplayLength of 10" do
+ send(:_per_page, {:iDisplayLength => "10"}).should == 10
+ end
+
+ it "should return a default of 25 given an invalid iDisplayLength" do
+ send(:_per_page, {:iDisplayLength => "foobar"}).should == 25
+ end
+
+ it "should return self.count given an iDisplayLength of -1" do
+ mock(self).count { :all }
+ send(:_per_page, {:iDisplayLength => "-1"}).should == :all
+ end
+
+ end
+
end
View
3  spec/mongoid_data_table_spec.rb
@@ -11,8 +11,9 @@
mock(self)._where_conditions("answer", %w(foo bar)) { "where clause" }
mock(self)._order_by_fields(params, %w(foo bar baz)) { "order by" }
- mock(self).where("where clause") { mock!.order_by("order by") { mock!.paginate({:page => :page, :per_page => 10}) { :answer } } }
+ mock(self).where("where clause") { mock!.order_by("order by") { mock!.paginate({:page => :page, :per_page => :per_page}) { :answer } } }
mock(self)._page(params) { :page }
+ mock(self)._per_page(params) { :per_page }
_find_objects(params, %w(foo bar baz), %w(foo bar)).should == :answer
end
Please sign in to comment.
Something went wrong with that request. Please try again.