Skip to content
This repository has been archived by the owner on Dec 28, 2023. It is now read-only.

Commit

Permalink
612: Fixes questionnaire form for edit (#644)
Browse files Browse the repository at this point in the history
* 612: Fixes questionnaire form for edit

* 612: Fixes questionnaire edit form - fix

* 612: test fixes

* 612: Requested fixes

---------

Co-authored-by: Lucas Boeira <lucas.boeira@protonmail.ch>
  • Loading branch information
Lucas-Boeira and Lucas Boeira authored Dec 21, 2023
1 parent 85b2e89 commit f658ab5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
12 changes: 11 additions & 1 deletion app/controllers/new_admin/questionnaires_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ def edit
def update
@questionnaire = Questionnaire.find(params[:id])

if @questionnaire.update(questionnaire_params)
if @questionnaire.update(questionnaire_params) &&
@questionnaire.questions.destroy_by(id: questions_to_be_destroyed_ids)
redirect_on_success new_admin_show_questionnaire_path(id: @questionnaire.id), message_scope: 'update'
else
render_on_failure :edit
Expand Down Expand Up @@ -67,6 +68,15 @@ def questionnaire_params
questions_attributes: %i[title kind raw_answer_options _destroy id])
end

def questions_to_be_destroyed_ids
return unless questionnaire_params[:questions_attributes]
questionnaire_params[:questions_attributes].values.filter_map do |question|
if question["_destroy"] != 'false'
question["id"]
end
end
end

def redirect_on_success(url, message_scope:)
flash[:notice] = I18n.t(:notice, scope: "flash.actions.#{message_scope}",
resource_name: Questionnaire.model_name.human)
Expand Down
14 changes: 9 additions & 5 deletions app/views/new_admin/questionnaires/_question_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<div class="w-full overflow-hidden rounded-lg ring-1 ring-black ring-opacity-5" data-new-record="<%= form.object.new_record? %>">
<div class="mt-6 w-full overflow-hidden rounded-lg ring-1 ring-black ring-opacity-5"
data-new-record="<%= form.object.new_record? %>">
<div class="w-full overflow-x-auto">
<table class="w-full whitespace-no-wrap" id="form_questionnaire">
<thead>
Expand Down Expand Up @@ -53,10 +54,13 @@
</tbody>
</table>
</div>

<div class="flex mt-4">
<button type="button" class="px-4 py-2 cursor-pointer font-semibold text-sm text-white transition-colors duration-150 rounded-lg bg-primary-600 hover:bg-primary-700" data-action="click->nested-form#remove"> <%= t 'questionnaires.remove_question' %> </button>
</div>

<div class="flex mt-2">
<button type="button"
class="px-4 py-2 cursor-pointer font-semibold text-sm text-white transition-colors duration-150 rounded-lg bg-primary-600 hover:bg-primary-700"
data-action="click->nested-form#remove">
<%= t 'questionnaires.remove_question' %>
</button>
</div>
<%= form.hidden_field :_destroy %>
</div>
4 changes: 4 additions & 0 deletions app/views/new_admin/questionnaires/edit.html.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
<% content_for :assets do %>
<%= javascript_pack_tag 'questionnaire' %>
<% end %>
<% content_for :page_title do %>
<%= t 'questionnaires.edit' %>
<% end %>
Expand Down

0 comments on commit f658ab5

Please sign in to comment.