Created
June 21, 2014 06:44
-
-
Save fred/428316f4557ada56570e to your computer and use it in GitHub Desktop.
Activeadmin MongoID with Fulltext Search on Sidebar
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/ file: app/views/admin/users/_search.html.haml | |
#panel_contents | |
= form_for(admin_users_path, method: :get) do |f| | |
.filter_form_field.filter_string | |
= label_tag :query, "Full Text Search" | |
= text_field_tag :query, params[:query], { placeholder: "Keyword to search" } | |
.buttons | |
= submit_tag "Search" | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/models/user.rb | |
class User | |
include Mongoid::Document | |
field :name, type: String | |
field :email, type: String | |
field :phone, type: String | |
# Full Text search for users, only one per collection allowed | |
index({ name: 'text', email: 'text', phone: 'text' }) | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/admin/user.rb | |
ActiveAdmin.register User do | |
# Filter does not work yet, so using full text search from mongoDB | |
config.filters = false | |
sidebar :search, partial: 'search' | |
# ... the usual admin code | |
controller do | |
def scoped_collection | |
scope = User | |
if params[:query].present? | |
ids = User.text_search(params[:query]).map { |t| t.id.to_s } | |
# text_search method is not a MongoID::Criteria | |
# so need to search again using :id in array | |
scope = scope.in(id: ids) | |
end | |
scope | |
end | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment