[英]Pagination for custom solr filter in active admin
我為活動管理員提供了一個自定義的袋式過濾器:
# app/admin/user.rb
filter :by_solr_in, :as => :string
使用solr進行搜索:
# app/models/user.rb
ransacker :by_solr, formatter: -> (v) {
ids = UserSearch.all({params: {q: v}, state: :all}).results.map(&:id)
ids.present? ? ids : nil
} do |product|
product.table[:id] # i think collection shoud be paginated here
end
Solr(UserSearch.all)給出所有找到的用戶的ID集合,而product.table [:id]從db中獲取具有這些ID的用戶。 問題是沒有分頁。 活動管理員僅顯示正確的頁面,但每個頁面的整個集合均來自db。 我該如何糾正?
我試圖將頁面傳遞給before_filter中的solr並在那里使用它,但是如果solr僅提供1頁,則活動管理員僅獲得1頁。
ids = UserSearch.all({params: {q: v['q'], page: v['page'], per_page: 30}, state: :all}).results.map(&:id)
我知道了 這不是完美的解決方案,但可能會對某人有所幫助。
在before_filter中,我為ransacker設置當前頁面並將其從params中刪除。
在ransacker中,我使用此頁面並從SolrPaginatedCollection獲取分頁數據:
results = UserSearch.all({params: {q: v, page: page, per_page: 30}, state: :all}).results
UserSearch.admin_pagination_data = [results.total_pages, results.total_count, page || 1]
在活動管理員中,我必須修改集合以顯示正確的頁面數據:
def collection
collection = super
if params['q'].present? && params['q']['by_solr_in'].present? && UserSearch.admin_pagination_data.present?
collection.define_singleton_method(:total_pages) { UserSearch.admin_pagination_data[0] }
collection.define_singleton_method(:total_count) { UserSearch.admin_pagination_data[1] }
collection.define_singleton_method(:current_page) { UserSearch.admin_pagination_data[2] }
end
collection
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.