From b3f8ba819b3396609630a1ad35d3d6e1741bc3a9 Mon Sep 17 00:00:00 2001 From: taitus Date: Wed, 6 Aug 2025 11:54:30 +0200 Subject: [PATCH] Adapt 'show' view for open questions without options - Prevent creating options for open questions - Skip rendering the options table when none exist --- app/models/abilities/administrator.rb | 2 +- app/models/poll/question.rb | 4 + app/models/votation_type.rb | 4 + app/views/admin/poll/questions/show.html.erb | 108 ++++++++++--------- spec/models/abilities/administrator_spec.rb | 5 + spec/system/admin/poll/questions_spec.rb | 6 ++ 6 files changed, 75 insertions(+), 54 deletions(-) diff --git a/app/models/abilities/administrator.rb b/app/models/abilities/administrator.rb index 9e9c123ce..857280fe0 100644 --- a/app/models/abilities/administrator.rb +++ b/app/models/abilities/administrator.rb @@ -101,7 +101,7 @@ module Abilities end can [:read, :order_options], Poll::Question::Option can [:create, :update, :destroy], Poll::Question::Option do |option| - can?(:update, option.question) + can?(:update, option.question) && option.question.accepts_options? end can :read, Poll::Question::Option::Video can [:create, :update, :destroy], Poll::Question::Option::Video do |video| diff --git a/app/models/poll/question.rb b/app/models/poll/question.rb index d7a8d07e5..9e393394b 100644 --- a/app/models/poll/question.rb +++ b/app/models/poll/question.rb @@ -61,6 +61,10 @@ class Poll::Question < ApplicationRecord votation_type.nil? || votation_type.unique? end + def accepts_options? + votation_type.nil? || votation_type.accepts_options? + end + def max_votes if multiple? votation_type.max_votes diff --git a/app/models/votation_type.rb b/app/models/votation_type.rb index 24326204b..79c974681 100644 --- a/app/models/votation_type.rb +++ b/app/models/votation_type.rb @@ -9,6 +9,10 @@ class VotationType < ApplicationRecord validates :questionable_type, inclusion: { in: ->(*) { QUESTIONABLE_TYPES }} validates :max_votes, presence: true, if: :max_votes_required? + def accepts_options? + !open? + end + private def max_votes_required? diff --git a/app/views/admin/poll/questions/show.html.erb b/app/views/admin/poll/questions/show.html.erb index 4a6a98378..d9c83803d 100644 --- a/app/views/admin/poll/questions/show.html.erb +++ b/app/views/admin/poll/questions/show.html.erb @@ -46,57 +46,59 @@ -
- <% if can?(:create, Poll::Question::Option.new(question: @question)) %> - <%= link_to t("admin.questions.show.add_answer"), new_admin_question_option_path(@question), - class: "button float-right" %> - <% else %> -
- <%= t("admin.questions.no_edit") %> -
- <% end %> -
- - - - - - - - - - - - - - - - <% @question.question_options.each do |option| %> - - - - - - - - +<% if @question.accepts_options? %> +
+ <% if can?(:create, Poll::Question::Option.new(question: @question)) %> + <%= link_to t("admin.questions.show.add_answer"), new_admin_question_option_path(@question), + class: "button float-right" %> + <% else %> +
+ <%= t("admin.questions.no_edit") %> +
<% end %> -
-
<%= t("admin.questions.show.valid_answers") %>
<%= t("admin.questions.show.answers.title") %><%= t("admin.questions.show.answers.description") %><%= t("admin.questions.show.answers.images") %><%= t("admin.questions.show.answers.documents") %><%= t("admin.questions.show.answers.videos") %><%= t("admin.actions.actions") %>
<%= option.title %><%= wysiwyg(option.description) %> - (<%= option.images.count %>) -
- <%= link_to t("admin.questions.show.answers.images_list"), - admin_option_images_path(option) %> -
- (<%= option.documents.count rescue 0 %>) -
- <%= link_to t("admin.questions.show.answers.documents_list"), - admin_option_documents_path(option) %> -
- (<%= option.videos.count %>) -
- <%= link_to t("admin.questions.show.answers.video_list"), - admin_option_videos_path(option) %> -
- <%= render Admin::Poll::Questions::Options::TableActionsComponent.new(option) %> -
+ + + + + + + + + + + + + + + + + <% @question.question_options.each do |option| %> + + + + + + + + + <% end %> + +
<%= t("admin.questions.show.valid_answers") %>
<%= t("admin.questions.show.answers.title") %><%= t("admin.questions.show.answers.description") %><%= t("admin.questions.show.answers.images") %><%= t("admin.questions.show.answers.documents") %><%= t("admin.questions.show.answers.videos") %><%= t("admin.actions.actions") %>
<%= option.title %><%= wysiwyg(option.description) %> + (<%= option.images.count %>) +
+ <%= link_to t("admin.questions.show.answers.images_list"), + admin_option_images_path(option) %> +
+ (<%= option.documents.count rescue 0 %>) +
+ <%= link_to t("admin.questions.show.answers.documents_list"), + admin_option_documents_path(option) %> +
+ (<%= option.videos.count %>) +
+ <%= link_to t("admin.questions.show.answers.video_list"), + admin_option_videos_path(option) %> +
+ <%= render Admin::Poll::Questions::Options::TableActionsComponent.new(option) %> +
+<% end %> diff --git a/spec/models/abilities/administrator_spec.rb b/spec/models/abilities/administrator_spec.rb index 24b10a9cc..e6678ee21 100644 --- a/spec/models/abilities/administrator_spec.rb +++ b/spec/models/abilities/administrator_spec.rb @@ -20,6 +20,8 @@ describe Abilities::Administrator do let(:future_poll) { create(:poll, :future) } let(:current_poll_question) { create(:poll_question) } let(:future_poll_question) { create(:poll_question, poll: future_poll) } + let(:future_poll_question_open) { create(:poll_question_open, poll: future_poll) } + let(:future_poll_question_option_open) { future_poll_question_open.question_options.new } let(:current_poll_question_option) { create(:poll_question_option) } let(:future_poll_question_option) { create(:poll_question_option, poll: future_poll) } let(:current_poll_option_video) { create(:poll_option_video, option: current_poll_question_option) } @@ -143,6 +145,9 @@ describe Abilities::Administrator do it { should_not be_able_to(:create, current_poll_question_option) } it { should_not be_able_to(:update, current_poll_question_option) } it { should_not be_able_to(:destroy, current_poll_question_option) } + it { should_not be_able_to(:create, future_poll_question_option_open) } + it { should_not be_able_to(:update, future_poll_question_option_open) } + it { should_not be_able_to(:destroy, future_poll_question_option_open) } it { should be_able_to(:create, future_poll_option_video) } it { should be_able_to(:update, future_poll_option_video) } diff --git a/spec/system/admin/poll/questions_spec.rb b/spec/system/admin/poll/questions_spec.rb index 1bb7a55fb..ac913fe07 100644 --- a/spec/system/admin/poll/questions_spec.rb +++ b/spec/system/admin/poll/questions_spec.rb @@ -89,6 +89,8 @@ describe "Admin poll questions", :admin do expect(page).to have_content "Question with unique answer" expect(page).to have_content "Unique answer" expect(page).not_to have_content "Maximum number of votes" + expect(page).to have_link "Add answer" + expect(page).to have_table "Valid answers" end scenario "Multiple" do @@ -106,6 +108,8 @@ describe "Admin poll questions", :admin do expect(page).to have_content "Question with multiple answers" expect(page).to have_content "Multiple answers" expect(page).to have_text "Maximum number of votes 6", normalize_ws: true + expect(page).to have_link "Add answer" + expect(page).to have_table "Valid answers" end scenario "Open-ended" do @@ -122,6 +126,8 @@ describe "Admin poll questions", :admin do expect(page).to have_content "What do you want?" expect(page).to have_content "Open-ended" expect(page).not_to have_content "Maximum number of votes" + expect(page).not_to have_link "Add answer" + expect(page).not_to have_table "Valid answers" end end end