diff --git a/app/views/admin/poll/questions/answers/documents.html.erb b/app/components/admin/poll/questions/answers/documents/index_component.html.erb similarity index 77% rename from app/views/admin/poll/questions/answers/documents.html.erb rename to app/components/admin/poll/questions/answers/documents/index_component.html.erb index 4c0396849..9c6b95c20 100644 --- a/app/views/admin/poll/questions/answers/documents.html.erb +++ b/app/components/admin/poll/questions/answers/documents/index_component.html.erb @@ -1,18 +1,15 @@ -<%= back_link_to %> +<%= back_link_to admin_question_path(@answer.question) %>
| <%= t("admin.questions.show.answers.document_title") %> | <%= t("admin.questions.show.answers.document_actions") %> |
|---|---|
<%= link_to document.title, document.attachment %>
diff --git a/app/components/admin/poll/questions/answers/documents/index_component.rb b/app/components/admin/poll/questions/answers/documents/index_component.rb
new file mode 100644
index 000000000..ca72d4f98
--- /dev/null
+++ b/app/components/admin/poll/questions/answers/documents/index_component.rb
@@ -0,0 +1,7 @@
+class Admin::Poll::Questions::Answers::Documents::IndexComponent < ApplicationComponent
+ attr_reader :answer
+
+ def initialize(answer)
+ @answer = answer
+ end
+end
diff --git a/app/controllers/admin/poll/polls_controller.rb b/app/controllers/admin/poll/polls_controller.rb
index 8cfa00136..68ba02a01 100644
--- a/app/controllers/admin/poll/polls_controller.rb
+++ b/app/controllers/admin/poll/polls_controller.rb
@@ -4,22 +4,21 @@ class Admin::Poll::PollsController < Admin::Poll::BaseController
include ReportAttributes
load_and_authorize_resource
- before_action :load_search, only: [:search_booths, :search_officers]
before_action :load_geozones, only: [:new, :create, :edit, :update]
def index
- @polls = Poll.not_budget.created_by_admin.order(starts_at: :desc)
+ @polls = @polls.not_budget.created_by_admin.order(starts_at: :desc)
end
def show
- @poll = Poll.find(params[:id])
end
def new
end
def create
- @poll = Poll.new(poll_params.merge(author: current_user))
+ @poll.author = current_user
+
if @poll.save
notice = t("flash.actions.create.poll")
if @poll.budget.present?
@@ -43,18 +42,6 @@ class Admin::Poll::PollsController < Admin::Poll::BaseController
end
end
- def add_question
- question = ::Poll::Question.find(params[:question_id])
-
- if question.present?
- @poll.questions << question
- notice = t("admin.polls.flash.question_added")
- else
- notice = t("admin.polls.flash.error_on_question_added")
- end
- redirect_to admin_poll_path(@poll), notice: notice
- end
-
def booth_assignments
@polls = Poll.current.created_by_admin
end
@@ -85,16 +72,4 @@ class Admin::Poll::PollsController < Admin::Poll::BaseController
[*attributes, *report_attributes, translation_params(Poll)]
end
-
- def search_params
- params.permit(:poll_id, :search)
- end
-
- def load_search
- @search = search_params[:search]
- end
-
- def resource
- @poll ||= Poll.find(params[:id])
- end
end
diff --git a/app/controllers/admin/poll/questions/answers/documents_controller.rb b/app/controllers/admin/poll/questions/answers/documents_controller.rb
new file mode 100644
index 000000000..3210896d6
--- /dev/null
+++ b/app/controllers/admin/poll/questions/answers/documents_controller.rb
@@ -0,0 +1,29 @@
+class Admin::Poll::Questions::Answers::DocumentsController < Admin::Poll::BaseController
+ include DocumentAttributes
+
+ load_and_authorize_resource :answer, class: "::Poll::Question::Answer"
+
+ def index
+ end
+
+ def create
+ @answer.attributes = documents_params
+
+ if @answer.save
+ redirect_to admin_answer_documents_path(@answer),
+ notice: t("admin.documents.create.success_notice")
+ else
+ render :new
+ end
+ end
+
+ private
+
+ def documents_params
+ params.require(:poll_question_answer).permit(allowed_params)
+ end
+
+ def allowed_params
+ [documents_attributes: document_attributes]
+ end
+end
diff --git a/app/controllers/admin/poll/questions/answers/images_controller.rb b/app/controllers/admin/poll/questions/answers/images_controller.rb
index f91389daa..a209b058f 100644
--- a/app/controllers/admin/poll/questions/answers/images_controller.rb
+++ b/app/controllers/admin/poll/questions/answers/images_controller.rb
@@ -1,7 +1,7 @@
class Admin::Poll::Questions::Answers::ImagesController < Admin::Poll::BaseController
include ImageAttributes
- before_action :load_answer, except: :destroy
+ load_and_authorize_resource :answer, class: "::Poll::Question::Answer"
def index
end
@@ -38,8 +38,4 @@ class Admin::Poll::Questions::Answers::ImagesController < Admin::Poll::BaseContr
def allowed_params
[:answer_id, images_attributes: image_attributes]
end
-
- def load_answer
- @answer = ::Poll::Question::Answer.find(params[:answer_id])
- end
end
diff --git a/app/controllers/admin/poll/questions/answers/videos_controller.rb b/app/controllers/admin/poll/questions/answers/videos_controller.rb
index 2df3ea80c..c198540b1 100644
--- a/app/controllers/admin/poll/questions/answers/videos_controller.rb
+++ b/app/controllers/admin/poll/questions/answers/videos_controller.rb
@@ -1,17 +1,14 @@
class Admin::Poll::Questions::Answers::VideosController < Admin::Poll::BaseController
- before_action :load_answer, only: [:index, :new, :create]
- before_action :load_video, only: [:edit, :update, :destroy]
+ load_and_authorize_resource :answer, class: "::Poll::Question::Answer"
+ load_and_authorize_resource class: "::Poll::Question::Answer::Video", through: :answer
def index
end
def new
- @video = ::Poll::Question::Answer::Video.new
end
def create
- @video = ::Poll::Question::Answer::Video.new(video_params)
-
if @video.save
redirect_to admin_answer_videos_path(@answer),
notice: t("flash.actions.create.poll_question_answer_video")
@@ -25,20 +22,16 @@ class Admin::Poll::Questions::Answers::VideosController < Admin::Poll::BaseContr
def update
if @video.update(video_params)
- redirect_to admin_answer_videos_path(@video.answer_id),
- notice: t("flash.actions.save_changes.notice")
+ redirect_to admin_answer_videos_path(@answer), notice: t("flash.actions.save_changes.notice")
else
render :edit
end
end
def destroy
- notice = if @video.destroy
- t("flash.actions.destroy.poll_question_answer_video")
- else
- t("flash.actions.destroy.error")
- end
- redirect_back(fallback_location: (request.referer || root_path), notice: notice)
+ @video.destroy!
+ notice = t("flash.actions.destroy.poll_question_answer_video")
+ redirect_to admin_answer_videos_path(@answer), notice: notice
end
private
@@ -48,14 +41,6 @@ class Admin::Poll::Questions::Answers::VideosController < Admin::Poll::BaseContr
end
def allowed_params
- [:title, :url, :answer_id]
- end
-
- def load_answer
- @answer = ::Poll::Question::Answer.find(params[:answer_id])
- end
-
- def load_video
- @video = ::Poll::Question::Answer::Video.find(params[:id])
+ [:title, :url]
end
end
diff --git a/app/controllers/admin/poll/questions/answers_controller.rb b/app/controllers/admin/poll/questions/answers_controller.rb
index 5e224a514..c015f1ebe 100644
--- a/app/controllers/admin/poll/questions/answers_controller.rb
+++ b/app/controllers/admin/poll/questions/answers_controller.rb
@@ -1,19 +1,15 @@
class Admin::Poll::Questions::AnswersController < Admin::Poll::BaseController
include Translatable
- include DocumentAttributes
-
- before_action :load_answer, only: [:show, :edit, :update, :documents]
load_and_authorize_resource :question, class: "::Poll::Question"
+ load_and_authorize_resource class: "::Poll::Question::Answer",
+ through: :question,
+ through_association: :question_answers
def new
- @answer = ::Poll::Question::Answer.new
end
def create
- @answer = ::Poll::Question::Answer.new(answer_params)
- @question = @answer.question
-
if @answer.save
redirect_to admin_question_path(@question),
notice: t("flash.actions.create.poll_question_answer")
@@ -22,27 +18,18 @@ class Admin::Poll::Questions::AnswersController < Admin::Poll::BaseController
end
end
- def show
- end
-
def edit
end
def update
if @answer.update(answer_params)
- redirect_to admin_question_path(@answer.question),
+ redirect_to admin_question_path(@question),
notice: t("flash.actions.save_changes.notice")
else
render :edit
end
end
- def documents
- @documents = @answer.documents
-
- render "admin/poll/questions/answers/documents"
- end
-
def order_answers
::Poll::Question::Answer.order_answers(params[:ordered_list])
head :ok
@@ -55,18 +42,8 @@ class Admin::Poll::Questions::AnswersController < Admin::Poll::BaseController
end
def allowed_params
- attributes = [:title, :description, :given_order, :question_id,
- documents_attributes: document_attributes]
+ attributes = [:title, :description, :given_order]
[*attributes, translation_params(Poll::Question::Answer)]
end
-
- def load_answer
- @answer = ::Poll::Question::Answer.find(params[:id] || params[:answer_id])
- end
-
- def resource
- load_answer unless @answer
- @answer
- end
end
diff --git a/app/controllers/admin/poll/questions_controller.rb b/app/controllers/admin/poll/questions_controller.rb
index bb07a5ac2..598e664e7 100644
--- a/app/controllers/admin/poll/questions_controller.rb
+++ b/app/controllers/admin/poll/questions_controller.rb
@@ -43,12 +43,8 @@ class Admin::Poll::QuestionsController < Admin::Poll::BaseController
end
def destroy
- if @question.destroy
- notice = "Question destroyed succesfully"
- else
- notice = t("flash.actions.destroy.error")
- end
- redirect_to admin_questions_path, notice: notice
+ @question.destroy!
+ redirect_to admin_poll_path(@question.poll), notice: t("admin.questions.destroy.notice")
end
private
@@ -66,8 +62,4 @@ class Admin::Poll::QuestionsController < Admin::Poll::BaseController
def search_params
params.permit(:poll_id, :search)
end
-
- def resource
- @poll_question ||= Poll::Question.find(params[:id])
- end
end
diff --git a/app/models/abilities/administrator.rb b/app/models/abilities/administrator.rb
index 0f492cd0b..e9a455c3c 100644
--- a/app/models/abilities/administrator.rb
+++ b/app/models/abilities/administrator.rb
@@ -83,13 +83,18 @@ module Abilities
can [:index, :create, :update, :destroy], Geozone
- can [:read, :create, :update, :destroy, :add_question, :search_booths, :search_officers, :booth_assignments], Poll
+ can [:read, :create, :update, :destroy, :booth_assignments], Poll
can [:read, :create, :update, :destroy, :available], Poll::Booth
can [:search, :create, :index, :destroy], ::Poll::Officer
can [:create, :destroy, :manage], ::Poll::BoothAssignment
can [:create, :destroy], ::Poll::OfficerAssignment
can [:read, :create, :update], Poll::Question
can :destroy, Poll::Question
+ can :manage, Poll::Question::Answer
+ can :manage, Poll::Question::Answer::Video
+ can [:create, :destroy], Image do |image|
+ image.imageable_type == "Poll::Question::Answer"
+ end
can :manage, SiteCustomization::Page
can :manage, SiteCustomization::Image
diff --git a/app/views/admin/poll/polls/edit.html.erb b/app/views/admin/poll/polls/edit.html.erb
index f7bd93212..2fd150b35 100644
--- a/app/views/admin/poll/polls/edit.html.erb
+++ b/app/views/admin/poll/polls/edit.html.erb
@@ -1,4 +1,4 @@
-<%= back_link_to %>
+<%= back_link_to admin_polls_path %>
<%= t("admin.polls.edit.title") %>diff --git a/app/views/admin/poll/polls/new.html.erb b/app/views/admin/poll/polls/new.html.erb index 528b7ff69..899de8223 100644 --- a/app/views/admin/poll/polls/new.html.erb +++ b/app/views/admin/poll/polls/new.html.erb @@ -1,6 +1,6 @@
- <%= back_link_to %>
+ <%= back_link_to admin_polls_path %>
diff --git a/app/views/admin/poll/questions/answers/_form.html.erb b/app/views/admin/poll/questions/answers/_form.html.erb
index 53eceae17..ef32b93e5 100644
--- a/app/views/admin/poll/questions/answers/_form.html.erb
+++ b/app/views/admin/poll/questions/answers/_form.html.erb
@@ -7,8 +7,6 @@
<%= f.hidden_field :given_order,
value: @answer.persisted? ? @answer.given_order : @answer.class.last_position(@answer.question_id || @question.id) + 1 %>
- <%= f.hidden_field :question_id, value: @answer.question_id || @question.id %>
-
<%= t("admin.polls.new.title") %>
<%= f.translatable_fields do |translations_form| %>
diff --git a/app/views/admin/poll/questions/answers/documents/index.html.erb b/app/views/admin/poll/questions/answers/documents/index.html.erb
new file mode 100644
index 000000000..037ab68f2
--- /dev/null
+++ b/app/views/admin/poll/questions/answers/documents/index.html.erb
@@ -0,0 +1 @@
+<%= render Admin::Poll::Questions::Answers::Documents::IndexComponent.new(@answer) %>
diff --git a/app/views/admin/poll/questions/answers/edit.html.erb b/app/views/admin/poll/questions/answers/edit.html.erb
index 46a8784cc..3abeef3b6 100644
--- a/app/views/admin/poll/questions/answers/edit.html.erb
+++ b/app/views/admin/poll/questions/answers/edit.html.erb
@@ -1,4 +1,4 @@
-<%= back_link_to %>
+<%= back_link_to admin_question_path(@question) %>
|