Rename Poll::Question::Answer to Poll::Question::Option
Having a class named `Poll::Question::Answer` and another class named `Poll::Answer` was so confusing that no developer working on the project has ever been capable of remembering which is which for more than a few seconds. Furthermore, we're planning to add open answers to polls, and we might add a reference from the `poll_answers` table to the `poll_question_answers` table to property differentiate between open answers and closed answers. Having yet another thing named answer would be more than what our brains can handle (we know it because we did this once in a prototype). So we're renaming `Poll::Question::Answer` to `Poll::Question::Option`. Hopefully that'll make it easier to remember. The name is also (more or less) consistent with the `Legislation::QuestionOption` class, which is similar. We aren't changing the table or columns names for now in order to avoid possible issues when upgrading (old code running with the new database tables/columns after running the migrations but before deployment has finished, for instance). We might do it in the future. I've tried not to change the internationalization keys either so existing translations would still be valid. However, since we have to change the keys in `activerecord.yml` so methods like `human_attribute_name` keep working, I'm also changing them in places where similar keys were used (like `poll_question_answer` or `poll/question/answer`). Note that it isn't clear whether we should use `option` or `question_option` in some cases. In order to keep things simple, we're using `option` where we were using `answer` and `question_option` where we were using `question_answer`. Also note we're adding tests for the admin menu component, since at first I forgot to change the `answers` reference there and all tests passed.
This commit is contained in:
@@ -1,30 +0,0 @@
|
||||
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
|
||||
authorize! :update, @answer
|
||||
|
||||
if @answer.save
|
||||
redirect_to admin_answer_documents_path(@answer),
|
||||
notice: t("admin.documents.create.success_notice")
|
||||
else
|
||||
render :index
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def documents_params
|
||||
params.require(:poll_question_answer).permit(allowed_params)
|
||||
end
|
||||
|
||||
def allowed_params
|
||||
[documents_attributes: document_attributes]
|
||||
end
|
||||
end
|
||||
@@ -1,41 +0,0 @@
|
||||
class Admin::Poll::Questions::Answers::ImagesController < Admin::Poll::BaseController
|
||||
include ImageAttributes
|
||||
|
||||
load_and_authorize_resource :answer, class: "::Poll::Question::Answer"
|
||||
load_and_authorize_resource only: [:destroy]
|
||||
|
||||
def index
|
||||
end
|
||||
|
||||
def new
|
||||
end
|
||||
|
||||
def create
|
||||
@answer.attributes = images_params
|
||||
authorize! :update, @answer
|
||||
|
||||
if @answer.save
|
||||
redirect_to admin_answer_images_path(@answer),
|
||||
notice: t("flash.actions.create.poll_question_answer_image")
|
||||
else
|
||||
render :new
|
||||
end
|
||||
end
|
||||
|
||||
def destroy
|
||||
@image.destroy!
|
||||
|
||||
redirect_to admin_answer_images_path(@image.imageable),
|
||||
notice: t("flash.actions.destroy.poll_question_answer_image")
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def images_params
|
||||
params.require(:poll_question_answer).permit(allowed_params)
|
||||
end
|
||||
|
||||
def allowed_params
|
||||
[:answer_id, images_attributes: image_attributes]
|
||||
end
|
||||
end
|
||||
@@ -1,46 +0,0 @@
|
||||
class Admin::Poll::Questions::Answers::VideosController < Admin::Poll::BaseController
|
||||
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
|
||||
end
|
||||
|
||||
def create
|
||||
if @video.save
|
||||
redirect_to admin_answer_videos_path(@answer),
|
||||
notice: t("flash.actions.create.poll_question_answer_video")
|
||||
else
|
||||
render :new
|
||||
end
|
||||
end
|
||||
|
||||
def edit
|
||||
end
|
||||
|
||||
def update
|
||||
if @video.update(video_params)
|
||||
redirect_to admin_answer_videos_path(@answer), notice: t("flash.actions.save_changes.notice")
|
||||
else
|
||||
render :edit
|
||||
end
|
||||
end
|
||||
|
||||
def destroy
|
||||
@video.destroy!
|
||||
notice = t("flash.actions.destroy.poll_question_answer_video")
|
||||
redirect_to admin_answer_videos_path(@answer), notice: notice
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def video_params
|
||||
params.require(:poll_question_answer_video).permit(allowed_params)
|
||||
end
|
||||
|
||||
def allowed_params
|
||||
[:title, :url]
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,30 @@
|
||||
class Admin::Poll::Questions::Options::DocumentsController < Admin::Poll::BaseController
|
||||
include DocumentAttributes
|
||||
|
||||
load_and_authorize_resource :option, class: "::Poll::Question::Option"
|
||||
|
||||
def index
|
||||
end
|
||||
|
||||
def create
|
||||
@option.attributes = documents_params
|
||||
authorize! :update, @option
|
||||
|
||||
if @option.save
|
||||
redirect_to admin_option_documents_path(@option),
|
||||
notice: t("admin.documents.create.success_notice")
|
||||
else
|
||||
render :index
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def documents_params
|
||||
params.require(:poll_question_option).permit(allowed_params)
|
||||
end
|
||||
|
||||
def allowed_params
|
||||
[documents_attributes: document_attributes]
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,41 @@
|
||||
class Admin::Poll::Questions::Options::ImagesController < Admin::Poll::BaseController
|
||||
include ImageAttributes
|
||||
|
||||
load_and_authorize_resource :option, class: "::Poll::Question::Option"
|
||||
load_and_authorize_resource only: [:destroy]
|
||||
|
||||
def index
|
||||
end
|
||||
|
||||
def new
|
||||
end
|
||||
|
||||
def create
|
||||
@option.attributes = images_params
|
||||
authorize! :update, @option
|
||||
|
||||
if @option.save
|
||||
redirect_to admin_option_images_path(@option),
|
||||
notice: t("flash.actions.create.poll_question_option_image")
|
||||
else
|
||||
render :new
|
||||
end
|
||||
end
|
||||
|
||||
def destroy
|
||||
@image.destroy!
|
||||
|
||||
redirect_to admin_option_images_path(@image.imageable),
|
||||
notice: t("flash.actions.destroy.poll_question_option_image")
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def images_params
|
||||
params.require(:poll_question_option).permit(allowed_params)
|
||||
end
|
||||
|
||||
def allowed_params
|
||||
[:option_id, images_attributes: image_attributes]
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,46 @@
|
||||
class Admin::Poll::Questions::Options::VideosController < Admin::Poll::BaseController
|
||||
load_and_authorize_resource :option, class: "::Poll::Question::Option"
|
||||
load_and_authorize_resource class: "::Poll::Question::Option::Video", through: :option
|
||||
|
||||
def index
|
||||
end
|
||||
|
||||
def new
|
||||
end
|
||||
|
||||
def create
|
||||
if @video.save
|
||||
redirect_to admin_option_videos_path(@option),
|
||||
notice: t("flash.actions.create.poll_question_option_video")
|
||||
else
|
||||
render :new
|
||||
end
|
||||
end
|
||||
|
||||
def edit
|
||||
end
|
||||
|
||||
def update
|
||||
if @video.update(video_params)
|
||||
redirect_to admin_option_videos_path(@option), notice: t("flash.actions.save_changes.notice")
|
||||
else
|
||||
render :edit
|
||||
end
|
||||
end
|
||||
|
||||
def destroy
|
||||
@video.destroy!
|
||||
notice = t("flash.actions.destroy.poll_question_option_video")
|
||||
redirect_to admin_option_videos_path(@option), notice: notice
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def video_params
|
||||
params.require(:poll_question_option_video).permit(allowed_params)
|
||||
end
|
||||
|
||||
def allowed_params
|
||||
[:title, :url]
|
||||
end
|
||||
end
|
||||
@@ -1,18 +1,18 @@
|
||||
class Admin::Poll::Questions::AnswersController < Admin::Poll::BaseController
|
||||
class Admin::Poll::Questions::OptionsController < Admin::Poll::BaseController
|
||||
include Translatable
|
||||
|
||||
load_and_authorize_resource :question, class: "::Poll::Question"
|
||||
load_and_authorize_resource class: "::Poll::Question::Answer",
|
||||
load_and_authorize_resource class: "::Poll::Question::Option",
|
||||
through: :question,
|
||||
through_association: :question_answers
|
||||
through_association: :question_options
|
||||
|
||||
def new
|
||||
end
|
||||
|
||||
def create
|
||||
if @answer.save
|
||||
if @option.save
|
||||
redirect_to admin_question_path(@question),
|
||||
notice: t("flash.actions.create.poll_question_answer")
|
||||
notice: t("flash.actions.create.poll_question_option")
|
||||
else
|
||||
render :new
|
||||
end
|
||||
@@ -22,7 +22,7 @@ class Admin::Poll::Questions::AnswersController < Admin::Poll::BaseController
|
||||
end
|
||||
|
||||
def update
|
||||
if @answer.update(answer_params)
|
||||
if @option.update(option_params)
|
||||
redirect_to admin_question_path(@question),
|
||||
notice: t("flash.actions.save_changes.notice")
|
||||
else
|
||||
@@ -31,24 +31,24 @@ class Admin::Poll::Questions::AnswersController < Admin::Poll::BaseController
|
||||
end
|
||||
|
||||
def destroy
|
||||
@answer.destroy!
|
||||
@option.destroy!
|
||||
redirect_to admin_question_path(@question), notice: t("admin.answers.destroy.success_notice")
|
||||
end
|
||||
|
||||
def order_answers
|
||||
::Poll::Question::Answer.order_answers(params[:ordered_list])
|
||||
def order_options
|
||||
::Poll::Question::Option.order_options(params[:ordered_list])
|
||||
head :ok
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def answer_params
|
||||
params.require(:poll_question_answer).permit(allowed_params)
|
||||
def option_params
|
||||
params.require(:poll_question_option).permit(allowed_params)
|
||||
end
|
||||
|
||||
def allowed_params
|
||||
attributes = [:title, :description, :given_order]
|
||||
|
||||
[*attributes, translation_params(Poll::Question::Answer)]
|
||||
[*attributes, translation_params(Poll::Question::Option)]
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user