Skip to content

Instantly share code, notes, and snippets.

@fred
Created June 21, 2014 06:44
Show Gist options
  • Save fred/428316f4557ada56570e to your computer and use it in GitHub Desktop.
Save fred/428316f4557ada56570e to your computer and use it in GitHub Desktop.
Activeadmin MongoID with Fulltext Search on Sidebar
/ 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"
# 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
# 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