-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
UI for adding workflow roles. Fixes #1037
- Loading branch information
Showing
14 changed files
with
282 additions
and
21 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
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,18 @@ | ||
module CurationConcerns | ||
module AdminPage | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
cattr_accessor :configuration | ||
self.configuration = CurationConcerns.config.dashboard_configuration | ||
before_action :load_configuration | ||
layout "admin" | ||
end | ||
|
||
private | ||
|
||
def load_configuration | ||
@configuration = self.class.configuration.with_indifferent_access | ||
end | ||
end | ||
end |
36 changes: 36 additions & 0 deletions
36
app/controllers/curation_concerns/admin/workflow_roles_controller.rb
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,36 @@ | ||
module CurationConcerns | ||
module Admin | ||
class WorkflowRolesController < ApplicationController | ||
include AdminPage | ||
before_action :require_permissions | ||
|
||
def index | ||
@presenter = WorkflowRolePresenter.new | ||
end | ||
|
||
def destroy | ||
responsibility = Sipity::WorkflowResponsibility.find(params[:id]) | ||
authorize! :destroy, responsibility | ||
responsibility.destroy | ||
redirect_to admin_workflow_roles_path | ||
end | ||
|
||
def create | ||
authorize! :create, Sipity::WorkflowResponsibility | ||
form = Forms::WorkflowResponsibilityForm.new(params[:sipity_workflow_responsibility]) | ||
begin | ||
form.save! | ||
rescue ActiveRecord::RecordNotUnique | ||
logger.info "Not unique *****\n\n\n" | ||
end | ||
redirect_to admin_workflow_roles_path | ||
end | ||
|
||
private | ||
|
||
def require_permissions | ||
authorize! :read, :admin_dashboard | ||
end | ||
end | ||
end | ||
end |
42 changes: 42 additions & 0 deletions
42
app/forms/curation_concerns/forms/workflow_responsibility_form.rb
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,42 @@ | ||
module CurationConcerns | ||
module Forms | ||
class WorkflowResponsibilityForm | ||
def initialize(params = {}) | ||
model_instance.workflow_role_id = params[:workflow_role_id] | ||
if params[:user_id] | ||
user = ::User.find(params[:user_id]) | ||
model_instance.agent = user.to_sipity_agent | ||
end | ||
end | ||
|
||
def model_instance | ||
@model ||= Sipity::WorkflowResponsibility.new | ||
end | ||
|
||
def to_model | ||
model_instance | ||
end | ||
|
||
delegate :model_name, :to_key, :workflow_role_id, :persisted?, :save!, to: :model_instance | ||
|
||
def user_id | ||
nil | ||
end | ||
|
||
def user_options | ||
::User.all | ||
end | ||
|
||
# The select options for choosing a responsibility | ||
def workflow_role_options | ||
Sipity::WorkflowRole.all.map { |wf_role| [label(wf_role), wf_role.id] } | ||
end | ||
|
||
private | ||
|
||
def label(wf_role) | ||
"#{wf_role.workflow.name} - #{wf_role.role.name}" | ||
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
40 changes: 40 additions & 0 deletions
40
app/presenters/curation_concerns/admin/workflow_role_presenter.rb
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,40 @@ | ||
module CurationConcerns | ||
module Admin | ||
class WorkflowRolePresenter | ||
def users | ||
::User.all | ||
end | ||
|
||
def presenter_for(user) | ||
agent = user.sipity_agent | ||
return unless agent | ||
AgentPresenter.new(agent) | ||
end | ||
|
||
class AgentPresenter | ||
def initialize(agent) | ||
@agent = agent | ||
end | ||
|
||
def responsibilities | ||
@agent.workflow_responsibilities.each do |responsibility| | ||
yield ResponsibilityPresenter.new(responsibility) | ||
end | ||
end | ||
end | ||
|
||
class ResponsibilityPresenter | ||
def initialize(responsibility) | ||
@responsibility = responsibility | ||
@wf_role = responsibility.workflow_role | ||
end | ||
|
||
attr_accessor :responsibility | ||
|
||
def label | ||
"#{@wf_role.workflow.name} - #{@wf_role.role.name}" | ||
end | ||
end | ||
end | ||
end | ||
end |
45 changes: 45 additions & 0 deletions
45
app/views/curation_concerns/admin/workflow_roles/index.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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<div class="<%= "dashboard-#{action_name}" %>"> | ||
<div class="wrapper"> | ||
<div class="widget widget-default"> | ||
<table class="table table-striped"> | ||
<thead> | ||
<th><%= t('.header.user') %></th> | ||
<th><%= t('.header.roles') %></th> | ||
</thead> | ||
<tbody> | ||
<% @presenter.users.each do |user| %> | ||
<tr> | ||
<td><%= user.user_key %></td> | ||
<% agent_presenter = @presenter.presenter_for(user) %> | ||
<% if agent_presenter %> | ||
<td> | ||
<ul> | ||
<% agent_presenter.responsibilities do |responsibility_presenter| %> | ||
<li><%= responsibility_presenter.label %> | ||
<%= link_to admin_workflow_role_path(responsibility_presenter.responsibility), | ||
method: :delete, | ||
data: { confirm: t('.delete.confirm') } do %> | ||
<span class="delete">×</span> | ||
<% end %> | ||
</li> | ||
<% end %> | ||
</ul> | ||
</td> | ||
<% else %> | ||
<td><%= t('.no_roles') %></td> | ||
<% end %> | ||
</tr> | ||
<% end %> | ||
</tbody> | ||
</table> | ||
|
||
<h2><%= t('.new_role') %></h2> | ||
<%= simple_form_for CurationConcerns::Forms::WorkflowResponsibilityForm.new, url: admin_workflow_roles_path do |f| %> | ||
<%= f.input :user_id, as: :select, collection: f.object.user_options %> | ||
<%= f.input :workflow_role_id, as: :select, collection: f.object.workflow_role_options %> | ||
<%= f.submit %> | ||
<% end %> | ||
</div> | ||
</div> | ||
</div> | ||
|
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
26 changes: 26 additions & 0 deletions
26
spec/controllers/curation_concerns/admin/workflow_roles_controller_spec.rb
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,26 @@ | ||
require 'spec_helper' | ||
|
||
RSpec.describe CurationConcerns::Admin::WorkflowRolesController, :no_clean do | ||
routes { CurationConcerns::Engine.routes } | ||
|
||
describe "#get" do | ||
context "when you have permission" do | ||
before do | ||
allow(controller).to receive(:authorize!).with(:read, :admin_dashboard).and_return(true) | ||
end | ||
|
||
it "works" do | ||
get :index | ||
expect(response).to be_success | ||
expect(assigns[:presenter]).to be_kind_of CurationConcerns::Admin::WorkflowRolePresenter | ||
end | ||
end | ||
|
||
context "when they don't have permission" do | ||
it "throws a CanCan error" do | ||
get :index | ||
expect(response).to redirect_to new_user_session_path | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
require 'spec_helper' | ||
|
||
RSpec.describe "Manage workflow roles", type: :feature do | ||
let(:user) { create(:user) } | ||
before do | ||
allow_any_instance_of(CurationConcerns::Admin::WorkflowRolesController).to receive(:authorize!).with(:read, :admin_dashboard).and_return(true) | ||
CurationConcerns::Workflow::WorkflowImporter.generate_from_json_file(path: "#{EngineCart.destination}/config/workflows/generic_work_workflow.json") | ||
CurationConcerns::Workflow::PermissionGenerator.call(roles: Sipity::Role.all, | ||
workflow: Sipity::Workflow.last, | ||
agents: user) | ||
end | ||
|
||
it "shows the roles" do | ||
visit '/admin/workflow_roles' | ||
expect(page).to have_content 'generic_work - reviewing' | ||
end | ||
end |
33 changes: 33 additions & 0 deletions
33
spec/forms/curation_concerns/forms/workflow_responsibility_form_spec.rb
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,33 @@ | ||
require 'spec_helper' | ||
|
||
describe CurationConcerns::Forms::WorkflowResponsibilityForm, :no_clean do | ||
let(:instance) { described_class.new } | ||
|
||
describe "#initialize" do | ||
let(:user) { create(:user) } | ||
let(:instance) { described_class.new(user_id: user.id, workflow_role_id: 7) } | ||
subject { instance.model_instance } | ||
it "creates an agent and sets the workflow_role_id" do | ||
expect(subject.agent).to be_kind_of Sipity::Agent | ||
expect(subject.workflow_role_id).to eq 7 | ||
end | ||
end | ||
|
||
describe "#user_options" do | ||
subject { instance.user_options } | ||
it { is_expected.to eq User.all } | ||
end | ||
|
||
describe "#workflow_role_options" do | ||
let(:workflow) { instance_double(Sipity::Workflow, name: 'generic_work') } | ||
let(:role1) { instance_double(Sipity::Role, name: 'foo') } | ||
let(:role2) { instance_double(Sipity::Role, name: 'bar') } | ||
let(:wf_role1) { instance_double(Sipity::WorkflowRole, workflow: workflow, role: role1, id: 1) } | ||
let(:wf_role2) { instance_double(Sipity::WorkflowRole, workflow: workflow, role: role2, id: 2) } | ||
before do | ||
allow(Sipity::WorkflowRole).to receive(:all).and_return([wf_role1, wf_role2]) | ||
end | ||
subject { instance.workflow_role_options } | ||
it { is_expected.to eq [['generic_work - foo', 1], ['generic_work - bar', 2]] } | ||
end | ||
end |