Skip to content

Commit

Permalink
upload files into a collection starting from its show page
Browse files Browse the repository at this point in the history
  • Loading branch information
elrayle committed Sep 14, 2015
1 parent d7d9b3c commit c4c6f8e
Show file tree
Hide file tree
Showing 11 changed files with 135 additions and 8 deletions.
1 change: 1 addition & 0 deletions History.md
Expand Up @@ -5,6 +5,7 @@
* Add configuration allowing select menu on batch upload to upload files to a collection. [E. Lynette Rayle]
* Bump active-fedora version to 9.4 [E. Lynette Rayle]
* Bump hydra-collections to 5.0.3 [E. Lynette Rayle]
* Add button 'Upload files' on collection show page which goes to batch upload with collection select menu set to the calling collection [E. Lynette Rayle]

## 6.3.0

Expand Down
5 changes: 5 additions & 0 deletions app/assets/stylesheets/sufia/_collections.scss
Expand Up @@ -56,6 +56,11 @@
margin: 1em 0 0 1em;
}

.actions-controls-collections-line2 {
display: block;
margin: .5em 0 0 1em;
}

.label-default a:link,
.label-default a:visited,
.label-default a:hover {
Expand Down
5 changes: 5 additions & 0 deletions app/controllers/concerns/sufia/files_controller_behavior.rb
Expand Up @@ -64,6 +64,7 @@ def load_resource_from_solr

# routed to /files/new
def new
set_variables_for_new_form
@batch_id = ActiveFedora::Noid::Service.new.mint
end

Expand Down Expand Up @@ -157,6 +158,10 @@ def update

protected

def set_variables_for_new_form
@collection_id = params[:collection] if @user_collections.collect(&:id).include? params[:collection]
end

def set_variables_for_edit_form
@form = edit_form
@groups = current_user.groups
Expand Down
9 changes: 7 additions & 2 deletions app/views/collections/_show_actions.html.erb
@@ -1,7 +1,12 @@
<h2 class="sr-only">Actions</h2>
<div class="actions-controls-collections">
<% if can? :edit, @collection %>
<span class="label label-default"><%= link_to "Edit", collections.edit_collection_path, title: "Edit this Collection" %></span> &nbsp;&nbsp;
<span class="label label-default"><%= link_to "Add files", sufia.dashboard_files_path( add_files_to_collection: @collection.id ), title: "Add files to this Collection" %></span>
<span class="label label-default"><%= link_to t("sufia.collection.show.edit.label"), collections.edit_collection_path, title: t("sufia.collection.show.edit.label") %></span> &nbsp;&nbsp;
<span class="label label-default"><%= link_to t("sufia.collection.show.add_files.label"), sufia.dashboard_files_path( add_files_to_collection: @collection.id ), title: t("sufia.collection.show.add_files.label") %></span>
<% if Sufia.config.upload_to_collection %>
<div class="actions-controls-collections-line2">
<span class="label label-default"><%= link_to t("sufia.collection.show.upload_files.label"), sufia.new_generic_file_path( collection: @collection.id ), title: t("sufia.collection.show.upload_files.desc") %></span>
</div>
<%end %>
<%end %>
</div>
2 changes: 1 addition & 1 deletion app/views/generic_files/upload/_to_collection.html.erb
@@ -1,5 +1,5 @@
<p>
<label><%= t("sufia.upload.collection.select_label") %></label>
<%= select_tag 'collection', options_from_collection_for_select(@user_collections, "id", "title") %>
<%= select_tag 'collection', options_from_collection_for_select(@user_collections, "id", "title", @collection_id) %>
</p>
<p>&nbsp;</p>
10 changes: 10 additions & 0 deletions config/locales/sufia.en.yml
Expand Up @@ -163,6 +163,16 @@ en:
add_another_keyword: "add another Keyword"
add_another_creator: "add another Creator"
add_another_rights: "add another Rights"
show:
edit:
label: "Edit"
desc: "Edit metadata for this Collection"
add_files:
label: "Add files"
desc: "Add files to this Collection"
upload_files:
label: "Upload files"
desc: "Upload files to this Collection"
mailbox:
date: 'Date'
subject: 'Subject'
Expand Down
35 changes: 35 additions & 0 deletions spec/actors/generic_file/actor_spec.rb
Expand Up @@ -69,6 +69,41 @@
expect(VersionCommitter.where(version_id: versions.last.uri).pluck(:committer_login)).to eq [second_user.user_key]
end
end

context "with collection" do
let(:file) { "world.png" }
let(:actor) { described_class.new(generic_file, user) }
let(:col_editable) do
Collection.new(title: 'editable', description: 'user can edit this collection') do |c|
c.apply_depositor_metadata(user)
end
end
let(:col_editable_id) { col_editable.id }
let(:col_not_editable) { Collection.new(title: 'not editable', description: 'user cannot edit this collection') }
let(:col_not_editable_id) { col_not_editable.id }
before do
allow(generic_file).to receive(:label).and_return(file)
allow(col_editable).to receive(:id).and_return('ce')
allow(Collection).to receive(:find).with(col_editable_id).and_return(col_editable)
allow(user).to receive(:can?).with(:edit, col_editable).and_return(true)
allow(col_not_editable).to receive(:id).and_return('cne')
allow(Collection).to receive(:find).with(col_not_editable_id).and_return(col_not_editable)
allow(user).to receive(:can?).with(:edit, col_not_editable).and_return(false)
allow(Sufia.queue).to receive(:push)
end

it "adds file to collection when user can edit the collection" do
actor.create_content(fixture_file_upload(file), file, 'content', 'image/png', col_editable_id)
updated_collection = Collection.find(col_editable_id)
expect(updated_collection.member_ids).to eq [generic_file.id]
end

it "does not add file to collection when user can NOT edit the collection" do
actor.create_content(fixture_file_upload(file), file, 'content', 'image/png', col_not_editable_id)
updated_collection = Collection.find(col_not_editable_id)
expect(updated_collection.member_ids).to eq []
end
end
end

describe "#virus_check" do
Expand Down
19 changes: 19 additions & 0 deletions spec/controllers/generic_files_controller_spec.rb
Expand Up @@ -21,6 +21,8 @@
end
}
let(:collection_id) { collection.id }
let(:collection_noedit) { Collection.create(title: 'test collection - NO EDIT') }
let(:collection_noedit_id) { collection_noedit.id }
let(:file) { fixture_file_upload('/world.png', 'image/png') }

before do
Expand All @@ -45,6 +47,18 @@
end
end

context "when user tries to upload to a collection they can't edit" do
# This shouldn't happen via the UI which will only present to the user collections they can edit.
it "adds new file but does not put the file in the collection" do
xhr :post, :create, files: [file], Filename: "The world", batch_id: batch_id, permission: { "group" => { "public" => "read" } }, terms_of_service: "1", collection: collection_noedit_id
expect(response).to be_success

updated_collection = Collection.find(collection_id)
# This is confirming that the file was NOT added to the collection the user cannot edit
expect(updated_collection.member_ids).to eq []
end
end

context "when everything is perfect" do
render_views
it "spawns a content deposit event job" do
Expand Down Expand Up @@ -89,6 +103,11 @@
expect(saved_file.to_solr['depositor_tesim']).to eq ['jilluser@example.com']
end

it "adds new file when collection is instructions id" do
xhr :post, :create, files: [file], Filename: "The world", batch_id: batch_id, permission: { "group" => { "public" => "read" } }, terms_of_service: "1", collection: '-1'
expect(response).to be_success
end

it "adds new file to collection" do
xhr :post, :create, files: [file], Filename: "The world", batch_id: batch_id, permission: { "group" => { "public" => "read" } }, terms_of_service: "1", collection: collection_id
expect(response).to be_success
Expand Down
19 changes: 19 additions & 0 deletions spec/features/collection_spec.rb
Expand Up @@ -212,6 +212,25 @@ def create_collection(title, description)
end
end

describe 'upload files to collection' do
let(:upload_to_collection) { true }
let!(:gf1) { gfs[0] }
let!(:gf2) { gfs[1] }

before do
Sufia.config.upload_to_collection = upload_to_collection
collection1 # create collections by referencing them
collection2
sign_in user
end

it "preselects the collection we are uploading files to" do
visit "/collections/#{collection1.id}"
click_link 'Upload files'
expect(page).to have_select('collection', selected: title1)
end
end

describe 'edit collection' do
let!(:collection) do
Collection.create(title: 'collection title', description: 'collection description',
Expand Down
35 changes: 31 additions & 4 deletions spec/views/generic_file/new.html.erb_spec.rb
Expand Up @@ -2,9 +2,9 @@

describe 'generic_files/new.html.erb', type: :view do
let(:user_collections) do
default_option = SolrDocument.new(id: -1, title_tesim: "Select collection...")
col1 = SolrDocument.new(id: "c1", title_tesim: "col1")
col2 = SolrDocument.new(id: "c2", title_tesim: "col2")
default_option = SolrDocument.new(id: -1, title_tesim: 'Select collection...')
col1 = SolrDocument.new(id: 'c1', title_tesim: 'col1')
col2 = SolrDocument.new(id: 'c2', title_tesim: 'col2')
[default_option, col1, col2]
end
let(:generic_file) { stub_model(GenericFile, id: '123') }
Expand All @@ -16,7 +16,6 @@
assign(:batch_id, batch_id)
assign(:user_collections, user_collections)
allow(controller).to receive(:current_user).and_return(stub_model(User))
allow(GenericFilesController).to receive(:upload_complete_path).with(batch_id).and_return("foo")
Sufia.config.upload_to_collection = upload_to_collection
end

Expand All @@ -27,6 +26,34 @@
render
page = Capybara::Node::Simple.new(rendered)
expect(page).to have_selector('select#collection', count: 2) # one per tab
expect(page).to have_select('collection', options: ['Select collection...', 'col1', 'col2'])
end

context 'and passed a default collection' do
context "and the collection is on the user's list of editable collections" do
before do
assign(:collection_id, 'c2')
end

it 'has default collection selected' do
render
page = Capybara::Node::Simple.new(rendered)
expect(page).to have_select('collection', options: ['Select collection...', 'col1', 'col2'], selected: 'col2')
end
end

context "and the collection is NOT on the user's list of editable collections" do
before do
assign(:collection_id, 'c3_noedit')
end

it 'has instructions selected' do
render
page = Capybara::Node::Simple.new(rendered)
expect(page).to have_select('collection', options: ['Select collection...', 'col1', 'col2'])
expect(page).not_to have_select('collection', selected: 'col3_noedit')
end
end
end
end

Expand Down
3 changes: 2 additions & 1 deletion sufia-models/app/actors/sufia/generic_file/actor.rb
Expand Up @@ -41,8 +41,9 @@ def create_content(file, file_name, path, mime_type, collection_id = nil)
end

def add_file_to_collection(collection_id)
return if collection_id.nil? || collection_id == -1
return if collection_id.nil? || collection_id == "-1"
collection = Collection.find(collection_id)
return unless user.can? :edit, collection
collection.add_members [generic_file.id]
collection.save
end
Expand Down

0 comments on commit c4c6f8e

Please sign in to comment.