Controllers and models to see the polls stats. Routes related also added.

This commit is contained in:
iagirre
2017-10-16 17:25:04 +02:00
parent 7af18ff77a
commit 78cc09cc5a
6 changed files with 114 additions and 0 deletions

View File

@@ -0,0 +1,20 @@
class Polls::StatsController < ApplicationController
before_action :load_poll
load_and_authorize_resource :poll
def show
authorize! :read_stats, @poll
@stats = load_stats
end
private
def load_stats
Poll::Stats.new(@poll).generate
end
def load_poll
@poll = Poll.find_by(id: params[:id])
end
end

View File

@@ -23,6 +23,7 @@ module Abilities
can [:read], Legislation::Question can [:read], Legislation::Question
can [:create], Legislation::Answer can [:create], Legislation::Answer
can [:search, :comments, :read, :create, :new_comment], Legislation::Annotation can [:search, :comments, :read, :create, :new_comment], Legislation::Annotation
can :read_stats, Poll
end end
end end
end end

36
app/models/poll/stats.rb Normal file
View File

@@ -0,0 +1,36 @@
class Poll
class Stats
def initialize(poll)
@poll = poll
end
def generate
stats = %w[total_participants total_participants_web total_participants_booth]
stats.map { |stat_name| [stat_name.to_sym, send(stat_name)] }.to_h
end
private
def total_participants
stats_cache('total_participants') { voters.uniq.count }
end
def total_participants_web
stats_cache('total_participants_web') { voters.where(origin: 'web').count }
end
def total_participants_booth
stats_cache('total_participants_booth') { voters.where(origin: 'booth').count }
end
def voters
stats_cache('voters') { @poll.voters }
end
def stats_cache(key, &block)
Rails.cache.fetch("polls_stats/#{@poll.id}/#{key}/v7", &block)
end
end
end

View File

@@ -29,6 +29,8 @@
</div> </div>
</div> </div>
<%= link_to "Estadísticas", custom_poll_stats_path(@poll.id) %>
<div class="row margin"> <div class="row margin">
<div class="small-12 medium-9 column"> <div class="small-12 medium-9 column">
<%= render "callout" %> <%= render "callout" %>

View File

@@ -0,0 +1,53 @@
<% cache [@stats] do %>
<% provide :title do %>
<%= t("polls.stats.page_title", poll: @poll.name) %>
<% end %>
<% provide :social_media_meta_tags do %>
<%= render "shared/social_media_meta_tags",
social_url: poll_url(@poll),
social_title: @poll.name,
social_description: @poll.description %>
<% end %>
<div class="budgets-stats">
<div class="expanded no-margin-top padding header">
<div class="row">
<div class="small-12 column text-center">
<%= back_link_to polls_path %>
<h1 class="title"><%= t("polls.stats.title") %><br><%= @poll.name %></h1>
</div>
</div>
</div>
<!-- Aquí se empiezan a mostrar los datos -->
<div class="row">
<div class="small-12 medium-6 column">
<div class="callout">
<span class="uppercase"><%= t("polls.stats.total_participants") %></span>
<p id="total_participants" class="big-number-stat budget">
<%= @stats[:total_participants] %>
</p>
<table>
<thead>
<tr>
<td>Web</td>
<td>Booth</td>
</tr>
</thead>
<tbody>
<tr>
<td><%= @stats[:total_participants_web] %></td>
<td><%= @stats[:total_participants_booth] %></td>
</tr>
</tbody>
</table>
<span class="uppercase"><%= t("polls.stats.total_votes") %></span>
<p class="big-number-stat budget">
<%= @stats[:total_votes] %>
</p>
</div>
</div>
</div>
</div>
<% end %>

View File

@@ -112,6 +112,8 @@ Rails.application.routes.draw do
get :search, on: :collection get :search, on: :collection
end end
get "polls/:id/stats", to: "polls/stats#show", as: 'custom_poll_stats'
resources :polls, only: [:show, :index] do resources :polls, only: [:show, :index] do
resources :questions, controller: 'polls/questions', shallow: true do resources :questions, controller: 'polls/questions', shallow: true do
post :answer, on: :member post :answer, on: :member