Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

蔵書点検の機能を追加 #1635

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 81 additions & 17 deletions app/models/inventory_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,88 @@ class InventoryFile < ApplicationRecord
paginates_per 10

def import
self.reload
file = File.open(self.inventory.path)
reader = file.read
reader.split.each do |row|
identifier = row.to_s.strip
item = Item.find_by(item_identifier: identifier)
next unless item
next if self.items.where(id: item.id).select('items.id').first

Inventory.create(
inventory_file: self,
item: item,
current_shelf_name: shelf.name,
item_identifier: identifier
)
if ENV['ENJU_STORAGE'] == 's3'
body = Faraday.get(inventory.expiring_url(10)).body.force_encoding('UTF-8')
else
body = File.open(inventory.path).read
end

CSV.parse(body, headers: true, col_sep: "\t") do |row|
# 電子書籍を除外
next if row['shelf'] == 'web'
item_identifier = row['item_identifier'].to_s.strip
next if item_identifier.blank?

Item.transaction do
item = Item.find_by(item_identifier: item_identifier)
Inventory.create(
inventory_file: self,
item: item,
current_shelf_name: shelf.name,
item_identifier: item_identifier
)

next unless item

# 配架場所の変更を反映
unless row['current_shelf'] == row['shelf']
unless row['current_shelf'] == '-' or row['current_shelf'].blank?
shelf = Shelf.find_by(name: row['current_shelf'])
if item && shelf
item.update!(shelf: Shelf.find_by(name: row['current_shelf']))
Rails.logger.info "Inventory: #{item.item_identifier}: shelf updated to #{shelf.name}"
end
end
end
next

case row['circulation_status']
when 'On Loan'
# 書架にないと報告されたが、確認したら貸出中の場合、不明日を削除
if item.missing_since.present?
item.update!(missing_since: nil)
Rails.logger.info "Inventory: #{row['item_identifier']}: wrong report (rent)"
end
when 'Available On Shelf'
# 書架にないと報告されたが、確認したら書架にあった本を「利用可能」に変更
if row['current_shelf'] == '-' or row['current_shelf'].blank?
if item.missing_since.present? && !item.rent?
item.update!(missing_since: nil, circulation_status: CirculationStatus.find_by(name: row['circulation_status']))
Rails.logger.info "Inventory: #{row['item_identifier']}: wrong report (on shelf)"
end
end

# 発見された不明本の状態を変更
unless row['current_shelf'] == '-' or row['current_shelf'].blank?
if row['found_at'].present?
if item.rent?
item.update!(missing_since: nil)
else
item.update!(
missing_since: nil,
circulation_status: CirculationStatus.find_by(name: row['circulation_status'])
)
end
Rails.logger.info "Inventory: #{row['item_identifier']}: changed status to found"
end
end

if item.circulation_status.name == 'In Process'
item.update!(circulation_status: CirculationStatus.find_by(name: 'Available On Shelf'))
Rails.logger.info "Inventory: #{row['item_identifier']}: changed status to available"
end
when 'Missing'
item.circulation_status = CirculationStatus.find_by(name: 'Missing') unless item.circulation_status == 'Missing'
if row['missing_since'].blank?
item.update!(missing_since: Date.today)
Rails.logger.info "Inventory: #{row['item_identifier']}: changed status to missing"
else
item.update!(missing_since: Date.parse(row['missing_since']))
Rails.logger.info "Inventory: #{row['item_identifier']}: changed status to missing"
end
end
end
end
file.close
true
end

def export(col_sep: "\t")
Expand Down
38 changes: 38 additions & 0 deletions spec/models/inventory_file_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require 'rails_helper'

describe InventoryFile do
fixtures :users

before(:each) do
@file = InventoryFile.create(
user: users(:admin),
shelf: Shelf.find_by(name: 'first_shelf'),
inventory: File.new("#{Rails.root.to_s}/spec/fixtures/files/inventory_file_sample.tsv")
)
end

it "should be imported" do
expect(@file.import).to be_truthy
end

it "should export results" do
expect(@file.export).to be_truthy
end
end

# == Schema Information
#
# Table name: inventory_files
#
# id :bigint not null, primary key
# user_id :bigint
# note :text
# created_at :datetime not null
# updated_at :datetime not null
# inventory_file_name :string
# inventory_content_type :string
# inventory_file_size :integer
# inventory_updated_at :datetime
# inventory_fingerprint :string
# shelf_id :bigint not null
#
20 changes: 20 additions & 0 deletions spec/models/inventory_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require 'rails_helper'

describe Inventory do
# pending "add some examples to (or delete) #{__FILE__}"

end

# == Schema Information
#
# Table name: inventories
#
# id :bigint not null, primary key
# item_id :bigint
# inventory_file_id :bigint
# note :text
# created_at :datetime not null
# updated_at :datetime not null
# item_identifier :string not null
# current_shelf_name :string not null
#