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 1 commit
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
12 changes: 11 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,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])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The search method's name should be configurable via DSL.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the time being, it's good to have this one;
I'll work on making it configurable from Rails Admin's model config later on.

Let me know if you wish me to proceed with the second one before pulling.

-- Btw, this little fix saved me hours and hours of building a custom search page

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Configurability is a key part of RailsAdmin, I don't merge this without it.
Also, having tests to make sure that this feature works is mandatory.

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])
Expand Down