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

refactor: make Ransack distinct optional #33

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ end

## Customizations

- Ransack options can be customized defining a `ransack_options` method in the controller, example:
- Ransack options can be set adding specific methods (`ransack_options` / `ransack_result_distinct`) to a resource controller, example:

```rb
module Admin
Expand All @@ -128,6 +128,12 @@ module Admin
# raises an exception on unknown parameters
{ ignore_unknown_conditions: false }
end

# by default distinct is applied on the search results
def ransack_result_distinct
# disable distinct
false
end
end
end
```
Expand Down
3 changes: 2 additions & 1 deletion lib/administrate_ransack/searchable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ module AdministrateRansack
module Searchable
def scoped_resource
options = respond_to?(:ransack_options) ? ransack_options : {}
distinct = respond_to?(:ransack_result_distinct) ? ransack_result_distinct : true
@ransack_results = super.ransack(params[:q], **options)
@ransack_results.result(distinct: true)
@ransack_results.result(distinct: distinct)
end

# ref => https://github.com/thoughtbot/administrate/blob/v0.18.0/app/helpers/administrate/application_helper.rb#L72-L78
Expand Down
4 changes: 4 additions & 0 deletions spec/dummy/app/controllers/admin/posts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ def ransack_options
{ ignore_unknown_conditions: false }
end

def ransack_result_distinct
false
end

# Overwrite any of the RESTful controller actions to implement custom behavior
# For example, you may want to send an email after a foo is updated.
#
Expand Down