Permalink
Browse files

Hack to improve performance with mongoid 2.4

There was a problem with mongo driver used in mongoid 2.4. I
incorporated something like pagination in search, to limit number of
documents queried.
  • Loading branch information...
1 parent 59a2f48 commit 0888e88611987a101a04cb0363c221710ac9ddd3 @Ostrzy Ostrzy committed Jul 28, 2012
Showing with 8 additions and 4 deletions.
  1. +3 −2 lib/active_admin/mongoid/adaptor.rb
  2. +5 −2 lib/active_admin/mongoid/resource.rb
@@ -4,11 +4,12 @@ module Adaptor
class Search
attr_reader :base, :query, :query_hash, :search_params
- def initialize(object, search_params = {})
+ def initialize(object, search_params = {}, per_page = 30, page = 1)
@base = object
@search_params = search_params
@query_hash = get_query_hash(search_params)
- @query = @base.where(@query_hash)
+ vpage = page.to_i > 0 ? page.to_i : 1
+ @query = @base.where(@query_hash).limit(per_page).skip(per_page * (vpage - 1))
end
def respond_to?(method_id)
@@ -6,6 +6,10 @@ class ActiveAdmin::Resource
def resource_table_name
resource_class.collection_name
end
+
+ def mongoid_per_page
+ per_page
+ end
end
ActiveAdmin::ResourceController # autoload
@@ -22,7 +26,6 @@ def sort_order(chain)
end
def search(chain)
- @search = ActiveAdmin::Mongoid::Adaptor::Search.new(chain, clean_search_params(params[:q]))
+ @search = ActiveAdmin::Mongoid::Adaptor::Search.new(chain, clean_search_params(params[:q]), active_admin_config.mongoid_per_page, params[:page])
end
-
end

0 comments on commit 0888e88

Please sign in to comment.