Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add possibility to have custom search on model #3019

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 9 additions & 1 deletion lib/rails_admin/adapters/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,15 @@ 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]
if self.config.search_scope
scope = scope.send(self.config.search_scope, options[:query])
else
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])
Expand Down
4 changes: 4 additions & 0 deletions lib/rails_admin/config/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down