Skip to content

Commit

Permalink
JS interactions
Browse files Browse the repository at this point in the history
  • Loading branch information
awead committed Jul 22, 2016
1 parent f4045ec commit 06632fb
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
//= require curation_concerns/file_manager/member
//= require curation_concerns/batch_select
//= require curation_concerns/collections
//= require curation_concerns/single_use_links


// Initialize plugins and Bootstrap dropdowns on jQuery's ready event as well as
Expand Down
28 changes: 28 additions & 0 deletions app/assets/javascripts/curation_concerns/single_use_links.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
Blacklight.onLoad(function () {

function updateLinkRows() {
var url = $('table.single-use-links tbody').data('url')
$.get(url).done(function(data) {
$('table.single-use-links tbody').html(data);
});
}

$('.generate-single-use-link').click(function(event) {
event.preventDefault();
$.post($(this).attr('href')).done(function(data) {
updateLinkRows();
});
return false;
});

$('table.single-use-links').on('click', '.delete-single-use-link', function(event) {
event.preventDefault();
$.ajax({
url: $(this).attr('href'),
type: 'DELETE',
done: $(this).parent('td').parent('tr').remove()
});
return false;
});

});
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ def create_show
end

def index
@links = SingleUseLink.where(itemId: params[:id])
links = SingleUseLink.where(itemId: params[:id])
render partial: 'curation_concerns/file_sets/single_use_link_rows', locals: { single_use_links: links }
end

def destroy
SingleUseLink.find_by_downloadKey(params[:link_id]).destroy
redirect_to action: 'index'
head :ok
end

protected
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<% single_use_links.each_with_index do |link, index| %>
<tr>
<td><%= link.path =~ /downloads/ ? 'Download' : 'Show' %></td>
<td><%= link.downloadKey %></td>
<td><%= link.expires %></td>
<td>
<%= link_to "Copy to Clipboard", "/", class: 'btn btn-xs btn-default' %>
<%= link_to "Delete", curation_concerns.delete_single_use_link_path(params[:id], link),
class: 'btn btn-xs btn-danger delete-single-use-link' %>
</td>
</tr>
<% end %>
21 changes: 5 additions & 16 deletions app/views/curation_concerns/file_sets/_single_use_links.html.erb
Original file line number Diff line number Diff line change
@@ -1,31 +1,20 @@
<table class="table table-striped <%= dom_class(presenter) %> single_use_links">
<table class="table table-striped <%= dom_class(presenter) %> single-use-links">
<caption class="table-heading"><h2>Single-Use Links</h2></caption>
<thead>
<tr><th>Link</th><th>Key</th><th>Expires</th><th>Actions</th></tr>
</thead>
<tbody>
<tbody data-url="<%= curation_concerns.generated_single_use_links_path(presenter) %>">
<% if presenter.single_use_links.empty? %>
<tr><td>No links have been genereated</td></tr>
<% else %>
<% presenter.single_use_links.each do |link| %>
<tr>
<td><%= link.path =~ /downloads/ ? 'Download' : 'Show' %></td>
<td><%= link.downloadKey %></td>
<td><%= link.expires %></td>
<td>
<%= link_to "Copy to Clipboard", "/", class: 'btn btn-xs btn-default' %>
<%= link_to "Delete", delete_single_use_link_path(presenter, link),
method: :delete, class: 'btn btn-xs btn-danger' %>
</td>
</tr>
<% end %>
<%= render partial: 'single_use_link_rows', locals: { single_use_links: presenter.single_use_links } %>
<% end %>
</tbody>
</table>

<div class="form_actions">
<%= link_to "Generate Download Link", curation_concerns.generate_download_single_use_link_path(presenter),
method: :post, class: 'btn btn-default' %>
class: 'btn btn-default generate-single-use-link' %>
<%= link_to "Generate Show Link", curation_concerns.generate_show_single_use_link_path(presenter),
method: :post, class: 'btn btn-default' %>
class: 'btn btn-default generate-single-use-link' %>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
let!(:link) { create(:download_link) }
it "deletes the link" do
expect { delete :destroy, id: file, link_id: link }.to change { SingleUseLink.count }.by(-1)
expect(response).to be_redirect
expect(response).to be_success
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@
describe 'single use links' do
before do
allow(presenter).to receive(:single_use_links).and_return(links)
controller.params = { id: presenter.id }
render
end

Expand Down

0 comments on commit 06632fb

Please sign in to comment.