allows user to support only in one heading per group

This commit is contained in:
rgarcia
2017-01-13 15:24:57 +01:00
parent a771408f4b
commit b2b9954e6d
8 changed files with 102 additions and 2 deletions

View File

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

View File

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

View File

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

View File

@@ -14,12 +14,12 @@
<%= t("budgets.investments.investment.already_supported") %>
</div>
<% 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 %>

View File

@@ -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"

View File

@@ -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"

View File

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

View File

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