Skip to content

Commit

Permalink
Implements support for providing users with the ability to revert Wor…
Browse files Browse the repository at this point in the history
…ks (#1912)

awaiting approval to draft Works
  • Loading branch information
jrgriffiniii authored Aug 23, 2024
1 parent a8ab55b commit 0535e76
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 0 deletions.
7 changes: 7 additions & 0 deletions app/controllers/works_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,13 @@ def resubmit
redirect_to work_path(@work)
end

def revert_to_draft
@work = Work.find(params[:id])
@work.revert_to_draft!(current_user)

redirect_to work_path(@work)
end

def assign_curator
work = Work.find(params[:id])
work.change_curator(params[:uid], current_user)
Expand Down
4 changes: 4 additions & 0 deletions app/decorators/work_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ def show_approve_button?
work.awaiting_approval? && current_user_is_admin?
end

def show_revert_button?
work.awaiting_approval? && (work.created_by_user_id == current_user.id || current_user_is_admin?)
end

def show_complete_button?
draft? && (work.created_by_user_id == current_user.id || current_user_is_admin?)
end
Expand Down
4 changes: 4 additions & 0 deletions app/models/work.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ class InvalidGroupError < ::ArgumentError; end
transitions from: :awaiting_approval, to: :awaiting_approval, guard: :valid_to_submit
end

event :revert_to_draft do
transitions from: :awaiting_approval, to: :draft, guard: :valid_to_draft
end

event :approve do
transitions from: :awaiting_approval, to: :approved, guard: :valid_to_approve, after: :publish
end
Expand Down
3 changes: 3 additions & 0 deletions app/views/works/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@
<% if @work_decorator.show_approve_button? %>
<%= button_to("Approve Dataset", approve_work_path(@work), class: "btn btn-secondary", method: :post, id: "approve-button") %>
<% end %>
<% if @work_decorator.show_revert_button? %>
<%= button_to("Revert Dataset to Draft", revert_work_path(@work), class: "btn btn-secondary", method: :post) %>
<% end %>
<% if @work_decorator.show_complete_button? %>
<%= button_to("Complete", work_validate_path(@work), class: "btn btn-secondary", method: :post) %>
<% end %>
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
post "work/:id/approve", to: "works#approve", as: :approve_work
post "work/:id/withdraw", to: "works#withdraw", as: :withdraw_work
post "work/:id/resubmit", to: "works#resubmit", as: :resubmit_work
post "work/:id/revert-to-draft", to: "works#revert_to_draft", as: :revert_work
post "work/:id/add-message", to: "works#add_message", as: :add_message_work
post "work/:id/add-provenance-note", to: "works#add_provenance_note", as: :add_provenance_note
put "works/:id/assign-curator/:uid", to: "works#assign_curator", as: :work_assign_curator
Expand Down
16 changes: 16 additions & 0 deletions spec/models/work_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1048,6 +1048,22 @@
end
end

describe "#revert_to_draft" do
subject(:work) { FactoryBot.create(:awaiting_approval_work, doi: "10.34770/123-abc") }

it "permits a user to return a Work awaiting approval to the draft state" do
work
expect(work.state).to eq("awaiting_approval")
expect(work.activities).to be_empty

work.revert_to_draft!(curator_user)
expect(work.state).to eq("draft")
expect(work.activities).not_to be_empty
work_activity = work.activities.first
expect(work_activity.message).to eq("marked as Draft")
end
end

describe "valid?" do
it "requires a group" do
work = Work.new(created_by_user_id: user.id, group_id: nil, user_entered_doi: false)
Expand Down
53 changes: 53 additions & 0 deletions spec/system/work_show_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,57 @@
expect(page).to have_select(:curator_select, selected: user.full_name_safe)
end
end

describe "reverting a Work awaiting approval" do
let(:work) { FactoryBot.create(:awaiting_approval_work) }

context "as a user with super admin privileges" do
let(:user) { FactoryBot.create :super_admin_user }

before do
sign_in(user)
visit work_path(work)
end

it "sets the Work state to draft" do
expect(page).to have_button("Revert Dataset to Draft")
click_on("Revert Dataset to Draft")
expect(page).not_to have_button("Revert Dataset to Draft")
expect(page).to have_button("Complete")
expect(page).to have_content("marked as Draft")
end
end

context "as a moderator user" do
let(:user) { FactoryBot.create :research_data_moderator }

before do
sign_in(user)
visit work_path(work)
end

it "sets the Work state to draft" do
expect(page).to have_button("Revert Dataset to Draft")
click_on("Revert Dataset to Draft")
expect(page).not_to have_button("Revert Dataset to Draft")
expect(page).to have_button("Complete")
expect(page).to have_content("marked as Draft")
end
end

context "as the submitter user" do
before do
sign_in(user)
visit work_path(work)
end

it "sets the Work state to draft" do
expect(page).to have_button("Revert Dataset to Draft")
click_on("Revert Dataset to Draft")
expect(page).not_to have_button("Revert Dataset to Draft")
expect(page).to have_button("Complete")
expect(page).to have_content("marked as Draft")
end
end
end
end

0 comments on commit 0535e76

Please sign in to comment.