Skip to content

Commit

Permalink
Merge pull request #34 from goosys/wip/issue-8
Browse files Browse the repository at this point in the history
Wip/issue 8
  • Loading branch information
goosys authored Jul 30, 2024
2 parents f2007ba + 6d74f3d commit 86dd7b7
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ gemspec
gem "rake", "~> 13.0"

gem "enumerize"
gem "ransack"
gem 'refile', require: 'refile/rails', github: 'vitalinfo/refile'
gem 'refile-mini_magick'

gem "sprockets-rails"
gem "administrate", github: "goosys/administrate", branch: "feature/contextualize"
gem "administrate_ransack", github: "goosys/administrate_ransack", branch: "feature/predicates-select-selectize-administrate1-invalid_term"

group :development, :test do
gem "rspec", "~> 3.0"
Expand Down
1 change: 1 addition & 0 deletions administrate-mistybird.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Gem::Specification.new do |spec|
spec.add_dependency 'rails', '>= 6.0', '< 8'
spec.add_dependency 'administrate', '>= 0.20.0', '< 1.1.0'
spec.add_dependency 'administrate-field-nested_has_many', '2.1.0'
spec.add_dependency 'administrate_ransack'

# For more information and examples about making a new gem, check out our
# guide at: https://bundler.io/guides/creating_gem.html
Expand Down
11 changes: 11 additions & 0 deletions app/views/administrate/application/_index_header.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,14 @@

<%= content_for(:header_last) %>
</header>

<% if @ransack_results.present? %>
<section class="ransack-filters-container">
<%= render(
"administrate_ransack/filters",
attribute_types: (page.collection_filter_types.presence || page.attribute_types),
page: page,
namespace: namespace
) %>
</section>
<% end %>
47 changes: 47 additions & 0 deletions app/views/administrate_ransack/_filters.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<%
attribute_labels ||= {}
attribute_types ||= @dashboard.attribute_types.select { |key, _value| @dashboard.collection_attributes.include?(key) }
options ||= {}
form_options = { html: { 'data-administrate-ransack-filters': '1' } }
namespace = local_assigns[:namespace] || :admin
if local_assigns.has_key?(:search_path)
form_path = @ransack_results
form_options[:url] = search_path
clear_filters_path = search_path
else
form_path = [namespace, @ransack_results]
clear_filters_path = url_for(url_for([namespace, @ransack_results.klass]))
end
%>
<%# render 'administrate_ransack/javascript' %>
<%= search_form_for form_path, form_options do |f| %>
<div class="filters">
<% attribute_types.each do |field, type| %>
<% label = attribute_labels.include?(field) ? attribute_labels[field] : nil %>
<% model = @ransack_results.klass %>
<% input_type = type.is_a?(Administrate::Field::Deferred) ? type.deferred_class.to_s : type.to_s %>
<% next if field == :id %>
<% next if input_type == 'Administrate::Field::Refile' %>
<% next if input_type == 'Administrate::Field::Enumerize' && (@ransack_results.klass.respond_to?("with_#{field}").blank? || @ransack_results.klass.ransackable_scopes.map(&:to_s).include?("with_#{field}").blank?) %>
<% next if input_type == 'Administrate::Field::String' && !(f.search_field "#{field}_cont" rescue nil) %>
<% component = AdministrateRansack::FILTERS[input_type] || 'field_other' %>

<div class="filter filter-<%= field.to_s.parameterize %> <%= input_type.parameterize %>">
<% case input_type %>
<% when 'Administrate::Field::Enumerize' %>
<% options = @ransack_results.klass.public_send(field.to_s).values.map {|w| [w.text, w]} %>
<%= f.label label || field %>
<%= f.select "with_#{field}", options, {include_blank: true}, data: {controller: :select} %>
<% else %>
<% field_options = options.is_a?(Hash) ? (options[field.to_s] || {}) : {} %>
<%= render "administrate_ransack/components/#{component}", form: f, model: model, field: field, label: label, type: type, options: field_options %>
<% end %>
</div>
<% end %>
</div>

<%= render 'administrate_ransack/components/filter_buttons', form: f, clear_filters_path: clear_filters_path %>
<% end %>
1 change: 1 addition & 0 deletions lib/administrate/mistybird.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
require_relative "role_aware_resource_resolver"
require_relative "patches/administrate_datetime_default"
# require_relative "patches/administrate_view_generator"
require_relative "patches/collection_filter_types"
require_relative "patches/dashboard_name_option_to_nested_has_many"
require_relative "patches/default_display_resource_in_dashboard"
require_relative "patches/enable_skip_option_in_nested_has_many"
Expand Down
20 changes: 20 additions & 0 deletions lib/administrate/patches/collection_filter_types.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require "administrate/base_dashboard"
require "administrate/page/collection"

Administrate::Page::Collection.prepend (
Module.new {
def collection_filter_types
dashboard.collection_filter_types
end
}
)

Administrate::BaseDashboard.prepend (
Module.new {
def collection_filter_types
self.class::COLLECTION_FILTER_TYPES
end
}
)

Administrate::BaseDashboard.const_set(:COLLECTION_FILTER_TYPES, {}.freeze)
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require "rails_helper"

describe "administrate/application/_index_header", type: :view do
before do
view.lookup_context.prepend_view_paths [File.expand_path("../../../../../app/views", __dir__)] #TODO
end

context "when the controller is not ransack searchable" do
it "does not render the ransack search form" do
allow(view).to receive(:content_for).and_return(nil)
allow(view).to receive(:show_search_bar).and_return(false)
allow(view).to receive(:new_resource).and_return(nil)
allow(view).to receive(:accessible_action?).and_return(false)
stub_template "administrate_ransack/filters" => "<div class='ransack-filters-container'></div>"

assign(:ransack_results, nil)
render

expect(rendered).not_to match("ransack-filters-container")
end
end
end

0 comments on commit 86dd7b7

Please sign in to comment.