Skip to content

Commit

Permalink
Add item exporting to admin (#1103)
Browse files Browse the repository at this point in the history
# What it does

Adds a button to the admin UI to export items.
  • Loading branch information
jim authored Jul 18, 2023
1 parent adef8f6 commit d18f8b1
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 36 deletions.
23 changes: 23 additions & 0 deletions app/controllers/admin/settings/exports_controller.rb
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
38 changes: 38 additions & 0 deletions app/lib/item_exporter.rb
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
12 changes: 12 additions & 0 deletions app/views/admin/settings/exports/index.html.erb
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>
4 changes: 3 additions & 1 deletion app/views/layouts/admin.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@
<li class="nav-item"><%= link_to "Categories", admin_categories_path %></li>
<% if current_user.admin? %>
<li class="nav-item"><%= link_to "Documents", admin_documents_path %></li>
<li class="nav-item"><%= link_to "Email Settings", admin_settings_email_settings_path %></li>
<li class="nav-item"><%= link_to "Item Export", admin_settings_exports_path %></li>
<% end %>
<li class="nav-item"><%= link_to "Email Settings", admin_settings_email_settings_path %></li>
<li class="nav-item"><%= link_to "Gift Memberships", admin_gift_memberships_path %></li>
<% if current_user.admin? %>
<li class="nav-item"><%= link_to "Library Updates", admin_settings_library_updates_path %></li>
Expand Down Expand Up @@ -151,6 +152,7 @@
<li class="menu-item"><%= link_to "Documents", admin_documents_path %></li>
<% if current_user.has_role?(:admin) %>
<li class="menu-item"><%= link_to "Email Settings", admin_settings_email_settings_path %></li>
<li class="menu-item"><%= link_to "Item Export", admin_settings_exports_path %></li>
<% end %>
<li class="menu-item"><%= link_to "Gift Memberships", admin_gift_memberships_path %></li>
<% if current_user.has_role?(:admin) %>
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@
get :preview, on: :member
end
resources :library_updates
resources :exports, only: [:index, :create]
end

resources :holds, only: [:index]
Expand Down
37 changes: 2 additions & 35 deletions lib/tasks/export.rake
Original file line number Diff line number Diff line change
Expand Up @@ -101,37 +101,6 @@ namespace :export do
end
end

desc "Export items to CSV"
task items_to_csv: :environment do
now = Time.current.rfc3339
path = Rails.root + "exports" + "all-items-#{now}.csv"
puts "writing items to #{path}"
columns = %w[
id name description size brand model serial strength
quantity checkout_notice status created_at updated_at
]
CSV.open(path, "wb") do |csv|
csv << [
"complete_number",
"code",
"number",
"categories",
*columns
]
Item.includes(:borrow_policy, :category_nodes).in_batches(of: 100) do |items|
items.each do |item|
csv << [
item.complete_number,
item.borrow_policy.code,
item.number,
item.category_nodes.map { |cn| cn.path_names.join("//") }.sort.join("; "),
*item.attributes.values_at(*columns)
]
end
end
end
end

desc "Export seeds to CSV"
task seeds_to_csv: :environment do
now = Time.current.rfc3339
Expand Down Expand Up @@ -173,16 +142,14 @@ namespace :export do
csv << [
"id",
"name",
"complete_name",
"items_count"
"complete_name"
]
CategoryNode.in_batches(of: 100) do |nodes|
nodes.each do |node|
csv << [
node.id,
node.name,
node.path_names.join("//"),
node.categorizations_count
node.path_names.join("//")
]
end
end
Expand Down
18 changes: 18 additions & 0 deletions test/lib/item_exporter_test.rb
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

0 comments on commit d18f8b1

Please sign in to comment.