Permalink
Browse files

using kaminari (or equivalent API) for mongoid

  • Loading branch information...
1 parent c9d636f commit 74872229a60bf12e59e5ff229242cd402cdda860 @jasondew committed Sep 5, 2011
Showing with 80 additions and 46 deletions.
  1. +41 −40 Gemfile.lock
  2. +8 −0 README.rdoc
  3. +0 −1 data_table.gemspec
  4. +2 −1 lib/data_table/base.rb
  5. +6 −2 lib/data_table/mongoid.rb
  6. +21 −0 spec/data_table_spec.rb
  7. +2 −2 spec/mongoid_data_table_spec.rb
View
@@ -1,72 +1,73 @@
PATH
remote: .
specs:
- data_table (0.2.0)
+ data_table (0.2.7)
rails (~> 3.0.0)
- will_paginate (~> 3.0.pre2)
GEM
remote: http://rubygems.org/
specs:
abstract (1.0.0)
- actionmailer (3.0.5)
- actionpack (= 3.0.5)
- mail (~> 2.2.15)
- actionpack (3.0.5)
- activemodel (= 3.0.5)
- activesupport (= 3.0.5)
+ actionmailer (3.0.10)
+ actionpack (= 3.0.10)
+ mail (~> 2.2.19)
+ actionpack (3.0.10)
+ activemodel (= 3.0.10)
+ activesupport (= 3.0.10)
builder (~> 2.1.2)
erubis (~> 2.6.6)
- i18n (~> 0.4)
+ i18n (~> 0.5.0)
rack (~> 1.2.1)
- rack-mount (~> 0.6.13)
+ rack-mount (~> 0.6.14)
rack-test (~> 0.5.7)
tzinfo (~> 0.3.23)
- activemodel (3.0.5)
- activesupport (= 3.0.5)
+ activemodel (3.0.10)
+ activesupport (= 3.0.10)
builder (~> 2.1.2)
- i18n (~> 0.4)
- activerecord (3.0.5)
- activemodel (= 3.0.5)
- activesupport (= 3.0.5)
- arel (~> 2.0.2)
+ i18n (~> 0.5.0)
+ activerecord (3.0.10)
+ activemodel (= 3.0.10)
+ activesupport (= 3.0.10)
+ arel (~> 2.0.10)
tzinfo (~> 0.3.23)
- activeresource (3.0.5)
- activemodel (= 3.0.5)
- activesupport (= 3.0.5)
- activesupport (3.0.5)
- arel (2.0.9)
+ activeresource (3.0.10)
+ activemodel (= 3.0.10)
+ activesupport (= 3.0.10)
+ activesupport (3.0.10)
+ arel (2.0.10)
builder (2.1.2)
diff-lcs (1.1.2)
erubis (2.6.6)
abstract (>= 1.0.0)
i18n (0.5.0)
- mail (2.2.17)
+ mail (2.2.19)
activesupport (>= 2.3.6)
i18n (>= 0.4.0)
mime-types (~> 1.16)
treetop (~> 1.4.8)
mime-types (1.16)
- polyglot (0.3.1)
- rack (1.2.2)
- rack-mount (0.6.13)
+ polyglot (0.3.2)
+ rack (1.2.3)
+ rack-mount (0.6.14)
rack (>= 1.0.0)
rack-test (0.5.7)
rack (>= 1.0)
- rails (3.0.5)
- actionmailer (= 3.0.5)
- actionpack (= 3.0.5)
- activerecord (= 3.0.5)
- activeresource (= 3.0.5)
- activesupport (= 3.0.5)
+ rails (3.0.10)
+ actionmailer (= 3.0.10)
+ actionpack (= 3.0.10)
+ activerecord (= 3.0.10)
+ activeresource (= 3.0.10)
+ activesupport (= 3.0.10)
bundler (~> 1.0)
- railties (= 3.0.5)
- railties (3.0.5)
- actionpack (= 3.0.5)
- activesupport (= 3.0.5)
+ railties (= 3.0.10)
+ railties (3.0.10)
+ actionpack (= 3.0.10)
+ activesupport (= 3.0.10)
rake (>= 0.8.7)
+ rdoc (~> 3.4)
thor (~> 0.14.4)
- rake (0.8.7)
+ rake (0.9.2)
+ rdoc (3.9.4)
rr (1.0.0)
rspec (2.0.1)
rspec-core (~> 2.0.1)
@@ -80,10 +81,10 @@ GEM
rspec-expectations (~> 2.0.1)
shoulda (2.11.3)
thor (0.14.6)
- treetop (1.4.9)
+ treetop (1.4.10)
+ polyglot
polyglot (>= 0.3.1)
- tzinfo (0.3.26)
- will_paginate (3.0.pre2)
+ tzinfo (0.3.29)
PLATFORMS
ruby
View
@@ -47,6 +47,14 @@ in your view (assuming HAML):
%tbody
+and in your Gemfile
+
+ # for ActiveRecord
+ gem "will_paginate"
+
+ # for Mongoid
+ gem "kaminari"
+
== Advanced Features
=== Date fields
View
@@ -19,7 +19,6 @@ Gem::Specification.new do |s|
s.require_paths = ["lib"]
s.add_dependency "rails", "~>3.0.0"
- s.add_dependency "will_paginate", "3.0.pre2"
s.add_development_dependency "rspec", "~>2.0.0"
s.add_development_dependency "shoulda", "~>2.11.0"
View
@@ -12,10 +12,11 @@ def for_data_table controller, fields, search_fields=nil, explicit_block=nil, &i
block = (explicit_block or implicit_block)
objects = _find_objects params, fields, search_fields
+ matching_count = objects.respond_to?(:total_entries) ? objects.total_entries : _matching_count(params, search_fields)
{:sEcho => params[:sEcho].to_i,
:iTotalRecords => self.count,
- :iTotalDisplayRecords => objects.total_entries,
+ :iTotalDisplayRecords => matching_count,
:aaData => _yield_and_render_array(controller, objects, block)
}.to_json.html_safe
end
@@ -4,8 +4,12 @@ module ClassMethods
def _find_objects params, fields, search_fields
self.where(_where_conditions params[:sSearch], search_fields).
order_by(_order_by_fields params, fields).
- limit(_per_page params).
- paginate :page => _page(params), :per_page => _per_page(params)
+ page(_page params).
+ per(_per_page params)
+ end
+
+ def _matching_count params, search_fields
+ self.where(_where_conditions params[:sSearch], search_fields).count
end
def _where_conditions raw_query, search_fields
View
@@ -32,6 +32,27 @@
result.should == {:sEcho => 1, :iTotalRecords => 42, :iTotalDisplayRecords => 10, :aaData => :results}.to_json.html_safe
end
+ # won't work because of ruby 1.9.2 bug... https://gist.github.com/455547
+# it "should work with a pagination library that doesn't respond to #total_entries" do
+# params = {:sSearch => "answer", :iSortCol_0 => "0", :sSortDir_0 => "desc", :iDisplayLength => "10", :sEcho => "1"}
+# controller = mock!.params { params }.subject
+#
+# fields = %w(foo bar baz)
+# search_fields = %w(foo bar)
+#
+# mock(self).count { 42 }
+# mock(self)._matching_count(params, search_fields) { 10 }
+# mock(self)._find_objects(params, fields, search_fields) { :objects }
+# mock(self)._yield_and_render_array(controller, :objects, :block) { :results }
+#
+# result = for_data_table(controller, fields, search_fields, :block)
+# result.should == {:sEcho => 1, :iTotalRecords => 42, :iTotalDisplayRecords => 10, :aaData => :results}.to_json.html_safe
+# end
+
+ end
+
+ context "#_yield_and_render_array" do
+
end
context "#_yield_and_render_array" do
@@ -12,8 +12,8 @@
mock(self)._order_by_fields(params, %w(foo bar baz)) { "order by" }
mock(self)._page(params) { :page }
- mock(self)._per_page(params) { :per_page }.twice
- mock(self).where("where clause") { mock!.order_by("order by") { mock!.limit(:per_page) { mock!.paginate({:page => :page, :per_page => :per_page}) { :answer } } } }
+ mock(self)._per_page(params) { :per_page }
+ mock(self).where("where clause") { mock!.order_by("order by") { mock!.page(:page) { mock!.per(:per_page) { :answer } } } }
_find_objects(params, %w(foo bar baz), %w(foo bar)).should == :answer
end

0 comments on commit 7487222

Please sign in to comment.