Review my votes page

This commit is contained in:
rgarcia
2016-09-09 18:49:37 +02:00
parent 85d70a2cc2
commit 22373c3ddd
11 changed files with 130 additions and 135 deletions

View File

@@ -16,7 +16,7 @@ module Budgets
load_investment
load_heading
if @line.save
if @ballot.add_investment(@investment)
#@ballot.set_geozone(@geozone)
#@current_user.update(representative_id: nil)
if request.get?

View File

@@ -1,16 +1,17 @@
module Budgets
class BallotsController < ApplicationController
before_action :authenticate_user!
load_and_authorize_resource :budget
before_action :load_ballot
load_and_authorize_resource
def show
render template: "budgets/ballot/show"
end
private
def load_ballot
@ballot = Ballot.where(user: current_user).first_or_create
@ballot = Budget::Ballot.where(user: current_user, budget: @budget).first_or_create
end
end

View File

@@ -5,6 +5,8 @@ class Budget
has_many :lines, dependent: :destroy
has_many :investments, through: :lines
has_many :groups, -> { uniq }, through: :lines
has_many :headings, -> { uniq }, through: :groups
def add_investment(investment)
lines.create!(budget: budget, investment: investment, heading: investment.heading, group_id: investment.heading.group_id)
@@ -22,6 +24,10 @@ class Budget
budget.heading_price(heading) - amount_spent(heading.id)
end
def has_lines_in_group?(group)
self.groups.include?(group)
end
def valid_heading?(heading)
group = heading.group
return false if group.budget_id != budget_id
@@ -47,5 +53,10 @@ class Budget
def has_investment?(investment)
self.investment_ids.include?(investment.id)
end
def heading_for_group(group)
self.headings.where(group: group).first.id
end
end
end

View File

@@ -45,6 +45,7 @@ class Budget
scope :undecided, -> { where(feasibility: "undecided") }
scope :with_supports, -> { where('cached_votes_up > 0') }
scope :by_group, -> (group_id) { where(group_id: group_id) }
scope :by_heading, -> (heading_id) { where(heading_id: heading_id) }
scope :by_admin, -> (admin_id) { where(administrator_id: admin_id) }
scope :by_tag, -> (tag_name) { tagged_with(tag_name) }

View File

@@ -1,94 +0,0 @@
<div class="row ballot">
<%= render 'shared/back_link' %>
<h1 class="text-center"><%= t("budgets.ballots.show.title") %></h1>
<div class="small-12 medium-8 column small-centered text-center">
<h2>
<%= t("budgets.ballots.show.voted_html",
count: @ballot.investments.count) %>
</h2>
<% if @ballot.geozone.present? && district_wide_amount_spent(@ballot) > 0 %>
<%= social_share_button_tag("#{t('budgets.ballots.show.social_share',
amount: format_price(district_wide_amount_spent(@ballot)),
geozone: @ballot.geozone.name)} #{setting['twitter_hashtag']}",
url: participatory_budget_url) %>
<% end %>
<h3>
<%= t("budgets.ballots.show.remaining_city_html",
amount_city: format_price(@ballot.amount_available(nil))) %>
</h3>
<% if @ballot.geozone.present? %>
<h3>
<%= t("budgets.ballots.show.remaining_district_html",
amount_district: format_price(@ballot.amount_available(@ballot.geozone)),
geozone: @ballot.geozone.name) %>
</h3>
<% end %>
<p>
<small>
<%= t("budgets.ballots.show.voted_info_html") %>
</small>
</p>
</div>
<hr>
<div class="margin-top">
<div id="city_wide" class="small-12 medium-6 column">
<h3 class="subtitle">
<%= t("budgets.ballots.show.city_wide") %>
</h3>
<% if @ballot.investments.by_geozone(nil).count > 0 %>
<h4 class="amount-spent text-right">
<%= t("budgets.ballots.show.amount_spent") %>
<span><%= format_price(city_wide_amount_spent(@ballot)) %></span>
</h4>
<% else %>
<p>
<%= t("budgets.ballots.show.zero") %><br>
<%= link_to t("budgets.ballots.show.city_link"),
investments_path(geozone: 'all'),
data: { no_turbolink: true } %>
</p>
<% end %>
<ul class="ballot-list">
<%= render partial: 'budgets/ballots/investment',
collection: @ballot.investments.no_heading %>
</ul>
</div>
<div id="district_wide" class="small-12 medium-6 column">
<h3 class="subtitle">
<%= t("budgets.ballots.show.district_wide") %>
<span>
<% if @ballot.geozone.present? %>
(<%= @ballot.geozone.name %>)
<% end %>
</span>
</h3>
<% if @ballot.geozone.present? %>
<h4 class="amount-spent text-right">
<%= t("budgets.ballots.show.amount_spent") %>
<span><%= format_price(district_wide_amount_spent(@ballot)) %></span>
</h4>
<% else %>
<p>
<%= t("budgets.ballots.show.zero") %><br>
<%= link_to t("budgets.ballots.show.districts_link"), select_district_path %>
</p>
<% end %>
<ul class="ballot-list">
<%= render partial: 'budgets/ballots/investment',
collection: @ballot.investments.with_heading %>
</ul>
</div>
</div>
</div>

View File

@@ -1,14 +1,14 @@
<li id="<%= dom_id(investment) %>">
<%= link_to investment.title, investment %>
<span><%= format_price(investment.price) %></span>
<%= link_to investment.title, budget_investment_path(@budget, investment) %>
<span><%= format_price(@budget, investment.price) %></span>
<% if @budget.balloting? %>
<%= link_to ballot_line_path(id: investment.id),
<%# link_to budget_ballot_line_path(budget, id: investment.id),
title: t('budgets.ballots.show.remove'),
class: "remove-investment-project",
method: :delete,
remote: true do %>
<span class="icon-x"></span>
<% end %>
<%# end %>
<% end %>
</li>

View File

@@ -1,3 +1,57 @@
<div id="ballot">
<%= render partial: "ballots/ballot" %>
<div class="row ballot">
<%= render 'shared/back_link' %>
<h1 class="text-center"><%= t("budgets.ballots.show.title") %></h1>
<div class="small-12 medium-8 column small-centered text-center">
<h2>
<%= t("budgets.ballots.show.voted_html",
count: @ballot.investments.count) %>
</h2>
<p>
<small>
<%= t("budgets.ballots.show.voted_info_html") %>
</small>
</p>
</div>
<hr>
<div class="margin-top">
<% @ballot.groups.each do |group| %>
<div id="<%= dom_id(group) %>"
class="small-12 medium-6 column">
<h3 class="subtitle">
<%= group.name %> - <%= group.headings.first.name %>
</h3>
<% if @ballot.has_lines_in_group?(group) %>
<h4 class="amount-spent text-right">
<%= t("budgets.ballots.show.amount_spent") %>
<span>
<%= format_price(@budget, @ballot.amount_spent(@ballot.heading_for_group(group))) %>
</span>
</h4>
<% else %>
<p>
<%= t("budgets.ballots.show.zero") %><br>
<%= link_to t("budgets.ballots.show.heading_link"),
budget_investments_path(budget, heading_id: heading.id),
data: { no_turbolink: true } %>
</p>
<% end %>
<ul class="ballot-list">
<%= render partial: 'budgets/ballot/investment',
collection: @ballot.investments.by_group(group.id) %>
</ul>
<h4>
<%= t("budgets.ballots.show.remaining",
amount_city: format_price(@budget, @ballot.amount_available(group.headings.first))).html_safe %>
</h4>
</div>
<% end %>
</div>
</div>
</div>

View File

@@ -2,15 +2,15 @@ en:
budgets:
ballots:
show:
amount_spent: "pending translation"
city_wide: "pending translation"
districts_link: "pending translation"
remaining_district_html: "pending translation"
social_share: "pending translation"
title: "pending translation"
voted_html: "pending translation"
voted_info_html: "pending translation"
zero: "pending translation"
title: Your ballot
amount_spent: Amount spent
remaining: "You still have <span>%{amount_city}</span> to invest."
remove: Remove vote
voted_html:
one: "You have voted <span>one</span> proposal."
other: "You have voted <span>%{count}</span> proposals."
voted_info_html: "You can change your vote at any time until the close of this phase.<br> No need to invest all the money available."
zero: You have not voted any investment project.
budget:
phase:
on_hold: On hold

View File

@@ -2,15 +2,15 @@ es:
budgets:
ballots:
show:
amount_spent: "pending translation"
city_wide: "pending translation"
districts_link: "pending translation"
remaining_district_html: "pending translation"
social_share: "pending translation"
title: "pending translation"
voted_html: "pending translation"
voted_info_html: "pending translation"
zero: "pending translation"
title: Mis votos
amount_spent: Coste total
remaining: "Te quedan <span>%{amount_city}</span> para invertir"
remove: Quitar voto
voted_html:
one: "Has votado <span>una</span> propuesta."
other: "Has votado <span>%{count}</span> propuestas."
voted_info_html: "Puedes cambiar tus votos en cualquier momento hasta el cierre de esta fase.<br> No hace falta que inviertas todo el dinero disponible."
zero: "Todavía no has votado ninguna propuesta de inversión."
budget:
phase:
on_hold: En pausa

View File

@@ -74,7 +74,7 @@ Rails.application.routes.draw do
resources :investments, controller: "budgets/investments", only: [:index, :new, :create, :show, :destroy] do
member { post :vote }
end
resource :ballot, only: :show do
resource :ballot, only: :show, controller: "budgets/ballots" do
resources :lines, controller: "budgets/ballot/lines", only: [:create, :destroy]
end
end

View File

@@ -275,7 +275,7 @@ feature 'Ballots' do
expect(page).to have_css("#budget_heading_#{california_heading.id}.active")
end
scenario 'Change my heading', :js, :focus do
scenario 'Change my heading', :js do
investment1 = create(:budget_investment, :feasible, :finished, heading: california_heading)
investment2 = create(:budget_investment, :feasible, :finished, heading: new_york_heading)
@@ -299,9 +299,6 @@ feature 'Ballots' do
expect(page).to_not have_css("#budget_heading_#{california_heading.id}.active")
end
#From here on, not implemented yet
scenario 'View another heading' do
investment = create(:budget_investment, :feasible, :finished, heading: california_heading)
@@ -317,22 +314,47 @@ feature 'Ballots' do
end
context 'Showing the ballot' do
pending "do not display heading name if there is only one heading in the group (example: group city)"
scenario 'Displaying the correct count & amount' do
scenario 'Displaying the correct count & amount', :focus do
user = create(:user, :level_two)
geozone = create(:geozone, name: "Carabanchel")
ballot = create(:ballot, user: user, geozone: geozone)
ballot.spending_proposals =
create_list(:spending_proposal, 2, :feasible, :finished, price: 10) +
create_list(:spending_proposal, 3, :feasible, :finished, price: 5, geozone: geozone)
group1 = create(:budget_group, budget: budget)
group2 = create(:budget_group, budget: budget)
heading1 = create(:budget_heading, name: "District 1", group: group1)
heading2 = create(:budget_heading, name: "District 2", group: group2)
ballot = create(:budget_ballot, user: user, budget: budget)
investment1 = create(:budget_investment, :feasible, price: 10, heading: heading1, group: group1)
investment2 = create(:budget_investment, :feasible, price: 10, heading: heading1, group: group1)
investment3 = create(:budget_investment, :feasible, price: 5, heading: heading2, group: group2)
investment4 = create(:budget_investment, :feasible, price: 5, heading: heading2, group: group2)
investment5 = create(:budget_investment, :feasible, price: 5, heading: heading2, group: group2)
create(:budget_ballot_line, ballot: ballot, investment: investment1, heading: heading1, group: group1)
create(:budget_ballot_line, ballot: ballot, investment: investment2, heading: heading1, group: group1)
create(:budget_ballot_line, ballot: ballot, investment: investment3, heading: heading2, group: group2)
create(:budget_ballot_line, ballot: ballot, investment: investment4, heading: heading2, group: group2)
create(:budget_ballot_line, ballot: ballot, investment: investment5, heading: heading2, group: group2)
login_as(user)
visit ballot_path
visit budget_ballot_path(budget)
expect(page).to have_content("You voted 5 proposals")
within("#city_wide") { expect(page).to have_content "€20" }
within("#district_wide") { expect(page).to have_content "€15" }
expect(page).to have_content("You have voted 5 proposals")
within("#budget_group_#{group1.id}") do
expect(page).to have_content "#{group1.name} - #{heading1.name}"
expect(page).to have_content "€20"
end
within("#budget_group_#{group2.id}") do
expect(page).to have_content "#{group2.name} - #{heading2.name}"
expect(page).to have_content "€15"
end
end
end