Merge pull request #1555 from consul/budgets-results

Budget Results
This commit is contained in:
Juanjo Bazán
2017-05-22 17:11:22 +02:00
committed by GitHub
17 changed files with 364 additions and 3 deletions

View File

@@ -312,6 +312,11 @@ body.admin {
}
}
[class^="icon-"].delete {
border: 0;
font-size: $base-font-size;
}
.verified {
color: $check;

View File

@@ -234,6 +234,14 @@ a {
color: $brand;
}
}
&.no-margin-top {
margin-top: 0;
}
&.no-padding-top {
padding-top: 0;
}
}
.small {
@@ -1194,6 +1202,16 @@ table {
}
}
.table-for-mobile {
@include breakpoint(medium down) {
th, td {
display: block;
text-align: left;
}
}
}
// 12. Social
// ----------

View File

@@ -0,0 +1,21 @@
module Budgets
class ResultsController < ApplicationController
load_and_authorize_resource :budget
def show
@result = load_result
end
private
def load_result
Budget::Result.new(@budget, heading)
end
def heading
@budget.headings.find(params[:heading_id])
end
end
end

View File

@@ -55,4 +55,8 @@ module ApplicationHelper
def content_block(name, locale)
SiteCustomization::ContentBlock.block_for(name, locale)
end
def format_price(number)
number_to_currency(number, precision: 0, locale: I18n.default_locale)
end
end

View File

@@ -33,6 +33,7 @@ class Budget
validates :terms_of_service, acceptance: { allow_nil: false }, on: :create
scope :sort_by_confidence_score, -> { reorder(confidence_score: :desc, id: :desc) }
scope :sort_by_ballots, -> { reorder(ballot_lines_count: :desc, id: :desc) }
scope :sort_by_price, -> { reorder(price: :desc, confidence_score: :desc, id: :desc) }
scope :sort_by_random, -> { reorder("RANDOM()") }

View File

@@ -0,0 +1,55 @@
class Budget
class Result
attr_accessor :budget, :heading, :money_spent, :current_investment
def initialize(budget, heading)
@budget = budget
@heading = heading
end
def calculate_winners
reset_winners
investments.each do |investment|
@current_investment = investment
if inside_budget?
set_winner
end
end
end
def investments
heading.investments.selected.sort_by_ballots
end
def inside_budget?
available_budget >= @current_investment.price
end
def available_budget
total_budget - money_spent
end
def total_budget
heading.price
end
def money_spent
@money_spent ||= 0
end
def reset_winners
investments.update_all(winner: false)
end
def set_winner
@money_spent += @current_investment.price
@current_investment.update(winner: true)
end
def winners
investments.where(winner: true)
end
end
end

View File

@@ -0,0 +1,72 @@
<div class="small-12 medium-9 column" id="results-container">
<h3 class="inline-block">
<%= heading.name %>
</h3>
<%= link_to t("budgets.results.show_all_link"), "#",
class: "js-toggle-link button hollow margin-bottom float-right",
data: {'toggle-selector' => '.js-discarded',
'toggle-text' => t("budgets.results.hide_discarded_link")} %>
<table id="budget-investments-results" class="table-for-mobile">
<thead>
<tr>
<th scope="col">
<%= t("budgets.results.spending_proposal") %>
</th>
<th scope="col" class="text-center">
<%= t("budgets.results.ballot_lines_count") %>
</th>
<th scope="col" class="text-center">
<%= t("budgets.results.price") %>
</th>
<th scope="col" class="text-right">
<%= format_price(heading.price) %><br>
<small><%= t("budgets.results.amount_available") %></small>
</th>
</tr>
</thead>
<tbody>
<% amount_available = heading.price %>
<% @result.investments.each do |investment| %>
<% if investment.winner? %>
<tr id="<%= dom_id(investment) %>"
class="spending_proposal success">
<% else %>
<tr id="<%= dom_id(investment) %>"
class="spending_proposal js-discarded" style="display:none">
<% end %>
<td>
<% if investment.winner? %>
<span class="icon-check">
<span class="sr-only">
<%= t("budgets.results.accepted") %>
</span>
</span>
<% else %>
<span class="icon-x delete">
<span class="sr-only">
<%= t("budgets.results.discarded") %>
</span>
</span>
<% end %>
<%= link_to investment.title,
budget_investment_path(@budget, investment) %>
</td>
<td class="text-center">
<%= investment.ballot_lines_count %>
</td>
<td class="text-center">
<%= format_price investment.price %>
</td>
<td class="small text-right"
title="<%= format_price(amount_available) %> - <%= format_price(investment.price) %>">
<%= format_price amount_available - investment.price %>
<% amount_available -= investment.price if investment.winner? %>
</td>
</tr>
<% end %>
</tbody>
</table>
</div>

View File

@@ -0,0 +1,38 @@
<% provide :title, t("budgets.results.page_title", budget: @budget.name) %>
<% content_for :canonical do %>
<%= render "shared/canonical", href: budget_results_url(@budget, heading_id: @result.heading) %>
<% end %>
<div class="expanded budget no-margin-top">
<div class="row">
<div class="small-12 column padding text-center">
<%= back_link_to budget_path(@budget) %>
<h2 class="title">
<%= @budget.name %><br>
<%= t("budgets.results.heading") %>
</h2>
</div>
</div>
</div>
<div class="row margin-top">
<div class="small-12 medium-3 column">
<ul class="menu vertical no-margin-top no-padding-top">
<li>
<strong>
<%= t("budgets.results.geozone_selection_title") %>
</strong>
</li>
<% @budget.headings.each do |heading| %>
<li>
<%= link_to heading.name,
budget_results_path(@budget, heading_id: heading.id) %>
</li>
<% end %>
</ul>
</div>
<%= render 'results_table', heading: @result.heading %>
</div>

View File

@@ -35,6 +35,12 @@
</div>
<% end %>
<% end %>
<% if @budget.finished? %>
<%= link_to t("budgets.show.see_results"),
budget_results_path(@budget, heading_id: @budget.headings.first),
class: "button margin-top expanded" %>
<% end %>
</div>
</div>
</div>