-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2035 from sul-dlss/tags_controller
Extract TagsController from ItemsController
- Loading branch information
Showing
13 changed files
with
143 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# frozen_string_literal: true | ||
|
||
# This controller is responsible for managing tags on an item | ||
class TagsController < ApplicationController | ||
def update | ||
@object = Dor.find params[:item_id] | ||
authorize! :manage_item, @object | ||
|
||
current_tags = tags_client.list | ||
|
||
if params[:add] | ||
tags = params.slice(:new_tag1, :new_tag2, :new_tag3).values.reject(&:empty?) | ||
tags_client.create(tags: tags) if tags.any? | ||
end | ||
|
||
if params[:del] | ||
tag_to_delete = current_tags[params[:tag].to_i - 1] | ||
raise 'failed to delete' unless tags_client.destroy(tag: tag_to_delete) | ||
end | ||
|
||
if params[:update] | ||
count = 1 | ||
current_tags.each do |tag| | ||
tags_client.update(current: tag, new: params["tag#{count}".to_sym]) | ||
count += 1 | ||
end | ||
end | ||
|
||
reindex | ||
respond_to do |format| | ||
msg = "Tags for #{params[:item_id]} have been updated!" | ||
format.any { redirect_to solr_document_path(params[:item_id]), notice: msg } | ||
end | ||
end | ||
|
||
def edit | ||
@pid = params[:item_id] | ||
@tags = tags_client.list | ||
|
||
respond_to do |format| | ||
format.html { render layout: !request.xhr? } | ||
end | ||
end | ||
|
||
private | ||
|
||
def tags_client | ||
Dor::Services::Client.object(params[:item_id]).administrative_tags | ||
end | ||
|
||
def reindex | ||
Argo::Indexer.reindex_pid_remotely(params[:item_id]) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<%= form_with url: item_tags_path(item_id: @pid, update: 'true'), method: 'patch' do %> | ||
<% @tags.each_with_index do |tag, count| %> | ||
<div class="form-group"> | ||
<div class="input-group"> | ||
<input class="form-control tag" name="tag<%= count + 1 %>" id="tag<%= count + 1 %>" type="text" value="<%= tag %>"> | ||
<span class='input-group-btn'> | ||
<%= link_to item_tags_path(item_id: @pid, del: 'true', tag: count + 1), | ||
method: 'patch', | ||
class: 'btn btn-default' do %> | ||
<span class='glyphicon glyphicon-remove glyphicon text-danger'></span> | ||
<% end %> | ||
</span> | ||
</div> | ||
</div> | ||
<% end %> | ||
<button class='btn btn-primary'>Update</button> | ||
<% end %> | ||
<hr> | ||
<%= form_tag item_tags_path(item_id: @pid, add: 'true'), method: 'patch' do %> | ||
<div class='form-group'> | ||
<label>Add a tag</label> | ||
<input class="form-control" name="new_tag1" id="new_tag1" type="text" value=""> | ||
<input class="form-control" name="new_tag2" id="new_tag2" type="text" value=""> | ||
<input class="form-control" name="new_tag3" id="new_tag3" type="text" value=""> | ||
</div> | ||
<button class='btn btn-primary'>Add</button> | ||
<% end %> |
2 changes: 1 addition & 1 deletion
2
app/views/items/tags_ui.html.erb → app/views/tags/edit.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
<%= render BlacklightModalComponent.new do |component| %> | ||
<% component.with(:header, 'Update tags or delete a tag') %> | ||
<% component.with(:body) { render 'tags_ui' } %> | ||
<% component.with(:body) { render 'edit' } %> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
RSpec.describe TagsController, type: :controller do | ||
before do | ||
sign_in user | ||
allow(Dor).to receive(:find).with(pid).and_return(item) | ||
allow(Argo::Indexer).to receive(:reindex_pid_remotely) | ||
end | ||
|
||
let(:pid) { 'druid:bc123df4567' } | ||
let(:item) { Dor::Item.new pid: pid } | ||
let(:user) { create(:user) } | ||
|
||
describe '#update' do | ||
context 'when they have manage access' do | ||
let(:current_tag) { 'Some : Thing' } | ||
let(:tags_client) do | ||
instance_double(Dor::Services::Client::AdministrativeTags, | ||
list: [current_tag], | ||
update: true, | ||
destroy: true, | ||
create: true) | ||
end | ||
|
||
before do | ||
allow(controller).to receive(:authorize!).and_return(true) | ||
allow(controller).to receive(:tags_client).and_return(tags_client) | ||
end | ||
|
||
it 'updates tags' do | ||
post :update, params: { item_id: pid, update: 'true', tag1: 'Some : Thing : Else' } | ||
expect(tags_client).to have_received(:update).with(current: current_tag, new: 'Some : Thing : Else') | ||
expect(Argo::Indexer).to have_received(:reindex_pid_remotely) | ||
end | ||
|
||
it 'deletes tag' do | ||
post :update, params: { item_id: pid, tag: '1', del: 'true' } | ||
expect(tags_client).to have_received(:destroy).with(tag: current_tag) | ||
expect(Argo::Indexer).to have_received(:reindex_pid_remotely) | ||
end | ||
|
||
it 'adds a tag' do | ||
post :update, params: { item_id: pid, new_tag1: 'New : Thing', add: 'true' } | ||
expect(Argo::Indexer).to have_received(:reindex_pid_remotely) | ||
expect(tags_client).to have_received(:create).with(tags: ['New : Thing']) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters