Skip to content
This repository has been archived by the owner on Oct 16, 2020. It is now read-only.

Commit

Permalink
Merge b42b8b9 into a0df214
Browse files Browse the repository at this point in the history
  • Loading branch information
JPrevost committed Dec 2, 2015
2 parents a0df214 + b42b8b9 commit 6d1cf4d
Show file tree
Hide file tree
Showing 21 changed files with 2,199 additions and 3,100 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -21,3 +21,5 @@ coverage
.byebug_history

*.lock

public/uploads/*
3 changes: 1 addition & 2 deletions Gemfile
Expand Up @@ -3,6 +3,7 @@ ruby '2.2.3'

gem 'rails', '4.2.5'
gem 'bootstrap_form'
gem 'carrierwave', github: 'carrierwaveuploader/carrierwave'
gem 'devise'
gem 'http_logger'
gem 'jquery-rails'
Expand Down Expand Up @@ -36,6 +37,4 @@ group :test do
gem 'minitest-reporters'
gem 'minitest-rails'
gem 'minitest-rails-capybara'
gem 'vcr'
gem 'webmock'
end
39 changes: 19 additions & 20 deletions Gemfile.lock
@@ -1,3 +1,13 @@
GIT
remote: git://github.com/carrierwaveuploader/carrierwave.git
revision: 08d0aea29b058c1b98db1c19f4ccd37c9105e9eb
specs:
carrierwave (0.10.0)
activemodel (>= 3.2.0)
activesupport (>= 3.2.0)
json (>= 1.7)
mime-types (>= 1.16)

GEM
remote: https://rubygems.org/
specs:
Expand Down Expand Up @@ -36,7 +46,6 @@ GEM
minitest (~> 5.1)
thread_safe (~> 0.3, >= 0.3.4)
tzinfo (~> 1.1)
addressable (2.3.8)
annotate (2.6.10)
activerecord (>= 3.2, <= 4.3)
rake (~> 10.4)
Expand All @@ -50,7 +59,7 @@ GEM
debug_inspector (>= 0.0.1)
bootstrap_form (2.3.0)
builder (3.2.2)
byebug (8.2.0)
byebug (8.2.1)
capybara (2.5.0)
mime-types (>= 1.16)
nokogiri (>= 1.3.3)
Expand All @@ -65,8 +74,6 @@ GEM
term-ansicolor (~> 1.3)
thor (~> 0.19.1)
tins (~> 1.6.0)
crack (0.4.2)
safe_yaml (~> 1.0.0)
debug_inspector (0.0.2)
devise (3.5.2)
bcrypt (~> 3.0)
Expand All @@ -84,7 +91,6 @@ GEM
multipart-post (>= 1.2, < 3)
globalid (0.3.6)
activesupport (>= 4.1.0)
hashdiff (0.2.3)
hashie (3.4.3)
http-cookie (1.0.2)
domain_name (~> 0.5)
Expand All @@ -108,9 +114,9 @@ GEM
nokogiri (>= 1.5.9)
mail (2.6.3)
mime-types (>= 1.16, < 3)
mime-types (2.6.2)
mini_portile (0.6.2)
minitest (5.8.2)
mime-types (2.99)
mini_portile2 (2.0.0)
minitest (5.8.3)
minitest-capybara (0.7.2)
capybara (~> 2.2)
minitest (~> 5.0)
Expand All @@ -125,7 +131,7 @@ GEM
minitest-capybara (~> 0.7.0)
minitest-metadata (~> 0.5.0)
minitest-rails (~> 2.1)
minitest-reporters (1.1.5)
minitest-reporters (1.1.7)
ansi
builder
minitest (>= 5.0)
Expand All @@ -134,8 +140,8 @@ GEM
multi_xml (0.5.5)
multipart-post (2.0.0)
netrc (0.11.0)
nokogiri (1.6.6.2)
mini_portile (~> 0.6.0)
nokogiri (1.6.7)
mini_portile2 (~> 2.0.0.rc2)
oauth2 (1.0.0)
faraday (>= 0.8, < 0.10)
jwt (~> 1.0)
Expand Down Expand Up @@ -207,13 +213,12 @@ GEM
ruby-progressbar (~> 1.7)
tins (<= 1.6.0)
ruby-progressbar (1.7.5)
safe_yaml (1.0.4)
simplecov (0.10.0)
docile (~> 1.1.0)
json (~> 1.8)
simplecov-html (~> 0.10.0)
simplecov-html (0.10.0)
sprockets (3.4.0)
sprockets (3.4.1)
rack (> 1, < 3)
sprockets-rails (2.3.3)
actionpack (>= 3.0)
Expand Down Expand Up @@ -242,18 +247,13 @@ GEM
unf (0.1.4)
unf_ext
unf_ext (0.0.7.1)
vcr (3.0.0)
warden (1.2.3)
rack (>= 1.0)
web-console (2.2.1)
activemodel (>= 4.0)
binding_of_caller (>= 0.7.2)
railties (>= 4.0)
sprockets-rails (>= 2.0, < 4.0)
webmock (1.22.3)
addressable (>= 2.3.6)
crack (>= 0.3.2)
hashdiff
xpath (2.0.0)
nokogiri (~> 1.3)

Expand All @@ -264,6 +264,7 @@ DEPENDENCIES
annotate
bootstrap_form
byebug
carrierwave!
coveralls
devise
http_logger
Expand All @@ -284,9 +285,7 @@ DEPENDENCIES
therubyracer
twitter-bootstrap-rails
uglifier (>= 1.3.0)
vcr
web-console (~> 2.0)
webmock

BUNDLED WITH
1.10.6
3 changes: 2 additions & 1 deletion app/controllers/submissions_controller.rb
Expand Up @@ -21,6 +21,7 @@ def create

def submission_params
params.require(:submission).permit(:title, :agreed_to_license, :author,
:journal, :doi, :grant_number, :doe)
:journal, :doi, :grant_number, :doe,
:documents_cache, documents: [])
end
end
4 changes: 4 additions & 0 deletions app/models/submission.rb
Expand Up @@ -13,13 +13,17 @@
# agreed_to_license :boolean
# created_at :datetime not null
# updated_at :datetime not null
# documents :string
#

class Submission < ActiveRecord::Base
belongs_to :user
validates :user, presence: true
validates :title, presence: true
validates :agreed_to_license, inclusion: { in: [true] }
validates :documents, presence: true
mount_uploaders :documents, DocumentUploader
serialize :documents, JSON

def to_mets
Mets.new(self).to_xml
Expand Down
50 changes: 50 additions & 0 deletions app/uploaders/document_uploader.rb
@@ -0,0 +1,50 @@
# encoding: utf-8

class DocumentUploader < CarrierWave::Uploader::Base
# Include RMagick or MiniMagick support:
# include CarrierWave::RMagick
# include CarrierWave::MiniMagick

# Choose what kind of storage to use for this uploader:
storage :file
# storage :fog

# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end

# Provide a default URL as a default if there hasn't been a file uploaded:
# def default_url
# # For Rails 3.1+ asset pipeline compatibility:
# # ActionController::Base.helpers.asset_path(
# "fallback/" + [version_name, "default.png"].compact.join('_'))
#
# "/images/fallback/" + [version_name, "default.png"].compact.join('_')
# end

# Process files as they are uploaded:
# process scale: [200, 300]
#
# def scale(width, height)
# # do something
# end

# Create different versions of your uploaded files:
# version :thumb do
# process resize_to_fit: [50, 50]
# end

# Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this:
def extension_white_list
%w(pdf)
end

# Override the filename of the uploaded files:
# Avoid using model.id or version_name here, see uploader/store.rb.
# def filename
# "something.jpg" if original_filename
# end
end
14 changes: 14 additions & 0 deletions app/views/submissions/new.html.erb
Expand Up @@ -17,6 +17,20 @@
label: "I am authorized to submit this article." %>
<% end %>
<%= f.file_field :documents, multiple: true %>
<%= f.hidden_field :documents_cache %>
<% if @submission.documents? %>
<%= f.form_group :documents do %>
Already Uploaded Documents for this Submission:
<ul>
<% @submission.documents.each do |doc| %>
<li><%= doc.filename %></li>
<% end %>
</ul>
<% end %>
<% end %>
<%= f.form_group do %>
<%= f.submit %>
<% end %>
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20151130204610_add_documents_to_submissions.rb
@@ -0,0 +1,5 @@
class AddDocumentsToSubmissions < ActiveRecord::Migration
def change
add_column :submissions, :documents, :string
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20151116184904) do
ActiveRecord::Schema.define(version: 20151130204610) do

create_table "submissions", force: :cascade do |t|
t.integer "user_id"
Expand All @@ -24,6 +24,7 @@
t.boolean "agreed_to_license"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "documents"
end

add_index "submissions", ["user_id"], name: "index_submissions_on_user_id"
Expand Down
69 changes: 55 additions & 14 deletions test/features/submission_test.rb
Expand Up @@ -11,6 +11,7 @@ def setup
def teardown
OmniAuth.config.test_mode = false
OmniAuth.config.mock_auth[:mit_oauth2] = nil
@sub.documents.map(&:remove!) if @sub
end

def mock_auth
Expand All @@ -21,6 +22,14 @@ def mock_auth
visit '/users/auth/mit_oauth2/callback'
end

def base_valid_form
mock_auth
visit new_submission_path
fill_in('Journal', with: 'Super Mega Journal')
fill_in('Title', with: 'Alphabetical Order is Good Enough')
check('I am authorized to submit this article.')
end

test 'requires signed_in user' do
visit new_submission_path
assert_equal(root_path, current_path)
Expand All @@ -29,15 +38,14 @@ def mock_auth
end

test 'authenticated users can view the form' do
mock_auth
visit new_submission_path
base_valid_form
assert_equal(new_submission_path, current_path)
end

test 'invalid form retains valid portions' do
mock_auth
visit new_submission_path
fill_in('Journal', with: 'Super Mega Journal')
base_valid_form
fill_in('Title', with: '')
uncheck('I am authorized to submit this article.')
click_on('Create Submission')
assert_text('Please fix the errors below')
assert_text("Title can't be blank")
Expand All @@ -46,25 +54,58 @@ def mock_auth
end

test 'invalid form submit does not create new submissions' do
mock_auth
subs = Submission.count
visit new_submission_path
fill_in('Journal', with: 'Super Mega Journal')
base_valid_form
uncheck('I am authorized to submit this article.')
click_on('Create Submission')
assert_equal(Submission.count, subs)
end

test 'valid form creates new submission' do
mock_auth
subs = Submission.count
visit new_submission_path
fill_in('Journal', with: 'Super Mega Journal')
fill_in('Title', with: 'Alphabetical Order is Good Enough')
check('I am authorized to submit this article.')
base_valid_form
attach_file('submission[documents][]',
File.absolute_path('./test/fixtures/a_pdf.pdf'))
click_on('Create Submission')
assert_equal(Submission.count, (subs + 1))
assert_equal('bob@asdf.com', Submission.last.user.email)
@sub = Submission.last
assert_equal('bob@asdf.com', @sub.user.email)
assert_equal(root_path, current_path)
assert_text('Your Submission is now in progress')
end

test 'multiple pdfs can be attached' do
base_valid_form
attach_file('submission[documents][]',
[File.absolute_path('./test/fixtures/a_pdf.pdf'),
File.absolute_path('./test/fixtures/b_pdf.pdf')])
click_on('Create Submission')
@sub = Submission.last
assert_equal(2, @sub.documents.count)
end

test 'non pdf cannot be attached' do
subs = Submission.count
base_valid_form
attach_file('submission[documents][]',
File.absolute_path('./test/fixtures/a.txt'))
click_on('Create Submission')
assert_equal(Submission.count, subs)
assert_text('You are not allowed to upload "txt" files, allowed types: pdf')
end

test 'pdf retained across invalid form submission' do
subs = Submission.count
base_valid_form
attach_file('submission[documents][]',
File.absolute_path('./test/fixtures/a_pdf.pdf'))
uncheck('I am authorized to submit this article.')
click_on('Create Submission')
assert_equal(Submission.count, subs)
assert_text('a_pdf.pdf')
check('I am authorized to submit this article.')
click_on('Create Submission')
assert_equal(Submission.count, (subs + 1))
assert('a_pdf.pdf', Submission.last.documents.first.filename)
end
end
1 change: 1 addition & 0 deletions test/fixtures/a.txt
@@ -0,0 +1 @@
This is a text file.
Binary file added test/fixtures/a_pdf.pdf
Binary file not shown.
Binary file added test/fixtures/b_pdf.pdf
Binary file not shown.

0 comments on commit 6d1cf4d

Please sign in to comment.