-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from goosys/wip/issue-9
Wip/issue 9
- Loading branch information
Showing
6 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
app/views/administrate_ransack/components/_filter_buttons.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
26 changes: 26 additions & 0 deletions
26
spec/administrate/views/administrate_ransack/components/_filter_buttons_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |