Skip to content

Commit

Permalink
feat: ransack options support
Browse files Browse the repository at this point in the history
  • Loading branch information
blocknotes committed Sep 11, 2023
1 parent 58f4273 commit 43934c1
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,21 @@ end

## Customizations

- Ransack options can be customized defining a `ransack_options` method in the controller, example:

```rb
module Admin
class PostsController < Admin::ApplicationController
prepend AdministrateRansack::Searchable

def ransack_options
# raises an exception on unknown parameters
{ ignore_unknown_conditions: false }
end
end
end
```

- Sample call of the filters partial with different options provided:

```erb
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 @@ -5,7 +5,8 @@
module AdministrateRansack
module Searchable
def scoped_resource
@ransack_results = super.ransack(params[:q])
options = respond_to?(:ransack_options) ? ransack_options : {}
@ransack_results = super.ransack(params[:q], **options)
@ransack_results.result(distinct: true)
end

Expand Down
8 changes: 8 additions & 0 deletions spec/controllers/admin/posts_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,13 @@

expect(assigns(:ransack_results)).to be_instance_of Ransack::Search
end

context "when ransack_options is defined" do
it "raises an exception with an invalid parameter" do
expect {
get :index, params: { "q[title2_cont]" => "test" }
}.to raise_exception(ArgumentError, 'Invalid search term title2_cont')
end
end
end
end
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 @@ -2,6 +2,10 @@ module Admin
class PostsController < Admin::ApplicationController
prepend AdministrateRansack::Searchable

def ransack_options
{ ignore_unknown_conditions: 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

0 comments on commit 43934c1

Please sign in to comment.