Skip to content

Commit

Permalink
Separating edit into wizard and non wizard controller methods
Browse files Browse the repository at this point in the history
  • Loading branch information
carolyncole committed Mar 25, 2024
1 parent 4af772b commit be62f96
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 49 deletions.
54 changes: 37 additions & 17 deletions app/controllers/works_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def new_submission_save
@work = Work.new(created_by_user_id: current_user.id, group_id:)
@work.resource = FormToResourceService.convert(params, @work)
@work.draft!(current_user)
redirect_to edit_work_path(@work, wizard: true)
redirect_to edit_work_wizard_path(@work)
end

##
Expand Down Expand Up @@ -131,28 +131,27 @@ def resolve_ark
redirect_to @work
end

# GET /works/1/edit_wizard
# only wizard
def edit_wizard
@work = Work.find(params[:id])
@work_decorator = WorkDecorator.new(@work, current_user)
@wizard_mode = true
if handle_edit_permissions
@form_resource_decorator = FormResourceDecorator.new(@work, current_user)
end
end

# GET /works/1/edit
# rubocop:disable Metrics/MethodLength
# Both wizard and not wizard mode
# only non wizard mode
def edit
@work = Work.find(params[:id])
@work_decorator = WorkDecorator.new(@work, current_user)
if current_user && @work.editable_by?(current_user)
if @work.approved? && !@work.administered_by?(current_user)
Honeybadger.notify("Can not edit work: #{@work.id} is approved but #{current_user.uid} is not admin")
redirect_to root_path, notice: I18n.t("works.uneditable.approved")
else
@uploads = @work.uploads
@wizard_mode = wizard_mode?
@form_resource_decorator = FormResourceDecorator.new(@work, current_user)
render "edit"
end
else
Honeybadger.notify("Can not edit work: #{@work.id} is not editable by #{current_user.uid}")
redirect_to root_path, notice: I18n.t("works.uneditable.privs")
if handle_edit_permissions
@uploads = @work.uploads
@form_resource_decorator = FormResourceDecorator.new(@work, current_user)
end
end
# rubocop:enable Metrics/MethodLength

# Both wizard and not wizard mode
def update
Expand Down Expand Up @@ -575,5 +574,26 @@ def migrated?

params[:submit] == "Migrate"
end

# @returns false if an error occured
def handle_edit_permissions
no_error = false
byebug
if current_user.blank? || !@work.editable_by?(current_user)
Honeybadger.notify("Can not edit work: #{@work.id} is not editable by #{current_user.uid}")
redirect_to root_path, notice: I18n.t("works.uneditable.privs")
elsif !@work.editable_in_current_state?(current_user)
Honeybadger.notify("Can not edit work: #{@work.id} is not editable in current state by #{current_user.uid}")
redirect_to root_path, notice: I18n.t("works.uneditable.approved")
redirect_to root_path, notice: I18n.t("works.uneditable.approved")
end
redirect_to root_path, notice: I18n.t("works.uneditable.approved")
end
else
no_error = true
end

no_error
end
end
# rubocop:enable Metrics/ClassLength
31 changes: 1 addition & 30 deletions app/views/works/edit.html.erb
Original file line number Diff line number Diff line change
@@ -1,35 +1,6 @@
<style>
.input-text-long {
width: 700px;
}

.input-text-year {
width: 70px;
}

.section-title {
padding-top: 20px;
}

.doi-text {
font-size: large;
font-style: italic;
}

.add-title-button {
color: #0c68f0;
font-weight: bold;
font-size: 22px;
}
</style>

<div class="wizard-area">
<% if @wizard_mode %>
<h1>New Submission</h1>
<%= render "wizard_progress", wizard_step: 1 %>
<% else %>
<h1>Editing Dataset</h1>
<% end %>
<h1>Editing Dataset</h1>
<%= render 'form' %>
</div>

Expand Down
7 changes: 7 additions & 0 deletions app/views/works/edit_wizard.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<div class="wizard-area">
<h1>New Submission</h1>
<%= render "wizard_progress", wizard_step: 1 %>
<%= render 'form' %>
</div>

<%= javascript_include_tag 'edit_work_utils' %>
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
get "works/:id/attachment-select", to: "works#attachment_select", as: :work_attachment_select
post "works/:id/attachment-select", to: "works#attachment_selected", as: :work_attachment_selected
patch "works/:id/file-upload", to: "works#file_uploaded", as: :work_file_uploaded
get "works/:id/edit-wizard", to: "works#edit_wizard", as: :edit_work_wizard
get "works/:id/file-upload", to: "works#file_upload", as: :work_file_upload
get "works/:id/file-other", to: "works#file_other", as: :work_file_other
get "works/:id/review", to: "works#review", as: :work_review
Expand Down
4 changes: 2 additions & 2 deletions spec/system/work_create_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@
context "when no description is provided" do
let(:resource) { FactoryBot.build(:resource, description: nil) }
let(:work) do
FactoryBot.create(:new_work, created_by_user_id: user.id, resource:)
FactoryBot.create(:new_draft_work, created_by_user_id: user.id, resource:)
end
it "renders a warning", js: true do
sign_in user
Expand Down Expand Up @@ -215,7 +215,7 @@
end
it "renders a warning", js: true do
sign_in user
visit edit_work_path(work, params: { wizard: true })
visit edit_work_wizard_path(work)

expect(work.resource.related_objects.count).to eq(0)

Expand Down

0 comments on commit be62f96

Please sign in to comment.