Skip to content

Commit a9c7696

Browse files
committed
貸出書籍在庫の CSV 一括出力機能を追加。
貸出書籍在庫の棚卸目的。
1 parent 24a1af3 commit a9c7696

File tree

5 files changed

+45
-5
lines changed

5 files changed

+45
-5
lines changed

app/controllers/book_stocks_controller.rb

+16
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,22 @@ def bulk_insert
7575
redirect_to book_stocks_url
7676
end
7777

78+
# GET /book_stocks/export
79+
def export
80+
@book_stocks = BookStock
81+
.eager_load(lending_sets: :customer)
82+
.eager_load(:book_master)
83+
.eager_load(:book_stock_status)
84+
.order("book_stocks.id, lending_sets.created_at desc")
85+
respond_to do |format|
86+
format.csv do
87+
send_data render_to_string, filename: 'book_stocks.csv', type: :csv
88+
@q = @book_stocks.ransack(params[:q])
89+
@q.sorts = "id asc" if @q.sorts.empty?
90+
end
91+
end
92+
end
93+
7894
private
7995

8096
# Use callbacks to share common setup or constraints between actions.

app/models/book_stock.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ def self.ransackable_attributes(_auth_object = nil)
99
belongs_to :book_stock_status
1010

1111
has_many :lending
12-
has_many :ng_sets, through: :lending
12+
has_many :lending_sets, through: :lending
1313
end

app/views/book_stocks/export.csv.ruby

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
require 'csv'
2+
3+
CSV.generate("\xEF\xBB\xBF") do |csv|
4+
csv << %w(id タイトル 状態 貸出セットID 貸出状態 貸出先, 貸出先連絡先)
5+
@book_stocks.each do |bs|
6+
csv << [
7+
bs.id,
8+
bs.book_master.title,
9+
bs.book_stock_status.name,
10+
bs.lending_sets.target.first ? bs.lending_sets.target.first.id : "",
11+
bs.lending_sets.target.first ? bs.lending_sets.target.first.lending_status.name : "",
12+
bs.lending_sets.target.first ? bs.lending_sets.target.first.customer.name : "",
13+
bs.lending_sets.target.first ? bs.lending_sets.target.first.customer.email_address : ""
14+
]
15+
end
16+
end

app/views/book_stocks/index.html.erb

+5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
<%= render partial: "flash", locals: {model: Author} %>
1010
</div>
1111

12+
<%= link_to(
13+
BookStock.model_name.human + " CSV 出力",
14+
export_book_stocks_path(format: :csv),
15+
) %>
16+
1217
<%= form_tag bulk_insert_book_stocks_path, multipart: true do %>
1318
<%= label_tag "CSVインポート:" %>
1419
<%= file_field_tag :file, accept: "text/csv" %>

config/routes.rb

+7-4
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,23 @@
22
resources :lending_sets
33
resources :lending_statuses
44
resources :customers
5-
resources :customers, except: %w[new] do
5+
resources :customers, except: %w[import] do
66
collection { post :bulk_insert }
77
end
8+
resources :book_stocks, except: %w[export] do
9+
collection { get :export }
10+
end
811
resources :book_stocks
9-
resources :book_stocks, except: %w[new] do
12+
resources :book_stocks, except: %w[import] do
1013
collection { post :bulk_insert }
1114
end
1215
resources :book_stock_statuses
1316
resources :authors
14-
resources :authors, except: %w[new] do
17+
resources :authors, except: %w[import] do
1518
collection { post :bulk_insert }
1619
end
1720
resources :book_masters
18-
resources :book_masters, except: %w[new] do
21+
resources :book_masters, except: %w[import] do
1922
collection { post :bulk_insert }
2023
end
2124
resources :ndc_categories

0 commit comments

Comments
 (0)