Skip to content

Commit

Permalink
add shelf_id to inventory_files
Browse files Browse the repository at this point in the history
  • Loading branch information
nabeta committed Dec 30, 2019
1 parent 773b063 commit f5111cd
Show file tree
Hide file tree
Showing 15 changed files with 72 additions and 55 deletions.
9 changes: 8 additions & 1 deletion app/controllers/inventory_files_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class InventoryFilesController < ApplicationController
before_action :set_inventory_file, only: [:show, :edit, :update, :destroy]
before_action :check_policy, only: [:index, :new, :create]
before_action :prepare_options, only: [:new, :edit]

# GET /inventory_files
# GET /inventory_files.json
Expand Down Expand Up @@ -65,6 +66,7 @@ def create
format.html { redirect_to(@inventory_file) }
format.json { render json: @inventory_file, status: :created, location: @inventory_file }
else
prepare_options
format.html { render action: "new" }
format.json { render json: @inventory_file.errors, status: :unprocessable_entity }
end
Expand All @@ -80,6 +82,7 @@ def update
format.html { redirect_to(@inventory_file) }
format.json { head :no_content }
else
prepare_options
format.html { render action: "edit" }
format.json { render json: @inventory_file.errors, status: :unprocessable_entity }
end
Expand Down Expand Up @@ -107,7 +110,11 @@ def check_policy
authorize InventoryFile
end

def prepare_options
@shelves = Shelf.order(:position)
end

def inventory_file_params
params.require(:inventory_file).permit(:inventory, :note)
params.require(:inventory_file).permit(:inventory, :shelf_id, :note)
end
end
2 changes: 2 additions & 0 deletions app/models/inventory_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ class InventoryFile < ApplicationRecord
has_many :inventories, dependent: :destroy
has_many :items, through: :inventories
belongs_to :user
belongs_to :shelf

if ENV['ENJU_STORAGE'] == 's3'
has_attached_file :inventory, storage: :s3,
Expand Down Expand Up @@ -78,4 +79,5 @@ def found_items
# inventory_file_size :integer
# inventory_updated_at :datetime
# inventory_fingerprint :string
# shelf_id :bigint not null
#
23 changes: 23 additions & 0 deletions app/views/inventory_files/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<%= form_for(@inventory_file) do |f| -%>
<%= f.error_messages -%>

<div class="field">
<%= f.label "#{t('activerecord.models.library')} / #{t('activerecord.models.shelf')}" -%>
<%= f.select :library_id, @libraries.map{|l| [l.display_name, l.id]} %>
<%= f.select :shelf_id, @shelves.map{|s| [s.display_name, s.id]} %>
<%= render 'observe_field' %>
</div>

<div class="field">
<%= f.label t('page.file') -%><br />
<%= f.file_field :inventory -%>
</div>

<div class="field">
<%= f.label :note -%> <br />
<%= f.text_area :note -%>
</div>
<div class="actions">
<%= f.submit %>
</div>
<%- end -%>
7 changes: 7 additions & 0 deletions app/views/inventory_files/_observe_field.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script type="text/javascript">
function displayVals() {
var libraryValue = $("#inventory_file_library_id").val();
$("#inventory_file_shelf_id").load('<%= shelves_path %>?mode=select&library_id=' + libraryValue);
}
$("#inventory_file_library_id").change(displayVals);
</script>
36 changes: 3 additions & 33 deletions app/views/inventory_files/edit.html.erb
Original file line number Diff line number Diff line change
@@ -1,40 +1,10 @@
<div id="content_detail" class="ui-corner-all ui-widget-content">
<h1 class="title"><%= t('page.editing', :model => t('activerecord.models.inventory_file')) -%></h1>
<div id="content_list">
<h1 class="title"><%= t('page.editing', :model => t('activerecord.models.inventory_file')) -%></h1>
<div id="content_list">

<%= form_for(@inventory_file) do |f| -%>
<%= f.error_messages -%>
<%= render 'form' %>

<div class="field">
<%= f.label :inventory_file_name -%> <br />
<%= f.text_field :inventory_file_name -%>
</div>
<div class="field">
<%= f.label :inventory_content_type -%> <br />
<%= f.text_field :inventory_content_type -%>
</div>
<div class="field">
<%= f.label :inventory_file_size -%> <br />
<%= f.text_field :inventory_file_size -%>
</div>
<div class="field">
<%= f.label :inventory_fingerprint -%> <br />
<%= f.text_field :inventory_fingerprint -%>
</div>
<div class="field">
<%= f.label :user_id -%> <br />
<%= f.text_field :user_id -%>
</div>
<div class="field">
<%= f.label :note -%> <br />
<%= f.text_area :note -%>
</div>
<div class="actions">
<%= f.submit %>
</div>
<%- end -%>

</div>
</div>

<div id="submenu" class="ui-corner-all ui-widget-content">
Expand Down
4 changes: 2 additions & 2 deletions app/views/inventory_files/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@

<table class="table table-striped index">
<tr>
<th><%= t('activerecord.attributes.inventory_file.shelf') -%> </th>
<th><%= t('activerecord.attributes.inventory_file.inventory_file_name') -%> </th>
<th><%= t('activerecord.attributes.inventory_file.inventory_content_type') -%> </th>
<th><%= t('activerecord.attributes.inventory_file.inventory_file_size') -%> </th>
<th><%= t('activerecord.models.user') -%> </th>
<th></th>
</tr>

<%- @inventory_files.each do |inventory_file| -%>
<tr class="line<%= cycle("0", "1") -%>">
<td><%= link_to inventory_file.shelf.display_name, inventory_file.shelf %></td>
<td>
<%= link_to inventory_file.inventory_file_name, inventory_file -%>
<br />
<%= inventory_file.created_at -%>
</td>
<td><%= inventory_file.inventory_content_type -%> </td>
<td><%= inventory_file.inventory_file_size -%> </td>
<td><%= link_to inventory_file.user.username, inventory_file.user.profile if inventory_file.user.try(:profile) -%> </td>
<td>
Expand Down
17 changes: 3 additions & 14 deletions app/views/inventory_files/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,21 +1,10 @@
<div id="content_detail" class="ui-corner-all ui-widget-content">
<h1 class="title"><%= t('page.new', :model => t('activerecord.models.inventory_file')) -%> </h1>
<div id="content_list">
<h1 class="title"><%= t('page.new', :model => t('activerecord.models.inventory_file')) -%> </h1>
<div id="content_list">

<%= form_for(@inventory_file, :html => {:multipart => true}) do |f| -%>
<%= f.error_messages -%>
<%= render 'form' %>

<div class="field">
<%= f.label t('page.file') -%><br />
<%= f.file_field :inventory -%>
</div>

<div class="actions">
<%= f.submit %>
</div>
<%- end -%>

</div>
</div>

<div id="submenu" class="ui-corner-all ui-widget-content">
Expand Down
5 changes: 5 additions & 0 deletions app/views/inventory_files/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
<div id="content_list">
<p id="notice"><%= notice %></p>

<p>
<strong><%= t('activerecord.attributes.inventory_file.shelf') -%> :</strong>
<%= link_to @inventory_file.shelf.display_name, @inventory_file.shelf -%>
</p>

<p>
<strong><%= t('activerecord.attributes.inventory_file.inventory_file_name') -%> :</strong>
<%= @inventory_file.inventory_file_name -%>
Expand Down
1 change: 1 addition & 0 deletions config/locales/translation_en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ en:
inventory_file_size: Size
inventory_fingerprint: File hash
note: Note
shelf: Shelf

inventory_file:
not_in_catalog: "Not in catalog"
Expand Down
1 change: 1 addition & 0 deletions config/locales/translation_ja.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ ja:
inventory_file_size: ファイルサイズ
inventory_fingerprint: ファイルのハッシュ
note: 注記
shelf: 書棚

inventory_file:
not_in_catalog: "目録に存在しない資料"
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20191230082846_add_shelf_to_inventory_file.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddShelfToInventoryFile < ActiveRecord::Migration[5.2]
def change
add_reference :inventory_files, :shelf, foreign_key: true, null: false
end
end
7 changes: 3 additions & 4 deletions spec/controllers/inventory_files_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,7 @@
login_fixture_librarian

it "should create inventory_file" do
post :create, params: { inventory_file: {inventory: fixture_file_upload("/../../examples/inventory_file_sample.tsv", 'text/csv') } }
assigns(:inventory_file).save!
post :create, params: { inventory_file: { shelf_id: 1, inventory: fixture_file_upload("/../../examples/inventory_file_sample.tsv", 'text/csv') } }
expect(assigns(:inventory_file)).to be_valid
expect(assigns(:inventory_file).user.username).to eq @user.username
expect(response).to redirect_to inventory_file_url(assigns(:inventory_file))
Expand All @@ -135,14 +134,14 @@
login_fixture_user

it "should be forbidden" do
post :create, params: { inventory_file: {inventory: fixture_file_upload("/../../examples/inventory_file_sample.tsv", 'text/csv') } }
post :create, params: { inventory_file: { shelf_id: 1, inventory: fixture_file_upload("/../../examples/inventory_file_sample.tsv", 'text/csv') } }
expect(response).to be_forbidden
end
end

describe "When not logged in" do
it "should be redirect to new session url" do
post :create, params: { inventory_file: {inventory: fixture_file_upload("/../../examples/inventory_file_sample.tsv", 'text/csv') } }
post :create, params: { inventory_file: { shelf_id: 1, inventory: fixture_file_upload("/../../examples/inventory_file_sample.tsv", 'text/csv') } }
expect(response).to redirect_to new_user_session_url
end
end
Expand Down
5 changes: 4 additions & 1 deletion spec/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2019_12_24_091957) do
ActiveRecord::Schema.define(version: 2019_12_30_082846) do

# These are extensions that must be enabled in order to support this database
enable_extension "pgcrypto"
Expand Down Expand Up @@ -499,6 +499,8 @@
t.integer "inventory_file_size"
t.datetime "inventory_updated_at"
t.string "inventory_fingerprint"
t.bigint "shelf_id", null: false
t.index ["shelf_id"], name: "index_inventory_files_on_shelf_id"
t.index ["user_id"], name: "index_inventory_files_on_user_id"
end

Expand Down Expand Up @@ -1469,6 +1471,7 @@
add_foreign_key "identifiers", "manifestations"
add_foreign_key "import_requests", "manifestations"
add_foreign_key "import_requests", "users"
add_foreign_key "inventory_files", "shelves"
add_foreign_key "isbn_record_and_manifestations", "isbn_records"
add_foreign_key "isbn_record_and_manifestations", "manifestations"
add_foreign_key "issn_record_and_manifestations", "issn_records"
Expand Down
4 changes: 4 additions & 0 deletions spec/fixtures/inventory_files.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
inventory_file_00001:
id: 1
user_id: 1
shelf_id: 2
inventory_file_00002:
id: 2
user_id: 1
shelf_id: 2
inventory_file_00003:
id: 3
user_id: 1
shelf_id: 3

# == Schema Information
#
Expand All @@ -24,4 +27,5 @@ inventory_file_00003:
# inventory_file_size :integer
# inventory_updated_at :datetime
# inventory_fingerprint :string
# shelf_id :bigint not null
#
1 change: 1 addition & 0 deletions spec/models/inventory_file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@
# inventory_file_size :integer
# inventory_updated_at :datetime
# inventory_fingerprint :string
# shelf_id :bigint not null
#

0 comments on commit f5111cd

Please sign in to comment.