From 68139d811ab7c8fbacf195ca0dab3ac145adcc33 Mon Sep 17 00:00:00 2001 From: Amr Hossam Date: Sat, 28 Sep 2024 10:40:03 +0300 Subject: [PATCH 1/2] feat: delete questions functionality --- app/controllers/admins/questions_controller.rb | 2 +- app/views/admins/questions/_form.html.erb | 11 ++++++++++- spec/requests/admins/questions_spec.rb | 13 +++++++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/app/controllers/admins/questions_controller.rb b/app/controllers/admins/questions_controller.rb index 4408354..e47fede 100644 --- a/app/controllers/admins/questions_controller.rb +++ b/app/controllers/admins/questions_controller.rb @@ -50,7 +50,7 @@ def update def destroy @question = @survey.questions.find(params[:id]) @question.destroy - redirect_to admin_study_survey_path(@study, @survey), notice: 'Question was deleted.' + redirect_to admin_study_survey_path(@survey.study, @survey), notice: 'Question was deleted.' end private diff --git a/app/views/admins/questions/_form.html.erb b/app/views/admins/questions/_form.html.erb index 3b9e1f8..53a816f 100644 --- a/app/views/admins/questions/_form.html.erb +++ b/app/views/admins/questions/_form.html.erb @@ -11,6 +11,15 @@ <%= render Forms::CheckboxComponent.new(form: f, method: :required, help_text: "Mark this if this question requires a response.") %> - <%= render Forms::ButtonBarComponent.new(f) %> +
+ <% if @question.persisted? %> + <%= link_to "Delete Question", admin_study_survey_question_path(@survey.study, @survey, @question), class: "oba-btn--danger", + data: { turbo_method: :delete, turbo_confirm: "Are you sure?" } %> + <% end %> + +
+ + <%= render Forms::ButtonBarComponent.new(f) %> +
<% end %> \ No newline at end of file diff --git a/spec/requests/admins/questions_spec.rb b/spec/requests/admins/questions_spec.rb index ddd164b..7278ae8 100644 --- a/spec/requests/admins/questions_spec.rb +++ b/spec/requests/admins/questions_spec.rb @@ -25,4 +25,17 @@ end end end + + context "DELETE /admin/studies/:study_id/surveys/:survey_id/questions/:id" do + let!(:question) { create(:question, survey:) } + it "deletes the question" do + sign_in admin + expect do + delete admin_study_survey_question_path(study, survey, question) + end.to change(Question, :count).by(-1) + expect(response).to redirect_to(admin_study_survey_path(study, survey)) + expect(flash[:notice]).to eq("Question was deleted.") + end + end + end From 6ddf3e1e53d2e20f21ed34a65057227fe2e5b3e0 Mon Sep 17 00:00:00 2001 From: Amr Hossam Date: Sat, 28 Sep 2024 10:46:56 +0300 Subject: [PATCH 2/2] chore: run linting --- spec/requests/admins/questions_spec.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/spec/requests/admins/questions_spec.rb b/spec/requests/admins/questions_spec.rb index 7278ae8..1a31065 100644 --- a/spec/requests/admins/questions_spec.rb +++ b/spec/requests/admins/questions_spec.rb @@ -37,5 +37,4 @@ expect(flash[:notice]).to eq("Question was deleted.") end end - end