From 22373c3dddae2408cd84602e03835285916337dc Mon Sep 17 00:00:00 2001 From: rgarcia Date: Fri, 9 Sep 2016 18:49:37 +0200 Subject: [PATCH] Review my votes page --- .../budgets/ballot/lines_controller.rb | 2 +- app/controllers/budgets/ballots_controller.rb | 5 +- app/models/budget/ballot.rb | 11 +++ app/models/budget/investment.rb | 1 + app/views/budgets/ballot/_ballot.html.erb | 94 ------------------- app/views/budgets/ballot/_investment.html.erb | 8 +- app/views/budgets/ballot/show.html.erb | 56 ++++++++++- config/locales/budgets.en.yml | 18 ++-- config/locales/budgets.es.yml | 18 ++-- config/routes.rb | 2 +- spec/features/budgets/ballots_spec.rb | 50 +++++++--- 11 files changed, 130 insertions(+), 135 deletions(-) delete mode 100644 app/views/budgets/ballot/_ballot.html.erb diff --git a/app/controllers/budgets/ballot/lines_controller.rb b/app/controllers/budgets/ballot/lines_controller.rb index 42f39a5b7..5483f40a3 100644 --- a/app/controllers/budgets/ballot/lines_controller.rb +++ b/app/controllers/budgets/ballot/lines_controller.rb @@ -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? diff --git a/app/controllers/budgets/ballots_controller.rb b/app/controllers/budgets/ballots_controller.rb index 1f435dc69..3c163253d 100644 --- a/app/controllers/budgets/ballots_controller.rb +++ b/app/controllers/budgets/ballots_controller.rb @@ -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 diff --git a/app/models/budget/ballot.rb b/app/models/budget/ballot.rb index b28c7d859..3f71921c0 100644 --- a/app/models/budget/ballot.rb +++ b/app/models/budget/ballot.rb @@ -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 diff --git a/app/models/budget/investment.rb b/app/models/budget/investment.rb index 56ae67d41..619602b2a 100644 --- a/app/models/budget/investment.rb +++ b/app/models/budget/investment.rb @@ -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) } diff --git a/app/views/budgets/ballot/_ballot.html.erb b/app/views/budgets/ballot/_ballot.html.erb deleted file mode 100644 index a3b0d22f3..000000000 --- a/app/views/budgets/ballot/_ballot.html.erb +++ /dev/null @@ -1,94 +0,0 @@ -
- - <%= render 'shared/back_link' %> - -

<%= t("budgets.ballots.show.title") %>

- -
-

- <%= t("budgets.ballots.show.voted_html", - count: @ballot.investments.count) %> -

- - <% 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 %> - -

- <%= t("budgets.ballots.show.remaining_city_html", - amount_city: format_price(@ballot.amount_available(nil))) %> -

- - <% if @ballot.geozone.present? %> -

- <%= t("budgets.ballots.show.remaining_district_html", - amount_district: format_price(@ballot.amount_available(@ballot.geozone)), - geozone: @ballot.geozone.name) %> -

- <% end %> - -

- - <%= t("budgets.ballots.show.voted_info_html") %> - -

-
- -
- -
-
-

- <%= t("budgets.ballots.show.city_wide") %> -

- <% if @ballot.investments.by_geozone(nil).count > 0 %> -

- <%= t("budgets.ballots.show.amount_spent") %> - <%= format_price(city_wide_amount_spent(@ballot)) %> -

- <% else %> -

- <%= t("budgets.ballots.show.zero") %>
- <%= link_to t("budgets.ballots.show.city_link"), - investments_path(geozone: 'all'), - data: { no_turbolink: true } %> -

- <% end %> - -
    - <%= render partial: 'budgets/ballots/investment', - collection: @ballot.investments.no_heading %> -
-
- -
-

- <%= t("budgets.ballots.show.district_wide") %> - - <% if @ballot.geozone.present? %> - (<%= @ballot.geozone.name %>) - <% end %> - -

- <% if @ballot.geozone.present? %> -

- <%= t("budgets.ballots.show.amount_spent") %> - <%= format_price(district_wide_amount_spent(@ballot)) %> -

- <% else %> -

- <%= t("budgets.ballots.show.zero") %>
- <%= link_to t("budgets.ballots.show.districts_link"), select_district_path %> -

- <% end %> - -
    - <%= render partial: 'budgets/ballots/investment', - collection: @ballot.investments.with_heading %> -
-
-
-
diff --git a/app/views/budgets/ballot/_investment.html.erb b/app/views/budgets/ballot/_investment.html.erb index af78b4168..a2358a434 100644 --- a/app/views/budgets/ballot/_investment.html.erb +++ b/app/views/budgets/ballot/_investment.html.erb @@ -1,14 +1,14 @@
  • - <%= link_to investment.title, investment %> - <%= format_price(investment.price) %> + <%= link_to investment.title, budget_investment_path(@budget, investment) %> + <%= format_price(@budget, investment.price) %> <% 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 %> - <% end %> + <%# end %> <% end %>
  • diff --git a/app/views/budgets/ballot/show.html.erb b/app/views/budgets/ballot/show.html.erb index 393444368..4b9808aef 100644 --- a/app/views/budgets/ballot/show.html.erb +++ b/app/views/budgets/ballot/show.html.erb @@ -1,3 +1,57 @@
    - <%= render partial: "ballots/ballot" %> +
    + + <%= render 'shared/back_link' %> + +

    <%= t("budgets.ballots.show.title") %>

    + +
    +

    + <%= t("budgets.ballots.show.voted_html", + count: @ballot.investments.count) %> +

    +

    + + <%= t("budgets.ballots.show.voted_info_html") %> + +

    +
    +
    + +
    + <% @ballot.groups.each do |group| %> +
    +

    + <%= group.name %> - <%= group.headings.first.name %> +

    + <% if @ballot.has_lines_in_group?(group) %> +

    + <%= t("budgets.ballots.show.amount_spent") %> + + <%= format_price(@budget, @ballot.amount_spent(@ballot.heading_for_group(group))) %> + +

    + <% else %> +

    + <%= t("budgets.ballots.show.zero") %>
    + <%= link_to t("budgets.ballots.show.heading_link"), + budget_investments_path(budget, heading_id: heading.id), + data: { no_turbolink: true } %> +

    + <% end %> + +
      + <%= render partial: 'budgets/ballot/investment', + collection: @ballot.investments.by_group(group.id) %> +
    + +

    + <%= t("budgets.ballots.show.remaining", + amount_city: format_price(@budget, @ballot.amount_available(group.headings.first))).html_safe %> +

    +
    + <% end %> +
    +
    diff --git a/config/locales/budgets.en.yml b/config/locales/budgets.en.yml index 953c263d3..f969274ee 100644 --- a/config/locales/budgets.en.yml +++ b/config/locales/budgets.en.yml @@ -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 %{amount_city} to invest." + remove: Remove vote + voted_html: + one: "You have voted one proposal." + other: "You have voted %{count} proposals." + voted_info_html: "You can change your vote at any time until the close of this phase.
    No need to invest all the money available." + zero: You have not voted any investment project. budget: phase: on_hold: On hold diff --git a/config/locales/budgets.es.yml b/config/locales/budgets.es.yml index db9e480da..893a61706 100644 --- a/config/locales/budgets.es.yml +++ b/config/locales/budgets.es.yml @@ -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 %{amount_city} para invertir" + remove: Quitar voto + voted_html: + one: "Has votado una propuesta." + other: "Has votado %{count} propuestas." + voted_info_html: "Puedes cambiar tus votos en cualquier momento hasta el cierre de esta fase.
    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 diff --git a/config/routes.rb b/config/routes.rb index 19cdf89b3..e2d3bc053 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 diff --git a/spec/features/budgets/ballots_spec.rb b/spec/features/budgets/ballots_spec.rb index 47bedc776..924df42f8 100644 --- a/spec/features/budgets/ballots_spec.rb +++ b/spec/features/budgets/ballots_spec.rb @@ -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