-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
# What it does Adds a button to the admin UI to export items.
- Loading branch information
Showing
7 changed files
with
97 additions
and
36 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,23 @@ | ||
class Admin::Settings::ExportsController < Admin::BaseController | ||
include ActionController::Live | ||
|
||
def index | ||
end | ||
|
||
def create | ||
now = Time.current.rfc3339 | ||
filename = "all-items-#{now}.csv" | ||
|
||
send_file_headers!( | ||
type: "text/csv", | ||
disposition: "attachment", | ||
filename: filename | ||
) | ||
response.headers["Last-Modified"] = Time.now.httpdate | ||
|
||
exporter = ItemExporter.new(Item.all) | ||
exporter.export(response.stream) | ||
ensure | ||
response.stream.close | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
require "csv" | ||
|
||
class ItemExporter | ||
def initialize(items) | ||
@items = items | ||
end | ||
|
||
def export(stream) | ||
columns = %w[ | ||
id name description size brand model serial strength power_source | ||
other_names quantity checkout_notice status | ||
location_area location_shelf | ||
purchase_link purchase_price myturn_item_type url | ||
created_at updated_at | ||
] | ||
headers = [ | ||
"complete_number", | ||
"code", | ||
"number", | ||
"categories", | ||
"item_url", | ||
*columns | ||
] | ||
stream.write(CSV.generate_line(headers)) | ||
@items.includes(:borrow_policy, :category_nodes, :rich_text_description).in_batches(of: 100) do |items| | ||
items.each do |item| | ||
stream.write(CSV.generate_line([ | ||
item.complete_number, | ||
item.borrow_policy.code, | ||
item.number, | ||
item.category_nodes.map { |cn| cn.path_names.join("//") }.sort.join("; "), | ||
"https://app.chicagotoollibrary.org/admin/items/#{item.id}", | ||
*columns.map { |c| item.send(c) } | ||
])) | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<%= content_for :header do %> | ||
<%= index_header "Item Export" %> | ||
<% end %> | ||
|
||
<h2>Export all items to CSV</h2> | ||
|
||
<%= link_to "Download CSV", admin_settings_exports_path, method: :post, class: "btn btn-lg", data: {turbolinks: false} %> | ||
|
||
<div class="columns"> | ||
<div class="column col-6"> | ||
</div> | ||
</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
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,18 @@ | ||
require "test_helper" | ||
|
||
class ItemExporterTest < ActiveSupport::TestCase | ||
setup do | ||
end | ||
|
||
test "exports items" do | ||
create(:item) | ||
|
||
exporter = ItemExporter.new(Item.all) | ||
stream = StringIO.new | ||
exporter.export(stream) | ||
|
||
stream.rewind | ||
|
||
assert_equal 2, stream.read.lines.size | ||
end | ||
end |