Skip to content

Commit

Permalink
Move filter attributes retrieval to view
Browse files Browse the repository at this point in the history
Because of when the user override the `scoped_resource` method in the
controller, the filter attributes retrieval will also be overriden. Thus
making the filter will not be shown in the view. So I move the retrieval
process to the view to make sure the filter will be shown.
  • Loading branch information
IrvanFza committed Dec 3, 2023
1 parent a97b133 commit c498748
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
administrate_filterable (0.0.2)
administrate_filterable (0.0.3)
actionview (>= 5.2.2.1)
administrate (>= 0.17.0)
rails (>= 4.2)
Expand Down
9 changes: 6 additions & 3 deletions app/views/admin/application/_index_filter.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<% if @administrate_filterable_attributes.present? %>
<% resource_new = resource_class.new %>
<% filter_attributes = AdministrateFilterable::FiltererService.filter_attributes(@dashboard, resource_new) %>
<% if filter_attributes.present? %>
<% resource_title = display_resource_name(resource_name) %>
<% # TODO: Improve the toggle button user experience (e.g. add open/close animation, add dynamic open/close title, etc) %>
Expand All @@ -13,8 +16,8 @@
<h2>Filter <%= resource_title %></h2>
</header>
<%= form_with(model: [:admin, new_resource], method: :get, html: { class: "form administrate-filterable__form" }) do |f| %>
<% @administrate_filterable_attributes.each do |attribute| -%>
<%= form_with(model: [:admin, resource_new], method: :get, html: { class: "form administrate-filterable__form" }) do |f| %>
<% filter_attributes.each do |attribute| -%>
<% # TODO: Add capability to customize the filter behavior (e.g. search by exact match, search by partial match, etc just like in the ActiveAdmin filter) %>
<div class="field-unit field-unit--<%= attribute.html_class %> administrate-filterable__field">
<%= render_field attribute, f: f %>
Expand Down
5 changes: 0 additions & 5 deletions lib/administrate_filterable/filterer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ def filterable
# This is a hack to implement the filter functionality by overriding the scoped_resource method
# It would be better to implement this as a separate controller action, but I don't have time to explore that right now
define_method(:scoped_resource) do
# TODO: Figure out a better way to pass the filter data to the form
# This is a hack to get the filter attributes to show up in the form, but it's not ideal
# So I tried to make the variable name as unique as possible to avoid collisions
@administrate_filterable_attributes = FiltererService.filter_attributes(dashboard, new_resource)

data = resource_class.all

filter_params = params[resource_name]
Expand Down

0 comments on commit c498748

Please sign in to comment.