Skip to content

Commit

Permalink
Merge pull request #4286 from sanger/Y24-221-tag-set-pages
Browse files Browse the repository at this point in the history
Y24-221 - Adds tag set pages
  • Loading branch information
BenTopping authored Sep 4, 2024
2 parents ce2939b + 7057ef4 commit 862edf4
Show file tree
Hide file tree
Showing 15 changed files with 292 additions and 5 deletions.
3 changes: 2 additions & 1 deletion app/controllers/tag_layout_templates_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ def show
##
# Allows for the passing in of tag group id using a link from the tag group show page.
def new
@tag_layout_template = TagLayoutTemplate.new(tag_group_id: params[:tag_group_id])
@tag_layout_template =
TagLayoutTemplate.new(tag_group_id: params[:tag_group_id], tag2_group_id: params[:tag2_group_id])
@direction_algorithms = TagLayout::DIRECTION_ALGORITHMS
@walking_algorithms = TagLayout::WALKING_ALGORITHMS

Expand Down
42 changes: 42 additions & 0 deletions app/controllers/tag_sets_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# frozen_string_literal: true
##
# This class is the controller for Tag Sets which are used to link together two related tag groups.
# It allows you to create and view Tag Sets.
class TagSetsController < ApplicationController
authorize_resource

def index
@tag_sets = TagSet.all

respond_to { |format| format.html }
end

def show
@tag_set = TagSet.find(params[:id])

respond_to { |format| format.html }
end

def new
@tag_set = TagSet.new

respond_to { |format| format.html }
end

def create
@tag_set = TagSet.new(tag_set_params)

respond_to do |format|
if @tag_set.save
flash[:notice] = 'Tag Set was successfully created.'
format.html { redirect_to tag_set_path(@tag_set) }
else
format.html { render action: :new }
end
end
end

def tag_set_params
params.require(:tag_set).permit(:name, :tag_group_id, :tag2_group_id)
end
end
2 changes: 1 addition & 1 deletion app/models/ability/base_user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def grant_privileges # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
can :print_asset_group_labels, Study, owners: { id: user.id }
can :print_asset_group_labels, Study, managers: { id: user.id }
can %i[read create update edit], Submission
can :read, [TagGroup, TagLayoutTemplate]
can :read, [TagGroup, TagLayoutTemplate, TagSet]
can %i[read update print_swipecard], User, { id: user.id }
can %i[projects study_reports], User

Expand Down
3 changes: 2 additions & 1 deletion app/models/ability/shared/tag_creation_user.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

# Prepend to an ability class to include Tag Creation User privileges
# Govern the ability to created tag groups and layouts
# Govern the ability to create tag groups, layouts and sets
module Ability::Shared::TagCreationUser
def grant_privileges
super
Expand All @@ -11,5 +11,6 @@ def grant_privileges
Rails.logger.debug { 'Granting TagCreationUser privileges' }
can :manage, TagGroup
can :manage, TagLayoutTemplate
can :manage, TagSet
end
end
1 change: 0 additions & 1 deletion app/resources/api/v2/tag_layout_template_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ class TagLayoutTemplateResource < BaseResource
# A relationship for the primary tag group associated with the tag layout template.
# @return [Api::V2::TagGroupResource]
has_one :tag_group
has_one :tag2_group

# @!attribute [r] tag2_group
# A relationship for the secondary tag group associated with the tag layout template.
Expand Down
1 change: 1 addition & 0 deletions app/views/homes/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<% add :menu, 'Plate Summaries' => plate_summaries_path %>
<% add :menu, 'Create Custom Pools' => new_pooling_path %>
<% add :menu, 'Status of Tube Rack Imports' => tube_rack_statuses_path %>
<% add :menu, 'Tag Sets' => tag_sets_path %>

<!-- External links -->
<% @links.each do |name, link| %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/tag_layout_templates/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

<div class="form-group">
<%= f.label :tag2_group, 'Tag2 Group' %><br>
<%= f.select :tag2_group_id, TagGroup.visible.pluck(:name, :id), { prompt: 'Select tag2 group...' }, { class: 'form-control select2' } %>
<%= f.select :tag2_group_id, TagGroup.visible.pluck(:name, :id), { prompt: 'Select tag2 group...', selected: @tag_layout_template.tag2_group_id }, { class: 'form-control select2' } %>
</div>

<div>
Expand Down
26 changes: 26 additions & 0 deletions app/views/tag_sets/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

<div class="page-header"><h1>Listing Tag Sets</h1></div>

<table class="table">
<thead>
<th>Name</th>
<th>Tag group (i7)</th>
<th>Tag group (i5)</th>
<th></th>
</thead>
<tbody>
<% for tag_set in @tag_sets %>
<tr>
<td><%= tag_set.name %></td>
<td><%= tag_set.tag_group.name %></td>
<td><%= tag_set.tag2_group&.name %></td>
<td><%= link_to 'Show', tag_set_path(tag_set.id) %></td>
</tr>
<% end %>
</tbody>
</table>


<% if can? :create, TagSet %>
<%= link_to 'Create a new Tag Set', new_tag_set_path %>
<% end %>
26 changes: 26 additions & 0 deletions app/views/tag_sets/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

<div class="page-header"><h1>New Tag Set</h1></div>

<%= form_for @tag_set, url: tag_sets_path do |f| %>
<%= render_error_messages(@tag_set) %>
<div class='form-group'>
<%= f.label :name, 'Tag Set name' %><br>
<%= f.text_field :name, class: 'form-control' %>
</div>

<div class="form-group">
<%= f.label :tag_group_id, 'Tag Group (i7)' %><br/>
<%= f.select(:tag_group_id, TagGroup.pluck(:name, :id), { prompt: 'Unspecified' }, class: 'form-control select2' )%>
</div>

<div class="form-group">
<%= f.label :tag2_group_id, 'Tag Group (i5)' %><br/>
<%= f.select(:tag2_group_id, TagGroup.pluck(:name, :id), { prompt: 'Unspecified' }, class: 'form-control select2' )%>
</div>

<div class="form-group actions-group">
<%= f.submit('Create tag set', class: 'btn btn-success') %>
</div>
<% end %>
<%= link_to 'Back to tag sets list', tag_sets_path %>
17 changes: 17 additions & 0 deletions app/views/tag_sets/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<% add :menu, "All Tag Sets" => tag_sets_path(@asset) %>
<%= page_title('Tag Set', @tag_set.name) %>

<dl>
<dt>Name</dt><dd><%= @tag_set.name %></dd>
<dt>Tag group (i7)</dt><dd><%= link_to @tag_set.tag_group.name, @tag_set.tag_group %></dd>
<dt>Tag group (i5)</dt><dd><%= @tag_set.tag2_group ? (link_to @tag_set.tag2_group&.name, @tag_set.tag2_group) : 'None'%></dd>
</dl>

<br>
<% if can? :create, TagLayoutTemplate %>
<%= link_to 'Create a new tag layout template from this tag set', new_tag_layout_template_path(tag_group_id: @tag_set.tag_group.id, tag2_group_id: @tag_set.tag2_group&.id) %>
<br>
<% end %>
<%= link_to 'Back to tag sets list', tag_sets_path %>
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,8 @@
resources :tags, except: %i[destroy index create new edit]
end

resources :tag_sets, only: %i[index new create show]

resources :tag_layout_templates, only: %i[index new create show]

resources :assets, except: %i[create new] do
Expand Down
92 changes: 92 additions & 0 deletions spec/controllers/tag_sets_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe TagSetsController do
let(:current_user) { create :user }
let(:tag_set) { create :tag_set }

it_behaves_like 'it requires login'

context 'when admin' do
before { session[:user] = create :admin }

describe '#index' do
before { get :index }

it 'is successful' do
expect(response).to have_http_status(:success)
expect(response).to render_template('index')
end
end

describe '#show' do
let(:tag_set) { create :tag_set }

before { get :show, params: { id: tag_set.id } }

it 'is successful' do
expect(response).to have_http_status(:success)
expect(response).to render_template('show')
end
end

describe '#new' do
before { get :new }

it 'is successful' do
expect(response).to have_http_status(:success)
expect(response).to render_template('new')
end
end

describe '#create' do
it 'redirects to the tag set show path' do
post :create, params: { tag_set: { name: 'test-123', tag_group_id: create(:tag_group).id } }
expect(response).to redirect_to tag_set_path(TagSet.last)
end

it 'renders the new template when there are errors' do
post :create, params: { tag_set: { name: 'test-123', tag_group_id: 'bad ID' } }
# Successful status because we handle and display the errors and remain on the new page
expect(response).to have_http_status(:success)
expect(response).to render_template('new')
end
end
end

context 'when a non-admin' do
before { session[:user] = create :user }

describe '#index' do
it 'redirects' do
get :index
expect(response).to have_http_status(:success)
expect(response).to render_template('index')
end
end

describe '#new' do
it 'redirects' do
get :new
expect(response).to redirect_to('/')
end
end

describe '#show' do
it 'redirects' do
tag_set = create(:tag_set)
get :show, params: { id: tag_set.id }
expect(response).to have_http_status(:success)
expect(response).to render_template('show')
end
end

describe '#create' do
it 'redirects' do
post :create, params: attributes_for(:tag_set)
expect(response).to redirect_to('/')
end
end
end
end
22 changes: 22 additions & 0 deletions spec/features/tag_layout_template_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
let(:user) { create :admin }
let(:tag_group_1) { create(:tag_group_with_tags, name: 'Test tag group 1') }
let(:tag_group_2) { create(:tag_group_with_tags, name: 'Test tag group 2') }
let(:tag_set_1) { create(:tag_set, name: 'Test tag set 1') }

before do
tag_group_1
tag_group_2
tag_set_1
end

it 'create a new layout template from a tag group', :js do
Expand All @@ -32,6 +34,26 @@
expect(page).to have_content 'To tag layout templates list'
end

it 'create a new layout template from a tag set', :js do
login_user user
visit tag_set_path(tag_set_1)
expect(page).to have_content 'Test tag set 1'
click_on 'Create a new tag layout template from this tag set'
expect(page).to have_content 'Tag Layout Template New'
within('#new_tag_layout_template') do
fill_in('tag_layout_template_name', with: 'Test tag layout template')
select('column', from: 'tag_layout_template_direction_algorithm')
click_on 'Create tag layout template'
end
expect(page).to have_content 'The Tag Layout Template has been successfully created.'
expect(page).to have_content 'Name: Test tag layout template'
expect(page).to have_content "Tag Group: #{tag_set_1.tag_group.name}"
expect(page).to have_content "Tag2 Group: #{tag_set_1.tag2_group.name}"
expect(page).to have_content 'Direction the tags are laid out by: column'
expect(page).to have_content 'Walking by: wells of plate'
expect(page).to have_content 'To tag layout templates list'
end

it 'create a new layout template directly', :js do
login_user user
visit new_tag_layout_template_path
Expand Down
55 changes: 55 additions & 0 deletions spec/features/tag_set_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# frozen_string_literal: true

require 'rails_helper'

describe 'Create a new tag set' do
let(:user) { create :admin }
let(:adapter_type) { create :adapter_type }
let(:tag_group) { create :tag_group, name: 'test-group-1', adapter_type: adapter_type }
let(:tag2_group) { create :tag_group, name: 'test-group-2', adapter_type: adapter_type }

before do
tag_group
tag2_group
end

it 'view tag sets and create a new valid one' do
login_user user
visit tag_sets_path
expect(page).to have_content 'Listing Tag Sets'
click_on 'Create a new Tag Set'
expect(page).to have_content 'New Tag Set'
fill_in('tag_set_name', with: 'Test tag set')
select(tag_group.name, from: 'Tag Group (i7)')
select(tag2_group.name, from: 'Tag Group (i5)')
click_on 'Create tag set'
expect(page).to have_content 'Tag Set was successfully created.'
expect(page).to have_content 'Test tag set'
expect(page).to have_content tag_group.name
expect(page).to have_content tag2_group.name
expect(page).to have_content 'Create a new tag layout template from this tag set'
end

it 'view tag groups and attempt to create a new one with an existing tag set name' do
create(:tag_set, name: 'Test tag set')

login_user user
visit tag_sets_path
expect(page).to have_content 'Listing Tag Sets'
click_on 'Create a new Tag Set'
expect(page).to have_content 'New Tag Set'
fill_in('tag_set_name', with: 'Test tag set')
select(tag_group.name, from: 'Tag Group (i7)')
select(tag2_group.name, from: 'Tag Group (i5)')
click_on 'Create tag set'
expect(page).to have_content 'error prohibited this tag set from being saved'
expect(page).to have_content 'Name has already been taken'
fill_in('tag_set_name', with: 'Test tag set 1')
click_on 'Create tag set'
expect(page).to have_content 'Tag Set was successfully created.'
expect(page).to have_content 'Test tag set 1'
expect(page).to have_content tag_group.name
expect(page).to have_content tag2_group.name
expect(page).to have_content 'Create a new tag layout template from this tag set'
end
end
3 changes: 3 additions & 0 deletions spec/models/ability_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
Submission => %i[create read edit update delete change_priority new order_fields study_assets],
Supplier => %i[create new],
TagGroup => %i[create read edit delete],
TagSet => %i[create read edit delete],
TagLayoutTemplate => %i[create read edit delete],
User => %i[administer edit read projects study_reports create delete]
}
Expand All @@ -96,6 +97,7 @@
Submission => %i[create read update new order_fields study_assets edit],
TagGroup => %i[read],
TagLayoutTemplate => %i[read],
TagSet => %i[read],
User => %i[edit read projects study_reports print_swipecard]
}
end
Expand Down Expand Up @@ -194,6 +196,7 @@ def merge_permissions(*permissions)
Submission => %i[delete change_priority],
Supplier => %i[create new],
TagGroup => %i[create edit delete],
TagSet => %i[create read edit delete],
TagLayoutTemplate => %i[create edit delete],
User => %i[administer create delete]
}
Expand Down

0 comments on commit 862edf4

Please sign in to comment.