Files
nairobi/app/controllers/dashboard/polls_controller.rb
Javi Martín 38b38d1fcc 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.
2024-06-13 19:13:01 +02:00

81 lines
2.0 KiB
Ruby

class Dashboard::PollsController < Dashboard::BaseController
include DocumentAttributes
helper_method :poll
before_action :authorize_manage_polls
def index
@polls = Poll.for(proposal)
end
def new
@poll = Poll.new
end
def create
@poll = Poll.new(poll_params.merge(author: current_user, related: proposal))
if @poll.save
redirect_to proposal_dashboard_polls_path(proposal), notice: t("flash.actions.create.poll")
else
render :new
end
end
def edit
end
def update
respond_to do |format|
if poll.update(poll_params)
format.html do
redirect_to proposal_dashboard_polls_path(proposal),
notice: t("flash.actions.update.poll")
end
format.json { head :no_content }
else
format.html { render :edit }
format.json { render json: poll.errors.full_messages, status: :unprocessable_entity }
end
end
end
def destroy
if ::Poll::Voter.where(poll: poll).any?
redirect_to proposal_dashboard_polls_path(proposal), alert: t("dashboard.polls.poll.unable_notice")
else
poll.destroy!
redirect_to proposal_dashboard_polls_path(proposal), notice: t("dashboard.polls.poll.success_notice")
end
end
private
def poll
@poll ||= Poll.includes(:questions).find(params[:id])
end
def poll_params
params.require(:poll).permit(allowed_params)
end
def allowed_params
[:name, :starts_at, :ends_at, :description, :results_enabled,
questions_attributes: question_attributes]
end
def question_attributes
[:id, :title, :author_id, :proposal_id, :_destroy,
question_options_attributes: question_options_attributes]
end
def question_options_attributes
[:id, :_destroy, :title, :description, :given_order, :question_id,
documents_attributes: document_attributes]
end
def authorize_manage_polls
authorize! :manage_polls, proposal
end
end