From b2b9954e6d38755d06694326b5f2de7cedca037d Mon Sep 17 00:00:00 2001 From: rgarcia Date: Fri, 13 Jan 2017 15:24:57 +0100 Subject: [PATCH 1/2] allows user to support only in one heading per group --- .../budgets/investments_controller.rb | 4 ++ app/models/budget/investment.rb | 19 ++++++ app/models/user.rb | 4 ++ app/views/budgets/investments/_votes.html.erb | 4 +- config/locales/budgets.en.yml | 1 + config/locales/budgets.es.yml | 1 + spec/features/budgets/investments_spec.rb | 59 +++++++++++++++++++ spec/models/budget/investment_spec.rb | 12 ++++ 8 files changed, 102 insertions(+), 2 deletions(-) diff --git a/app/controllers/budgets/investments_controller.rb b/app/controllers/budgets/investments_controller.rb index 235163f2e..932d760b0 100644 --- a/app/controllers/budgets/investments_controller.rb +++ b/app/controllers/budgets/investments_controller.rb @@ -61,6 +61,10 @@ module Budgets def vote @investment.register_selection(current_user) load_investment_votes(@investment) + respond_to do |format| + format.html { redirect_to budget_investments_path(heading_id: @investment.heading.id) } + format.js + end end private diff --git a/app/models/budget/investment.rb b/app/models/budget/investment.rb index 5389825e5..d3485aad9 100644 --- a/app/models/budget/investment.rb +++ b/app/models/budget/investment.rb @@ -146,6 +146,7 @@ class Budget def reason_for_not_being_selectable_by(user) return permission_problem(user) if permission_problem?(user) + return :different_heading_assigned unless valid_heading?(user) return :no_selecting_allowed unless budget.selecting? end @@ -173,6 +174,24 @@ class Budget reason_for_not_being_selectable_by(user).blank? end + def valid_heading?(user) + !different_heading_assigned?(user) + end + + def different_heading_assigned?(user) + other_heading_ids = group.heading_ids - [heading.id] + voted_in?(other_heading_ids, user) + end + + def voted_in?(heading_ids, user) + heading_ids.include? heading_voted_by_user?(user) + end + + def heading_voted_by_user?(user) + user.votes.for_budget_investments(budget.investments.where(group: group)). + votables.map(&:heading_id).first + end + def ballotable_by?(user) reason_for_not_being_ballotable_by(user).blank? end diff --git a/app/models/user.rb b/app/models/user.rb index 881f5700d..f8991d676 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -103,6 +103,10 @@ class User < ActiveRecord::Base comment_flags.each_with_object({}){ |f, h| h[f.flaggable_id] = true } end + def voted_in_group?(group) + votes.for_budget_investments(Budget::Investment.where(group: group)).exists? + end + def administrator? administrator.present? end diff --git a/app/views/budgets/investments/_votes.html.erb b/app/views/budgets/investments/_votes.html.erb index 6d62b9a02..c2870d76c 100644 --- a/app/views/budgets/investments/_votes.html.erb +++ b/app/views/budgets/investments/_votes.html.erb @@ -14,12 +14,12 @@ <%= t("budgets.investments.investment.already_supported") %> <% elsif investment.should_show_votes? %> - <%= link_to vote_url, class: "button button-support small expanded", title: t('budgets.investments.investment.support_title'), method: "post", - remote: true, + remote: (current_user && current_user.voted_in_group?(investment.group) ? true : false), + data: (current_user && current_user.voted_in_group?(investment.group) ? nil : { confirm: t('budgets.investments.investment.confirm_group')} ), "aria-hidden" => css_for_aria_hidden(reason) do %> <%= t("budgets.investments.investment.give_support") %> <% end %> diff --git a/config/locales/budgets.en.yml b/config/locales/budgets.en.yml index 3ce0bb3ee..538681694 100644 --- a/config/locales/budgets.en.yml +++ b/config/locales/budgets.en.yml @@ -84,6 +84,7 @@ en: already_added: You have already added this investment project already_supported: You have already supported this. Share it! support_title: Support this project + confirm_group: "You can only support investments in one district. If you continue you cannot change your decision. Are you sure?" supports: one: 1 support other: "%{count} supports" diff --git a/config/locales/budgets.es.yml b/config/locales/budgets.es.yml index d558d5edd..db4c02949 100644 --- a/config/locales/budgets.es.yml +++ b/config/locales/budgets.es.yml @@ -84,6 +84,7 @@ es: already_added: "Ya has añadido esta propuesta de inversión" already_supported: Ya has apoyado esta propuesta. ¡Compártelo! support_title: Apoyar esta propuesta + confirm_group: "Sólo puedes apoyar propuestas en un distrito. Si sigues adelante no podrás cambiar esta decisión. ¿Estás seguro?" supports: one: 1 apoyo other: "%{count} apoyos" diff --git a/spec/features/budgets/investments_spec.rb b/spec/features/budgets/investments_spec.rb index 98540aa68..9f3a0cc66 100644 --- a/spec/features/budgets/investments_spec.rb +++ b/spec/features/budgets/investments_spec.rb @@ -288,6 +288,65 @@ feature 'Budget Investments' do end + context "Selecting Phase" do + + background do + budget.update(phase: "selecting") + end + + context "Popup alert to vote only in one heading per group" do + + scenario "When supporting in the first heading group", :js, :focus do + carabanchel = create(:budget_heading, group: group) + salamanca = create(:budget_heading, group: group) + + carabanchel_investment = create(:budget_investment, :selected, heading: carabanchel) + salamanca_investment = create(:budget_investment, :selected, heading: salamanca) + + visit budget_investments_path(budget, heading_id: carabanchel.id) + + within("#budget_investment_#{carabanchel_investment.id}") do + expect(page).to have_css(".in-favor a[data-confirm]") + end + end + + scenario "When already supported in the group", :js, :focus do + carabanchel = create(:budget_heading, group: group) + salamanca = create(:budget_heading, group: group) + + carabanchel_investment = create(:budget_investment, heading: carabanchel) + salamanca_investment = create(:budget_investment, heading: salamanca) + + create(:vote, votable: carabanchel_investment, voter: author) + + login_as(author) + visit budget_investments_path(budget, heading_id: carabanchel.id) + + within("#budget_investment_#{carabanchel_investment.id}") do + expect(page).to_not have_css(".in-favor a[data-confirm]") + end + end + + scenario "When supporting in another group", :js, :focus do + carabanchel = create(:budget_heading, group: group) + another_heading = create(:budget_heading, group: create(:budget_group, budget: budget)) + + carabanchel_investment = create(:budget_investment, heading: carabanchel) + another_group_investment = create(:budget_investment, heading: another_heading) + + create(:vote, votable: carabanchel_investment, voter: author) + + login_as(author) + visit budget_investments_path(budget, heading_id: another_heading.id) + + within("#budget_investment_#{another_group_investment.id}") do + expect(page).to have_css(".in-favor a[data-confirm]") + end + end + end + + end + context "Balloting Phase" do background do diff --git a/spec/models/budget/investment_spec.rb b/spec/models/budget/investment_spec.rb index 0c6615a99..e9fa58593 100644 --- a/spec/models/budget/investment_spec.rb +++ b/spec/models/budget/investment_spec.rb @@ -236,6 +236,18 @@ describe Budget::Investment do budget.phase = "selecting" expect(district_sp.reason_for_not_being_selectable_by(user)).to be_nil end + + it "rejects votes in two headings of the same group" do + carabanchel = create(:budget_heading, group: group) + salamanca = create(:budget_heading, group: group) + + carabanchel_investment = create(:budget_investment, heading: carabanchel) + salamanca_investment = create(:budget_investment, heading: salamanca) + + create(:vote, votable: carabanchel_investment, voter: user) + + expect(salamanca_investment.valid_heading?(user)).to eq(false) + end end end From 17946b292bc00e9461394726886bb4001a522427 Mon Sep 17 00:00:00 2001 From: rgarcia Date: Fri, 13 Jan 2017 16:37:39 +0100 Subject: [PATCH 2/2] fixes specs --- .../management/budgets/investments_controller.rb | 4 ++++ spec/features/budgets/investments_spec.rb | 6 +++--- spec/features/budgets/votes_spec.rb | 2 +- spec/features/management/budget_investments_spec.rb | 2 +- 4 files changed, 9 insertions(+), 5 deletions(-) diff --git a/app/controllers/management/budgets/investments_controller.rb b/app/controllers/management/budgets/investments_controller.rb index e790312d6..025b6301d 100644 --- a/app/controllers/management/budgets/investments_controller.rb +++ b/app/controllers/management/budgets/investments_controller.rb @@ -34,6 +34,10 @@ class Management::Budgets::InvestmentsController < Management::BaseController def vote @investment.register_selection(managed_user) load_investment_votes(@investment) + respond_to do |format| + format.html { redirect_to management_budget_investments_path(heading_id: @investment.heading.id) } + format.js + end end def print diff --git a/spec/features/budgets/investments_spec.rb b/spec/features/budgets/investments_spec.rb index 9f3a0cc66..41297230d 100644 --- a/spec/features/budgets/investments_spec.rb +++ b/spec/features/budgets/investments_spec.rb @@ -296,7 +296,7 @@ feature 'Budget Investments' do context "Popup alert to vote only in one heading per group" do - scenario "When supporting in the first heading group", :js, :focus do + scenario "When supporting in the first heading group", :js do carabanchel = create(:budget_heading, group: group) salamanca = create(:budget_heading, group: group) @@ -310,7 +310,7 @@ feature 'Budget Investments' do end end - scenario "When already supported in the group", :js, :focus do + scenario "When already supported in the group", :js do carabanchel = create(:budget_heading, group: group) salamanca = create(:budget_heading, group: group) @@ -327,7 +327,7 @@ feature 'Budget Investments' do end end - scenario "When supporting in another group", :js, :focus do + scenario "When supporting in another group", :js do carabanchel = create(:budget_heading, group: group) another_heading = create(:budget_heading, group: create(:budget_group, budget: budget)) diff --git a/spec/features/budgets/votes_spec.rb b/spec/features/budgets/votes_spec.rb index c31545428..882e98b45 100644 --- a/spec/features/budgets/votes_spec.rb +++ b/spec/features/budgets/votes_spec.rb @@ -55,7 +55,7 @@ feature 'Votes' do feature 'Single spending proposal' do background do - @investment = create(:budget_investment, budget: budget) + @investment = create(:budget_investment, budget: budget, heading: heading) end scenario 'Show no votes' do diff --git a/spec/features/management/budget_investments_spec.rb b/spec/features/management/budget_investments_spec.rb index 02f4cf36f..5b3961306 100644 --- a/spec/features/management/budget_investments_spec.rb +++ b/spec/features/management/budget_investments_spec.rb @@ -140,7 +140,7 @@ feature 'Budget Investments' do context "Supporting" do scenario 'Supporting budget investments on behalf of someone in index view', :js do - budget_investment = create(:budget_investment, budget: @budget) + budget_investment = create(:budget_investment, budget: @budget, heading: @heading) user = create(:user, :level_two) login_managed_user(user)