From a150cbb73b20319f650a499295d8bd6da517b58c Mon Sep 17 00:00:00 2001 From: Kosuke Tanabe Date: Sun, 29 Dec 2019 22:19:10 +0900 Subject: [PATCH] update dummy files --- app/controllers/inventory_files_controller.rb | 13 ++++++- app/models/inventory_file.rb | 36 ++++++------------- app/views/inventory_files/edit.html.erb | 20 +++++++++++ app/views/inventory_files/index.html.erb | 3 +- app/views/inventory_files/show.html.erb | 11 ++++-- .../inventory_files_controller_spec.rb | 19 +++++----- .../app/controllers/application_controller.rb | 1 + spec/dummy/app/models/user.rb | 4 +-- spec/dummy/bin/bundle | 2 +- spec/dummy/bin/setup | 4 ++- spec/dummy/bin/update | 6 ++-- spec/dummy/bin/yarn | 6 ++-- spec/dummy/config/application.rb | 2 +- spec/dummy/config/environments/development.rb | 11 ++---- spec/dummy/config/environments/production.rb | 11 +++--- spec/dummy/config/environments/test.rb | 8 +---- spec/dummy/config/initializers/assets.rb | 2 +- spec/models/inventory_file_spec.rb | 20 +++++------ spec/models/inventory_spec.rb | 14 ++++---- spec/rails_helper.rb | 6 +--- 20 files changed, 97 insertions(+), 102 deletions(-) diff --git a/app/controllers/inventory_files_controller.rb b/app/controllers/inventory_files_controller.rb index 213ff25..22cbd93 100644 --- a/app/controllers/inventory_files_controller.rb +++ b/app/controllers/inventory_files_controller.rb @@ -16,11 +16,22 @@ def index # GET /inventory_files/1 # GET /inventory_files/1.json def show + if @inventory_file.inventory.path + unless ENV['ENJU_STORAGE'] == 's3' + file = @inventory_file.inventory.path + end + end + respond_to do |format| format.html # show.html.erb format.json { render json: @inventory_file } format.download { - send_data @inventory_file.inventory.download, filename: @inventory_file.inventory.filename.to_s, type: @inventory_file.inventory.content_type, disposition: disposition + if ENV['ENJU_STORAGE'] == 's3' + send_data Faraday.get(@inventory_file.inventory.expiring_url).body.force_encoding('UTF-8'), + filename: File.basename(@inventory_file.inventory_file_name), type: 'application/octet-stream' + else + send_file file, filename: @inventory_file.inventory_file_name, type: 'application/octet-stream' + end } end end diff --git a/app/models/inventory_file.rb b/app/models/inventory_file.rb index 491fdb9..b6dcf1c 100644 --- a/app/models/inventory_file.rb +++ b/app/models/inventory_file.rb @@ -16,7 +16,7 @@ class InventoryFile < ApplicationRecord path: ":rails_root/private/system/:class/:attachment/:id_partition/:style/:filename" end validates_attachment_content_type :inventory, content_type: ['text/csv', 'text/plain', 'text/tab-separated-values'] - validates_attachment_presence :inventory + validates_attachment_presence :inventory, on: :create paginates_per 10 @@ -35,27 +35,8 @@ def import end end end - end - - def export(col_sep: "\t") - file = Tempfile.create do |f| - inventories.each do |inventory| - f.write inventory.to_hash.values.to_csv(col_sep) - end - - f.rewind - f.read - end - - file - end - - def missing_items - Item.where(Inventory.where('items.id = inventories.item_id AND inventories.inventory_file_id = ?', id).exists.not) - end - - def found_items - items + file.close + true end def missing_items @@ -71,11 +52,14 @@ def found_items # # Table name: inventory_files # -# id :bigint not null, primary key -# user_id :bigint +# id :integer not null, primary key +# filename :string +# content_type :string +# size :integer +# user_id :integer # note :text -# created_at :datetime not null -# updated_at :datetime not null +# created_at :datetime +# updated_at :datetime # inventory_file_name :string # inventory_content_type :string # inventory_file_size :integer diff --git a/app/views/inventory_files/edit.html.erb b/app/views/inventory_files/edit.html.erb index 58aa1e1..75c18b2 100644 --- a/app/views/inventory_files/edit.html.erb +++ b/app/views/inventory_files/edit.html.erb @@ -5,6 +5,26 @@ <%= form_for(@inventory_file) do |f| -%> <%= f.error_messages -%> +
+ <%= f.label :inventory_file_name -%>
+ <%= f.text_field :inventory_file_name -%> +
+
+ <%= f.label :inventory_content_type -%>
+ <%= f.text_field :inventory_content_type -%> +
+
+ <%= f.label :inventory_file_size -%>
+ <%= f.text_field :inventory_file_size -%> +
+
+ <%= f.label :inventory_fingerprint -%>
+ <%= f.text_field :inventory_fingerprint -%> +
+
+ <%= f.label :user_id -%>
+ <%= f.text_field :user_id -%> +
<%= f.label :note -%>
<%= f.text_area :note -%> diff --git a/app/views/inventory_files/index.html.erb b/app/views/inventory_files/index.html.erb index 9cb767f..2d70214 100644 --- a/app/views/inventory_files/index.html.erb +++ b/app/views/inventory_files/index.html.erb @@ -12,10 +12,9 @@ <%- @inventory_files.each do |inventory_file| -%> - <% next unless inventory_file.inventory.attached? -%> "> - <%= link_to inventory_file.inventory.filename, inventory_file -%> + <%= link_to inventory_file.inventory_file_name, inventory_file -%>
<%= inventory_file.created_at -%> diff --git a/app/views/inventory_files/show.html.erb b/app/views/inventory_files/show.html.erb index 5e647d5..3262e2b 100644 --- a/app/views/inventory_files/show.html.erb +++ b/app/views/inventory_files/show.html.erb @@ -5,17 +5,22 @@

<%= t('activerecord.attributes.inventory_file.inventory_file_name') -%> : - <%= @inventory_file.inventory.filename -%> + <%= @inventory_file.inventory_file_name -%>

<%= t('activerecord.attributes.inventory_file.inventory_content_type') -%> : - <%= @inventory_file.inventory.content_type -%> + <%= @inventory_file.inventory_content_type -%>

<%= t('activerecord.attributes.inventory_file.inventory_file_size') -%> : - <%= @inventory_file.inventory.byte_size -%> + <%= @inventory_file.inventory_file_size -%> +

+ +

+ <%= t('activerecord.attributes.inventory_file.inventory_fingerprint') -%> : + <%= @inventory_file.inventory_fingerprint -%>

diff --git a/spec/controllers/inventory_files_controller_spec.rb b/spec/controllers/inventory_files_controller_spec.rb index a5fbb7c..661173f 100644 --- a/spec/controllers/inventory_files_controller_spec.rb +++ b/spec/controllers/inventory_files_controller_spec.rb @@ -46,9 +46,8 @@ login_fixture_admin it "assigns the requested inventory_file as @inventory_file" do - get :show, params: { id: inventory_files(:inventory_file_00001) } - expect(assigns(:inventory_file)).to eq(InventoryFile.find(inventory_files(:inventory_file_00001).id)) - expect(response).to be_successful + get :show, params: { id: 1 } + expect(assigns(:inventory_file)).to eq(InventoryFile.find(1)) end end @@ -56,9 +55,8 @@ login_fixture_librarian it "assigns the requested inventory_file as @inventory_file" do - get :show, params: { id: inventory_files(:inventory_file_00001) } - expect(assigns(:inventory_file)).to eq(InventoryFile.find(inventory_files(:inventory_file_00001).id)) - expect(response).to be_successful + get :show, params: { id: 1 } + expect(assigns(:inventory_file)).to eq(InventoryFile.find(1)) end end @@ -66,16 +64,15 @@ login_fixture_user it "assigns the requested inventory_file as @inventory_file" do - get :show, params: { id: inventory_files(:inventory_file_00001) } - expect(assigns(:inventory_file)).to eq(InventoryFile.find(inventory_files(:inventory_file_00001).id)) - expect(response).to be_forbidden + get :show, params: { id: 1 } + expect(assigns(:inventory_file)).to eq(InventoryFile.find(1)) end end describe "When not logged in" do it "assigns the requested inventory_file as @inventory_file" do - get :show, params: { id: inventory_files(:inventory_file_00001) } - expect(assigns(:inventory_file)).to eq(InventoryFile.find(inventory_files(:inventory_file_00001).id)) + get :show, params: { id: 1 } + expect(assigns(:inventory_file)).to eq(InventoryFile.find(1)) expect(response).to redirect_to(new_user_session_url) end end diff --git a/spec/dummy/app/controllers/application_controller.rb b/spec/dummy/app/controllers/application_controller.rb index 0604b45..389545c 100644 --- a/spec/dummy/app/controllers/application_controller.rb +++ b/spec/dummy/app/controllers/application_controller.rb @@ -5,6 +5,7 @@ class ApplicationController < ActionController::Base include EnjuLibrary::Controller include EnjuInventory::Controller after_action :verify_authorized + before_action :set_paper_trail_whodunnit include Pundit end diff --git a/spec/dummy/app/models/user.rb b/spec/dummy/app/models/user.rb index cd053fc..c25e10f 100644 --- a/spec/dummy/app/models/user.rb +++ b/spec/dummy/app/models/user.rb @@ -1,6 +1,6 @@ class User < ActiveRecord::Base - devise :database_authenticatable, # :registerable, - :recoverable, :rememberable, # :validatable + devise :database_authenticatable, #:registerable, + :recoverable, :rememberable, :trackable, # , :validatable :lockable, lock_strategy: :none, unlock_strategy: :none include EnjuSeed::EnjuUser diff --git a/spec/dummy/bin/bundle b/spec/dummy/bin/bundle index f19acf5..66e9889 100755 --- a/spec/dummy/bin/bundle +++ b/spec/dummy/bin/bundle @@ -1,3 +1,3 @@ #!/usr/bin/env ruby -ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__) +ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__) load Gem.bin_path('bundler', 'bundle') diff --git a/spec/dummy/bin/setup b/spec/dummy/bin/setup index 94fd4d7..78c4e86 100755 --- a/spec/dummy/bin/setup +++ b/spec/dummy/bin/setup @@ -1,9 +1,10 @@ #!/usr/bin/env ruby +require 'pathname' require 'fileutils' include FileUtils # path to your application root. -APP_ROOT = File.expand_path('..', __dir__) +APP_ROOT = Pathname.new File.expand_path('../../', __FILE__) def system!(*args) system(*args) || abort("\n== Command #{args} failed ==") @@ -20,6 +21,7 @@ chdir APP_ROOT do # Install JavaScript dependencies if using Yarn # system('bin/yarn') + # puts "\n== Copying sample files ==" # unless File.exist?('config/database.yml') # cp 'config/database.yml.sample', 'config/database.yml' diff --git a/spec/dummy/bin/update b/spec/dummy/bin/update index 58bfaed..a8e4462 100755 --- a/spec/dummy/bin/update +++ b/spec/dummy/bin/update @@ -1,9 +1,10 @@ #!/usr/bin/env ruby +require 'pathname' require 'fileutils' include FileUtils # path to your application root. -APP_ROOT = File.expand_path('..', __dir__) +APP_ROOT = Pathname.new File.expand_path('../../', __FILE__) def system!(*args) system(*args) || abort("\n== Command #{args} failed ==") @@ -17,9 +18,6 @@ chdir APP_ROOT do system! 'gem install bundler --conservative' system('bundle check') || system!('bundle install') - # Install JavaScript dependencies if using Yarn - # system('bin/yarn') - puts "\n== Updating database ==" system! 'bin/rails db:migrate' diff --git a/spec/dummy/bin/yarn b/spec/dummy/bin/yarn index 460dd56..c2bacef 100755 --- a/spec/dummy/bin/yarn +++ b/spec/dummy/bin/yarn @@ -1,8 +1,8 @@ #!/usr/bin/env ruby -APP_ROOT = File.expand_path('..', __dir__) -Dir.chdir(APP_ROOT) do +VENDOR_PATH = File.expand_path('..', __dir__) +Dir.chdir(VENDOR_PATH) do begin - exec "yarnpkg", *ARGV + exec "yarnpkg #{ARGV.join(" ")}" rescue Errno::ENOENT $stderr.puts "Yarn executable was not detected in the system." $stderr.puts "Download Yarn at https://yarnpkg.com/en/docs/install" diff --git a/spec/dummy/config/application.rb b/spec/dummy/config/application.rb index 827efa2..63c24c8 100644 --- a/spec/dummy/config/application.rb +++ b/spec/dummy/config/application.rb @@ -9,7 +9,7 @@ module Dummy class Application < Rails::Application # Initialize configuration defaults for originally generated Rails version. - config.load_defaults 5.2 + config.load_defaults 5.1 # Settings in config/environments/* take precedence over those specified here. # Application configuration should go into files in config/initializers diff --git a/spec/dummy/config/environments/development.rb b/spec/dummy/config/environments/development.rb index 366e75a..55d8c9e 100644 --- a/spec/dummy/config/environments/development.rb +++ b/spec/dummy/config/environments/development.rb @@ -13,13 +13,12 @@ config.consider_all_requests_local = true # Enable/disable caching. By default caching is disabled. - # Run rails dev:cache to toggle caching. - if Rails.root.join('tmp', 'caching-dev.txt').exist? + if Rails.root.join('tmp/caching-dev.txt').exist? config.action_controller.perform_caching = true config.cache_store = :memory_store config.public_file_server.headers = { - 'Cache-Control' => "public, max-age=#{2.days.to_i}" + 'Cache-Control' => "public, max-age=#{2.days.seconds.to_i}" } else config.action_controller.perform_caching = false @@ -27,9 +26,6 @@ config.cache_store = :null_store end - # Store uploaded files on the local file system (see config/storage.yml for options) - config.active_storage.service = :local - # Don't care if the mailer can't send. config.action_mailer.raise_delivery_errors = false @@ -41,9 +37,6 @@ # Raise an error on page load if there are pending migrations. config.active_record.migration_error = :page_load - # Highlight code that triggered database queries in logs. - config.active_record.verbose_query_logs = true - # Debug mode disables concatenation and preprocessing of assets. # This option may cause significant delays in view rendering with a large # number of complex assets. diff --git a/spec/dummy/config/environments/production.rb b/spec/dummy/config/environments/production.rb index 9ea5ad2..6ea0018 100644 --- a/spec/dummy/config/environments/production.rb +++ b/spec/dummy/config/environments/production.rb @@ -14,9 +14,10 @@ config.consider_all_requests_local = false config.action_controller.perform_caching = true - # Ensures that a master key has been made available in either ENV["RAILS_MASTER_KEY"] - # or in config/master.key. This key is used to decrypt credentials (and other encrypted files). - # config.require_master_key = true + # Attempt to read encrypted secrets from `config/secrets.yml.enc`. + # Requires an encryption key in `ENV["RAILS_MASTER_KEY"]` or + # `config/secrets.yml.key`. + config.read_encrypted_secrets = true # Disable serving static files from the `/public` folder by default since # Apache or NGINX already handles this. @@ -38,9 +39,6 @@ # config.action_dispatch.x_sendfile_header = 'X-Sendfile' # for Apache # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for NGINX - # Store uploaded files on the local file system (see config/storage.yml for options) - config.active_storage.service = :local - # Mount Action Cable outside main process or domain # config.action_cable.mount_path = nil # config.action_cable.url = 'wss://example.com/cable' @@ -62,7 +60,6 @@ # Use a real queuing backend for Active Job (and separate queues per environment) # config.active_job.queue_adapter = :resque # config.active_job.queue_name_prefix = "dummy_#{Rails.env}" - config.action_mailer.perform_caching = false # Ignore bad email addresses and do not raise email delivery errors. diff --git a/spec/dummy/config/environments/test.rb b/spec/dummy/config/environments/test.rb index 4f482e1..8e5cbde 100644 --- a/spec/dummy/config/environments/test.rb +++ b/spec/dummy/config/environments/test.rb @@ -15,7 +15,7 @@ # Configure public file server for tests with Cache-Control for performance. config.public_file_server.enabled = true config.public_file_server.headers = { - 'Cache-Control' => "public, max-age=#{1.hour.to_i}" + 'Cache-Control' => "public, max-age=#{1.hour.seconds.to_i}" } # Show full error reports and disable caching. @@ -27,10 +27,6 @@ # Disable request forgery protection in test environment. config.action_controller.allow_forgery_protection = false - - # Store uploaded files on the local file system in a temporary directory - config.active_storage.service = :test - config.action_mailer.perform_caching = false # Tell Action Mailer not to deliver emails to the real world. @@ -43,6 +39,4 @@ # Raises error for missing translations # config.action_view.raise_on_missing_translations = true - config.action_mailer.default_url_options = {host: 'localhost:3000'} - config.active_job.queue_adapter = :inline end diff --git a/spec/dummy/config/initializers/assets.rb b/spec/dummy/config/initializers/assets.rb index c04658e..4b828e8 100644 --- a/spec/dummy/config/initializers/assets.rb +++ b/spec/dummy/config/initializers/assets.rb @@ -6,7 +6,7 @@ # Add additional assets to the asset load path. # Rails.application.config.assets.paths << Emoji.images_path # Add Yarn node_modules folder to the asset load path. -Rails.application.config.assets.paths << Rails.root.join('../../', 'node_modules') +Rails.application.config.assets.paths << Rails.root.join('node_modules') # Precompile additional assets. # application.js, application.css, and all non-JS/CSS in the app/assets diff --git a/spec/models/inventory_file_spec.rb b/spec/models/inventory_file_spec.rb index 42bac57..9991c37 100644 --- a/spec/models/inventory_file_spec.rb +++ b/spec/models/inventory_file_spec.rb @@ -4,28 +4,28 @@ fixtures :users before(:each) do - @file = InventoryFile.create(user: users(:admin)) - @file.inventory.attach(io: File.new("#{Rails.root.to_s}/../../examples/inventory_file_sample.tsv"), filename: 'attachment.txt') + @file = InventoryFile.new inventory: File.new("#{Rails.root.to_s}/../../examples/inventory_file_sample.tsv") + @file.user = users(:admin) + @file.save 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 +# id :integer not null, primary key +# filename :string +# content_type :string +# size :integer +# user_id :integer # note :text -# created_at :datetime not null -# updated_at :datetime not null +# created_at :datetime +# updated_at :datetime # inventory_file_name :string # inventory_content_type :string # inventory_file_size :integer diff --git a/spec/models/inventory_spec.rb b/spec/models/inventory_spec.rb index b49da4e..098200d 100644 --- a/spec/models/inventory_spec.rb +++ b/spec/models/inventory_spec.rb @@ -9,12 +9,10 @@ # # 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 +# id :integer not null, primary key +# item_id :integer +# inventory_file_id :integer +# note :text +# created_at :datetime +# updated_at :datetime # diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 43bcf42..a0108e3 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -1,11 +1,7 @@ require 'simplecov' require 'coveralls' - -SimpleCov.formatters = SimpleCov::Formatter::MultiFormatter.new([ - SimpleCov::Formatter::HTMLFormatter, - Coveralls::SimpleCov::Formatter -]) SimpleCov.start 'rails' +Coveralls.wear! # This file is copied to spec/ when you run 'rails generate rspec:install' ENV["RAILS_ENV"] ||= 'test'