From d4e9ad62a9f9ab4871e12584cfe80781241166bf Mon Sep 17 00:00:00 2001 From: Roc Khalil Date: Wed, 2 May 2018 12:56:57 +0300 Subject: [PATCH 1/2] Add possibility to have custom search on model --- README.md | 21 +++++++++++++++++++++ lib/rails_admin/adapters/active_record.rb | 12 +++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 2a5045862f..b81a438718 100644 --- a/README.md +++ b/README.md @@ -72,6 +72,27 @@ end Details: [Models](https://github.com/sferik/rails_admin/wiki/Models), [Groups](https://github.com/sferik/rails_admin/wiki/Groups), [Fields](https://github.com/sferik/rails_admin/wiki/Fields) + +### Custom model search using pg_search +```ruby +class Ball < ActiveRecord::Base + include PgSearch + validates :name, presence: true + belongs_to :player + + pg_search_scope :rails_admin_search, + :against => [:id, :name], + :associated_against => { + player: [:id, :name] + }, + using: { + tsearch: {any_word: true,}, + trigram: {threshold: 0.1} + } +end +``` + + ## Documentation https://github.com/sferik/rails_admin/wiki diff --git a/lib/rails_admin/adapters/active_record.rb b/lib/rails_admin/adapters/active_record.rb index 46598aaf65..2f1b92f64d 100644 --- a/lib/rails_admin/adapters/active_record.rb +++ b/lib/rails_admin/adapters/active_record.rb @@ -30,7 +30,17 @@ def all(options = {}, scope = nil) scope = scope.includes(options[:include]) if options[:include] scope = scope.limit(options[:limit]) if options[:limit] scope = scope.where(primary_key => options[:bulk_ids]) if options[:bulk_ids] - scope = query_scope(scope, options[:query]) if options[:query] + + if options[:query] + begin + # if you're using pg_search, the method is not defined + # eventhough it's there + scope = scope.rails_admin_search(options[:query]) + rescue + scope = query_scope(scope, options[:query]) + end + end + scope = filter_scope(scope, options[:filters]) if options[:filters] if options[:page] && options[:per] scope = scope.send(Kaminari.config.page_method_name, options[:page]).per(options[:per]) From 90367379d489c3324287f825be45d61593ca0f23 Mon Sep 17 00:00:00 2001 From: Roc Khalil Date: Thu, 10 May 2018 11:09:26 +0300 Subject: [PATCH 2/2] Moved search_scope name to model's config --- lib/rails_admin/adapters/active_record.rb | 8 +++----- lib/rails_admin/config/model.rb | 4 ++++ 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/rails_admin/adapters/active_record.rb b/lib/rails_admin/adapters/active_record.rb index 2f1b92f64d..8426d46ca9 100644 --- a/lib/rails_admin/adapters/active_record.rb +++ b/lib/rails_admin/adapters/active_record.rb @@ -32,11 +32,9 @@ def all(options = {}, scope = nil) scope = scope.where(primary_key => options[:bulk_ids]) if options[:bulk_ids] if options[:query] - begin - # if you're using pg_search, the method is not defined - # eventhough it's there - scope = scope.rails_admin_search(options[:query]) - rescue + if self.config.search_scope + scope = scope.send(self.config.search_scope, options[:query]) + else scope = query_scope(scope, options[:query]) end end diff --git a/lib/rails_admin/config/model.rb b/lib/rails_admin/config/model.rb index 8d679c7c2c..14f2000084 100644 --- a/lib/rails_admin/config/model.rb +++ b/lib/rails_admin/config/model.rb @@ -76,6 +76,10 @@ def pluralize(count) 0 end + register_instance_option :search_scope do + @search_scope ||= nil + end + # parent node in navigation/breadcrumb register_instance_option :parent do @parent_model ||= begin