Skip to content

Commit

Permalink
support but deprecate legacy alchemy_resource_filters data structure
Browse files Browse the repository at this point in the history
This commit is supposed to be reverted after Alchemy v6.0 is released.
  • Loading branch information
robinboening committed Jul 8, 2021
1 parent 20aad20 commit ace7ac6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
30 changes: 27 additions & 3 deletions app/controllers/alchemy/admin/resources_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class ResourcesController < Alchemy::Admin::BaseController
helper Alchemy::ResourcesHelper, TagsHelper
helper_method :resource_handler, :search_filter_params,
:items_per_page, :items_per_page_options, :resource_has_filters,
:resource_filters
:resource_filters_for_select

before_action :load_resource,
only: [:show, :edit, :update, :destroy]
Expand Down Expand Up @@ -96,10 +96,34 @@ def resource_has_filters
resource_model.respond_to?(:alchemy_resource_filters)
end

def resource_has_deprecated_filters
resource_model.alchemy_resource_filters.any? { |f| !f.is_a?(Hash) }
end

def resource_filters
return unless resource_has_filters
return @_resource_filters if @_resource_filters

if resource_has_deprecated_filters
Alchemy::Deprecation.warn(
"#{resource_model}.alchemy_resource_filters is using a legacy data structure. " \
"Please use an Array of Hashes instead. i.e. [{ name: 'foo', values: ['bar', 'baz'] }, ...] " \
"where values are scopes. With Alchemy 6.1 only the new structure will be supported."
)

@_resource_filters ||= [
{
name: :misc,
values: resource_model.alchemy_resource_filters,
},
]
else
@_resource_filters ||= resource_model.alchemy_resource_filters
end
end

resource_model.alchemy_resource_filters.map do |filter|
def resource_filters_for_select
resource_filters.map do |filter|
ResourceFilter.new(filter, resource_handler.resource_name)
end
end
Expand Down Expand Up @@ -201,7 +225,7 @@ def common_search_filter_includes

if resource_has_filters
search_filters << {
filter: resource_model.alchemy_resource_filters.map { |f| f[:name] },
filter: resource_filters.map { |f| f[:name] },
}
end

Expand Down
2 changes: 1 addition & 1 deletion app/views/alchemy/admin/resources/_filter_bar.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div id="filter_bar">
<%= render partial: "filter", collection: resource_filters %>
<%= render partial: "filter", collection: resource_filters_for_select %>
</div>

<script type="text/javascript">
Expand Down

0 comments on commit ace7ac6

Please sign in to comment.