diff --git a/app/controllers/works_controller.rb b/app/controllers/works_controller.rb index 3b947c95f..9803fe013 100644 --- a/app/controllers/works_controller.rb +++ b/app/controllers/works_controller.rb @@ -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) diff --git a/app/decorators/work_decorator.rb b/app/decorators/work_decorator.rb index eaeb17541..458b7b517 100644 --- a/app/decorators/work_decorator.rb +++ b/app/decorators/work_decorator.rb @@ -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 diff --git a/app/models/work.rb b/app/models/work.rb index 5083e4209..980b5a364 100644 --- a/app/models/work.rb +++ b/app/models/work.rb @@ -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 diff --git a/app/views/works/show.html.erb b/app/views/works/show.html.erb index 26a084370..c03051517 100644 --- a/app/views/works/show.html.erb +++ b/app/views/works/show.html.erb @@ -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 %> diff --git a/config/routes.rb b/config/routes.rb index 7086eb1fe..ddaaa1f18 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 diff --git a/spec/models/work_spec.rb b/spec/models/work_spec.rb index 3c088e8a4..3e210e809 100644 --- a/spec/models/work_spec.rb +++ b/spec/models/work_spec.rb @@ -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) diff --git a/spec/system/work_show_spec.rb b/spec/system/work_show_spec.rb index fb7c11b71..634ad6d25 100644 --- a/spec/system/work_show_spec.rb +++ b/spec/system/work_show_spec.rb @@ -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