Files
nairobi/app/controllers/polls_controller.rb
Javi Martín 76c7827cf4 Use stats objects instead of hashes
It will make it far easier to call other methods on the stats object,
and we're already caching the methods.

We had to remove the view fragment caching because the stats object
isn't as easy to cache. The good thing about it is the view will
automatically be updated when we change logic regarding which stats to
show, and the methods taking long to execute are cached in the model.
2019-05-21 13:50:18 +02:00

53 lines
1.4 KiB
Ruby

class PollsController < ApplicationController
include PollsHelper
before_action :load_poll, except: [:index]
before_action :load_active_poll, only: :index
load_and_authorize_resource
has_filters %w[current expired]
has_orders %w[most_voted newest oldest], only: :show
::Poll::Answer # trigger autoload
def index
@polls = @polls.not_budget.public_polls.send(@current_filter).includes(:geozones)
.sort_for_list.page(params[:page])
end
def show
@questions = @poll.questions.for_render.sort_for_list
@token = poll_voter_token(@poll, current_user)
@poll_questions_answers = Poll::Question::Answer.where(question: @poll.questions)
.where.not(description: "").order(:given_order)
@answers_by_question_id = {}
poll_answers = ::Poll::Answer.by_question(@poll.question_ids).by_author(current_user.try(:id))
poll_answers.each do |answer|
@answers_by_question_id[answer.question_id] = answer.answer
end
@commentable = @poll
@comment_tree = CommentTree.new(@commentable, params[:page], @current_order)
end
def stats
@stats = Poll::Stats.new(@poll)
end
def results
end
private
def load_poll
@poll = Poll.where(slug: params[:id]).first || Poll.where(id: params[:id]).first
end
def load_active_poll
@active_poll = ActivePoll.first
end
end