-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allowing the new submission form to save and stay on page (#1727)
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
1 parent
2da1a30
commit d876ab3
Showing
6 changed files
with
78 additions
and
15 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,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 |
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