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

Add possibility to have custom search on model #3019

wants to merge 2 commits into from

Conversation

RocKhalil
Copy link
Contributor

You can now add a scope rails_admin_search that allows you to override the default search method. This will be useful for people who wants to search with joined model;

For example, if you're using globalize and the model's name column is translated, currently it fails to search for the correct one, but with pg_search and some minor basic querying with pg_search you'll be able to find your needed result.

Example of usage:

  pg_search_scope :rails_admin_search,
    :against => [:id],
    :associated_against => {
      translations: :name
    },
    using: {
      tsearch: {any_word: true,},
      trigram: {threshold: 0.1}
    }

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.

@RocKhalil
Copy link
Contributor Author

@mshibuya I've moved the search_scope to model's config;
However, I'm not sure how to write tests for it since I use Guard instead of RSpec;
Here's how it can be used:

config.model Player do
  search_scope :my_search
end

class Player < ApplicationRecord
  scope :my_search, -> (keyword) { where(name: keyword) }
end

@mshibuya mshibuya added this to the 2.0.0 milestone May 2, 2019
@mshibuya mshibuya closed this in 9abc7f9 Jun 1, 2019
@mshibuya
Copy link
Member

mshibuya commented Jun 1, 2019

Based on your idea, 9abc7f9 is now in the master.
Thanks!

@RocKhalil
Copy link
Contributor Author

@mshibuya excellent ! thanks :-)

@mshibuya
Copy link
Member

mshibuya commented Jun 2, 2019

Ah, I forgot to mention that I've made slight modification to the DSL.
I've updated the wiki accordingly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants