Skip to content

Commit

Permalink
Merge pull request #40 from goosys/wip/issue-10
Browse files Browse the repository at this point in the history
feat: Add CSV import templates
  • Loading branch information
goosys authored Sep 9, 2024
2 parents ecc7e8f + 5efb608 commit beffcf6
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 0 deletions.
23 changes: 23 additions & 0 deletions app/views/administrate/application/_import_buttons.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<% if accessible_action?(new_resource, :import_for_delete) %>
<div class='import-link-button'>
<% modal_target_import_for_delete = 'modal-import-for-delete-'.concat(page.resource_name) %>
<span class="modal-link button btn-danger" data-modal-target="<%= modal_target_import_for_delete %>">
<%= t('administrate.actions.import_for_delete', name: page.resource_name.titleize.downcase) %>
</span>
</div>
<%= render 'modal', modal_target: modal_target_import_for_delete do %>
<%= render 'import_form_for_delete', page: page %>
<% end %>
<% end %>

<% if accessible_action?(new_resource, :import) %>
<div class='import-link-button'>
<% modal_target_import = 'modal-import-'.concat(page.resource_name) %>
<span class="modal-link button btn-info" data-modal-target="<%= modal_target_import %>">
<%= t('administrate.actions.import_resource', name: page.resource_name.titleize.downcase) %>
</span>
</div>
<%= render 'modal', modal_target: modal_target_import do %>
<%= render 'import_form', page: page %>
<% end %>
<% end %>
11 changes: 11 additions & 0 deletions app/views/administrate/application/_import_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<%= form_tag({ controller: page.resource_name.pluralize, action: :import }, method: :post, multipart: true) do %>
<%= file_field_tag :csv_file, required: true %>
<div class="import-form-buttons">
<%= submit_tag t('administrate.actions.import_button'), class: 'button btn-update', data: { disable_with: false } %>
<%= link_to t('administrate.actions.download_csv_format'),
{ controller: page.resource_name.pluralize, action: :download_csv_format },
format: :csv,
class: 'button'
%>
</div>
<% end %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<%= form_tag({ controller: page.resource_name.pluralize, action: :import_for_delete }, method: :post, multipart: true) do %>
<%= file_field_tag :csv_file, required: true %>
<div class="import-form-buttons">
<%= submit_tag t('administrate.actions.import_for_delete_button'), class: 'button btn-danger', data: { disable_with: false } %>
<%= link_to t('administrate.actions.download_invalid_resource'),
{ controller: page.resource_name.pluralize, action: :download_invalid_resource },
format: :csv,
class: 'button'
%>
</div>
<% end %>
1 change: 1 addition & 0 deletions app/views/administrate/application/_index_header.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
) if accessible_action?(new_resource, :new) %>
</div>

<%= render("import_buttons", page: page, new_resource: new_resource) %>
<%= content_for(:header_last) %>
</header>

Expand Down
8 changes: 8 additions & 0 deletions app/views/administrate/application/_modal.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<div class="modal <%= modal_target %>">
<div class="modal-inner">
<div class="modal-bg"></div>
<div class="modal-contents">
<%= yield %>
</div>
</div>
</div>
10 changes: 10 additions & 0 deletions app/views/administrate/application/download_csv_format.csb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# vim: set ft=ruby:

return '' if @csv_data.blank?

csv.items = @csv_data
csv.filename = "#{resource_class.name.tableize}_import_format.csv"

@csv_header.each do |attr_name, header|
csv.cols.add(header, attr_name) { |r| r[attr_name]}
end
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
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(:page).and_return(nil)
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>"
stub_template "import_buttons" => ""

assign(:ransack_results, nil)
render
Expand Down

0 comments on commit beffcf6

Please sign in to comment.