Files
nairobi/app/controllers/dashboard/polls_controller.rb
Javi Martín 354b183e17 Create reports
This table will store which reports (stats, results, ...) will be shown
for a certain process (polls, budgets, ...).

Note Rails fails to save a poll and its report when both are new records
if we add a `validate :process, presence: true` rule. Since it caused a
lot of trouble when creating records for tests during factories rule
completely. Instead, I've created the `results_enabled=` and
`stats_enabled=` methods, so tests are easier to set up, while also
automatically creating a report if it doesn't already exist. This also
decouples form structure and database implemenation.

Originally I named this table `enabled_reports` and instead of having
`stats` and `results` columns, it had an `enabled` column and a `kind`
column, which would be set to "stats" or "results". However, although
that table would allow us to add arbitrary reports easily, I found the
way we had to handle the `has_many` relationship was a bit too complex.
2019-05-22 11:50:03 +02:00

74 lines
1.8 KiB
Ruby

class Dashboard::PollsController < Dashboard::BaseController
helper_method :poll
def index
authorize! :manage_polls, proposal
@polls = Poll.for(proposal)
end
def new
authorize! :manage_polls, proposal
@poll = Poll.new
end
def create
authorize! :manage_polls, proposal
@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
authorize! :manage_polls, proposal
end
def update
authorize! :manage_polls, proposal
respond_to do |format|
if poll.update(poll_params)
format.html { redirect_to proposal_dashboard_polls_path(proposal),
notice: t("flash.actions.update.poll") }
format.json { head :no_content }
else
format.html { render :edit }
format.json { render json: poll.errors.full_messages, status: :unprocessable_entity }
end
end
end
private
def poll
@poll ||= Poll.includes(:questions).find(params[:id])
end
def poll_params
params.require(:poll).permit(poll_attributes)
end
def poll_attributes
[: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: documents_attributes]
end
def documents_attributes
[:id, :title, :attachment, :cached_attachment, :user_id, :_destroy]
end
end