Files
nairobi/app/controllers/dashboard/polls_controller.rb
Javi Martín 11832cc07d Make it easier to customize allowed parameters
When customizing CONSUL, one of the most common actions is adding a new
field to a form.

This requires modifying the permitted/allowed parameters. However, in
most cases, the method returning these parameters returned an instance
of `ActionController::Parameters`, so adding more parameters to it
wasn't easy.

So customizing the code required copying the method returning those
parameters and adding the new ones. For example:

```
def something_params
  params.require(:something).permit(
    :one_consul_attribute,
    :another_consul_attribute,
    :my_custom_attribute
  )
end
```

This meant that, if the `something_params` method changed in CONSUL, the
customization of this method had to be updated as well.

So we're extracting the logic returning the parameters to a method which
returns an array. Now this code can be customized without copying the
original method:

```
alias_method :consul_allowed_params, :allowed_params

def allowed_params
  consul_allowed_params + [:my_custom_attribute]
end
```
2022-04-07 19:35:40 +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_answers_attributes: question_answers_attributes]
end
def question_answers_attributes
[:id, :_destroy, :title, :description, :given_order, :question_id,
documents_attributes: document_attributes]
end
def authorize_manage_polls
authorize! :manage_polls, proposal
end
end