Skip to content

Commit

Permalink
Allowing the new submission form to save and stay on page (#1727)
Browse files Browse the repository at this point in the history
This was more complex than expected, becuase some times the work is persisted and sometimes not now that we can stay on the page.

For this reason I extracted some code into the WorkMetadataService.
  • Loading branch information
carolyncole authored Apr 1, 2024
1 parent 2da1a30 commit d876ab3
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 15 deletions.
1 change: 1 addition & 0 deletions app/assets/stylesheets/application.scss
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ th {

.actions .btn-primary {
background-color: #467dd1ff;
margin-right: 0.5em;
}

.submit-ordered-list {
Expand Down
13 changes: 5 additions & 8 deletions app/controllers/works_wizard_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,12 @@ def new_submission
prepare_decorators_for_work_form(@work)
end

# Creates the new dataset
# POST /works/1/new_submission
# Creates the new dataset or update the dataset is save only was done previously
# POST /works/new_submission or POST /works/1/new_submission
def new_submission_save
group = Group.find_by(code: params[:group_code]) || current_user.default_group
group_id = group.id
@work = Work.new(created_by_user_id: current_user.id, group_id:)
@work.resource = FormToResourceService.convert(params, @work)
@work.draft!(current_user)
if params[:save_only] == "true"
@work = WorkMetadataService.new(params:, current_user:).new_submission
@errors = @work.errors.to_a
if params[:save_only] == "true" || @errors.count.positive?
prepare_decorators_for_work_form(@work)
render :new_submission
else
Expand Down
50 changes: 50 additions & 0 deletions app/services/work_metadata_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# frozen_string_literal: true

# A place to move the logic to from the work controllers
# Process the parameters and permissions to update a work
class WorkMetadataService
attr_reader :params, :current_user

# @params [User] current_user the user who is currently logged in
# @param [HashWithIndifferentAccess] update_params values to update the work with
def initialize(params:, current_user:)
@params = params
@current_user = current_user
end

# generates or load the work for a new submission based on the parameters
#
#
# @returns the new or updated work
#
def new_submission
if params[:id].present?
update_work
else
draft_work
end
end

private

def update_work
work = Work.find(params[:id])
update_params = { group: group_code, resource: FormToResourceService.convert(params, work) }
WorkCompareService.update_work(work:, update_params:, current_user:)
work
end

def draft_work
group_id = group_code.id
work = Work.new(created_by_user_id: current_user.id, group_id:)
work.resource = FormToResourceService.convert(params, work)
if work.valid_to_draft
work.draft!(current_user)
end
work
end

def group_code
@group_code ||= Group.find_by(code: params[:group_code]) || current_user.default_group
end
end
3 changes: 2 additions & 1 deletion app/views/works_wizard/new_submission.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,15 @@
your submission, we may be reached at <a href="mailto:prds@princeton.edu">prds@princeton.edu</a>.
</p>

<%= form_tag(action: "new_submission_save") do %>
<%= form_with(model: @work, url: work_new_submission_path(@work)) do |form| %>
<%= render(partial: 'works/required_title', locals: {allow_many: false}) %>
<%= render(partial: 'works/required_creators_table') %>
<%= render '/works/form_hidden_fields' %>
<hr />
<div class="actions">
<%= link_to "Cancel", root_path, class: "btn btn-secondary" %>
<%= submit_tag "Next", class: "btn btn-primary wizard-next-button", id: "btn-create-new" %>
<%= form.button 'Save', type: 'submit', name: 'save_only', value: 'true', class: "btn btn-primary wizard-next-button" %>
</div>
<% end %>
</div>
Expand Down
3 changes: 2 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@

# The work wizard
get "works/new-submission", to: "works_wizard#new_submission", as: :work_create_new_submission
post "works/new-submission", to: "works_wizard#new_submission_save", as: :work_new_submission
post "works/new-submission/(:id)", to: "works_wizard#new_submission_save", as: :work_new_submission
patch "works/new-submission/:id", to: "works_wizard#new_submission_save"
get "works/:id/readme-select", to: "works_wizard#readme_select", as: :work_readme_select
patch "works/:id/readme-uploaded", to: "works_wizard#readme_uploaded", as: :work_readme_uploaded
patch "works/:id/file-upload", to: "works_wizard#file_uploaded", as: :work_file_uploaded
Expand Down
23 changes: 18 additions & 5 deletions spec/controllers/works_wizard_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,9 @@
it "renders the edit page when creating a new dataset without a title" do
sign_in user
post(:new_submission_save, params: params_no_title)
expect(response.status).to be 302
# rubocop:disable Layout/LineLength
expect(assigns[:errors]).to eq(["We apologize, the following errors were encountered: Must provide a title. Please contact the PDC Describe administrators for any assistance."])
# rubocop:enable Layout/LineLength
expect(response).to redirect_to(work_create_new_submission_path)
expect(response.status).to be 200
expect(assigns[:errors]).to eq(["Must provide a title"])
expect(response).to render_template(:new_submission)
end
end

Expand All @@ -74,6 +72,21 @@
expect(response.status).to be 200
expect(response).to render_template(:new_submission)
end

context "when the user has hit save and the work has already been preserved" do
let(:work) { FactoryBot.create :draft_work }

it "renders the correct page" do
sign_in user
work_params = save_only_params.merge(id: work.id)
expect { post(:new_submission_save, params: work_params) }
.to change { Work.count }.by(0)
.and change { WorkActivity.count }.by(1)
expect(response.status).to be 200
expect(response).to render_template(:new_submission)
expect(work.reload.title).to eq("test dataset updated")
end
end
end
end

Expand Down

0 comments on commit d876ab3

Please sign in to comment.