8000 Fix filtering by nested resource by deivid-rodriguez · Pull Request #5816 · activeadmin/activeadmin · GitHub
[go: up one dir, main page]

Skip to content

Fix filtering by nested resource #5816

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

Merged
merged 4 commits into from
Jul 25, 2019
Merged
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Bug Fixes

* Fix CSVBuilder not respecting `ActiveAdmin.application.csv_options = { humanize_name: false }` setting. [#5800] by [@HappyKadaver]
* Fix crash when displaying current filters after filtering by a nested resource. [#5816] by [@deivid-rodriguez]

## 2.2.0 [☰](https://github.com/activeadmin/activeadmin/compare/v2.1.0..v2.2.0)

Expand Down Expand Up @@ -481,6 +482,7 @@ Please check [0-6-stable] for previous changes.
[#5800]: https://github.com/activeadmin/activeadmin/pull/5800
[#5801]: https://github.com/activeadmin/activeadmin/pull/5801
[#5802]: https://github.com/activeadmin/activeadmin/pull/5802
[#5816]: https://github.com/activeadmin/activeadmin/pull/5816

[@5t111111]: https://github.com/5t111111
[@aarek]: https://github.com/aarek
Expand Down
18 changes: 17 additions & 1 deletion features/index/filters.feature
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,6 @@ Feature: Index Filtering
And the "Jane Doe" checkbox should be checked

Scenario: Filtering posts without default scope

Given a post with the title "Hello World" written by "Jane Doe" exists
And an index configuration of:
"""
Expand Down Expand Up @@ -249,3 +248,20 @@ Feature: Index Filtering
"""
And I press "Filter"
Then I should not see a sidebar titled "Search status:"

Scenario: Filters and nested resources
Given a post with the title "The arrogant president" written by "Jane Doe" exists
And a configuration of:
"""
ActiveAdmin.register User
ActiveAdmin.register Post do
permit_params :user_id

belongs_to :author, class_name: "User"
end
"""
And I am logged in
And I am on the index page for users
When I select "The arrogant president" from "Posts"
And I press "Filter"
And I should see 1 user in the table
3 changes: 3 additions & 0 deletions lib/active_admin/resource/belongs_to.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ def initialize(key, namespace)
# The resource which initiated this relationship
attr_reader :owner

# The name of the relation
attr_reader :target_name

def initialize(owner, target_name, options = {})
@owner = owner
@target_name = target_name
Expand Down
14 changes: 11 additions & 3 deletions lib/active_admin/resource/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def route_name(resource_path_name, options = {})
# @return params to pass to instance path
def route_instance_params(instance)
if nested?
[instance.public_send(belongs_to_name).to_param, instance.to_param]
[instance.public_send(belongs_to_target_name).to_param, instance.to_param]
else
instance.to_param
end
Expand All @@ -123,11 +123,19 @@ def route_collection_params(params)
end

def nested?
resource.belongs_to? && resource.belongs_to_config.required?
resource.belongs_to? && belongs_to_config.required?
end

def belongs_to_target_name
belongs_to_config.target_name
end

def belongs_to_name
resource.belongs_to_config.target.resource_name.singular if nested?
belongs_to_config.target.resource_name.singular
end

def belongs_to_config
resource.belongs_to_config
end

def routes
Expand Down
0