Skip to content

Commit

Permalink
Merge pull request #38 from goosys/wip/issue-9
Browse files Browse the repository at this point in the history
Wip/issue 9
  • Loading branch information
goosys authored Sep 9, 2024
2 parents f5820d3 + c60b3a7 commit 6dbdd27
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 0 deletions.
66 changes: 66 additions & 0 deletions app/views/administrate/application/index.csv.csb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# vim: set ft=ruby:

#
# thx https://github.com/aki77/csb
# usage https://qiita.com/aki77/items/d9d453ed288f6302fd15#%E4%BD%BF%E3%81%84%E6%96%B9
#
return '' if @dashboard.collection_export_types.blank?

# see https://github.com/thoughtbot/administrate/blob/master/lib/administrate/base_dashboard.rb#L92
attribute_includes =
@dashboard.collection_export_types.map do |attr_name, type|
attr_name if type.associative?
end.compact
resources.includes(*attribute_includes) unless attribute_includes.empty?

#
csv.items = resources.except(:limit, :offset).find_each
csv.filename = "#{resource_class.name.tableize}.#{Time.current.to_i}.csv"

#
@dashboard.collection_export_types.each do |attr_name, type|
field = type.new(attr_name, nil, page, resource: authorized_new_resource)
input_type = type.is_a?(Administrate::Field::Deferred) ? type.deferred_class.to_s : type.to_s

header = t(
"administrate_exportable.label.#{page.resource_name}.#{attr_name}",
default: [
:"activerecord.attributes.#{resource_class.name.downcase}.#{resource_name}/#{attr_name}",
:"activerecord.attributes.#{page.resource_name}.#{attr_name}",
resource_class.human_attribute_name(attr_name)
]
)

#
csv.cols.add(header, attr_name) { |r|
field = type.new(attr_name, r.public_send(attr_name), page, resource: r)

# thx https://github.com/SourceLabsLLC/administrate_exportable/blob/master/lib/administrate_exportable/exporter_service.rb#L40
transform_on_export = type.respond_to?(:options) && type.options[:transform_on_export]
if transform_on_export.is_a? Proc
transform_on_export.call(field)
else
case field.class.to_s
when Administrate::Field::BelongsTo.to_s, Administrate::Field::HasOne.to_s, Administrate::Field::Polymorphic.to_s
field.display_associated_resource if field.data
when Administrate::Field::HasMany.to_s
field.data.count if field.data
when Administrate::Field::DateTime.to_s
field.datetime if field.data
when Administrate::Field::Date.to_s
field.date if field.data
when Administrate::Field::Email.to_s, Administrate::Field::Select.to_s
field.data
when Administrate::Field::Password.to_s
field.truncate
when Administrate::Field::Time.to_s
field.data.strftime("%I:%M%p").to_s if field.data
when Administrate::Field::String.to_s, Administrate::Field::Text.to_s
#Do not truncate
field.data
else
field.to_s
end
end
}
end
1 change: 1 addition & 0 deletions app/views/administrate/application/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ It renders the `_table` partial to display details about the resources.
search_term: search_term,
page: page,
show_search_bar: show_search_bar,
show_export_button: show_export_button?
)
%>

Expand Down
14 changes: 14 additions & 0 deletions app/views/administrate_ransack/components/_filter_buttons.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<div class="filters-buttons">
<%= hidden_field_tag :format, 'html', id: 'search-request-format' %>
<%= button_tag class: 'button button--search', onclick: "$('#search-request-format').val('html');" do %>
<span class="show-when-enabled"><%= t('administrate_ransack.filters.search') %></span>
<span class="show-when-disabled"><%= t('administrate_ransack.filters.searching', default: :'administrate_ransack.filters.search') %></span>
<% end %>
<% if defined?(show_export_button) && show_export_button %>
<%= button_tag class: 'button button--export', data: { disable_with: false, turbo: false }, onclick: "$('#search-request-format').val('csv');" do %>
<span class="show-when-enabled"><%= t('administrate_ransack.filters.csv_export') %></span>
<span class="show-when-disabled"><%= t('administrate_ransack.filters.csv_exporting', default: :'administrate_ransack.filters.csv_export') %></span>
<% end %>
<% end %>
<%= link_to t('administrate_ransack.filters.clear_filters'), clear_filters_path, class: 'button button--clear-filters' %>
</div>
1 change: 1 addition & 0 deletions lib/administrate/mistybird.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
require_relative "role_aware_resource_resolver"
require_relative "patches/administrate_datetime_default"
# require_relative "patches/administrate_view_generator"
require_relative "patches/collection_export_types"
require_relative "patches/collection_filter_types"
require_relative "patches/dashboard_name_option_to_nested_has_many"
require_relative "patches/default_display_resource_in_dashboard"
Expand Down
11 changes: 11 additions & 0 deletions lib/administrate/patches/collection_export_types.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require "administrate/base_dashboard"

Administrate::BaseDashboard.prepend (
Module.new {
def collection_export_types
self.class::COLLECTION_EXPORT_TYPES
end
}
)

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

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

context "when show_export_button is NOT defined in the Controller" do
it "does NOT display a button with 'button button--export' class" do
allow(view).to receive(:clear_filters_path).and_return("/")

render
expect(rendered).not_to include("button button--export")
end
end

context "when show_export_button IS defined in the Controller" do
it "displays a button with 'button button--export' class" do
allow(view).to receive(:show_export_button).and_return(true)
allow(view).to receive(:clear_filters_path).and_return("/")

render
expect(rendered).to include("button button--export")
end
end
end

0 comments on commit 6dbdd27

Please sign in to comment.