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

Commit

Permalink
Require attachment and retain across invalid submissions
Browse files Browse the repository at this point in the history
  • Loading branch information
JPrevost committed Dec 2, 2015
1 parent 44f8242 commit b42b8b9
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 3 deletions.
2 changes: 1 addition & 1 deletion app/controllers/submissions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ def create
def submission_params
params.require(:submission).permit(:title, :agreed_to_license, :author,
:journal, :doi, :grant_number, :doe,
documents: [])
:documents_cache, documents: [])
end
end
2 changes: 2 additions & 0 deletions app/models/submission.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@
# 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

Expand Down
12 changes: 12 additions & 0 deletions app/views/submissions/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@
<% 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 %>
Expand Down
20 changes: 18 additions & 2 deletions test/features/submission_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,27 @@ def base_valid_form
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')
@sub = Submission.last
assert_equal(0, @sub.documents.count)
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
3 changes: 3 additions & 0 deletions test/fixtures/submissions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@
# agreed_to_license :boolean
# created_at :datetime not null
# updated_at :datetime not null
# documents :string
#

# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

sub_one:
title: 'Popcorn is a fruit.'
agreed_to_license: true
documents: "[\"b_pdf.pdf\"]"
user: one

sub_two:
Expand All @@ -31,3 +33,4 @@ sub_two:
doe: true
grant_number: 'asdf123'
agreed_to_license: true
documents: "[\"b_pdf.pdf\"]"
7 changes: 7 additions & 0 deletions test/models/submission_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# agreed_to_license :boolean
# created_at :datetime not null
# updated_at :datetime not null
# documents :string
#

require 'test_helper'
Expand Down Expand Up @@ -41,6 +42,12 @@ class SubmissionTest < ActiveSupport::TestCase
assert_not sub.valid?
end

test 'invalid without documents' do
sub = submissions(:sub_one)
sub.remove_documents!
assert_not sub.valid?
end

test '#mets' do
Dir.chdir("#{Rails.root}/test/fixtures/schemas") do
sub = submissions(:sub_two)
Expand Down

0 comments on commit b42b8b9

Please sign in to comment.