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 @@ -
- - <%= t("budgets.ballots.show.voted_info_html") %> - -
-
- <%= t("budgets.ballots.show.zero") %>
- <%= link_to t("budgets.ballots.show.city_link"),
- investments_path(geozone: 'all'),
- data: { no_turbolink: true } %>
-
- <%= t("budgets.ballots.show.zero") %>
- <%= link_to t("budgets.ballots.show.districts_link"), select_district_path %>
-
+ + <%= t("budgets.ballots.show.voted_info_html") %> + +
+
+ <%= 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 } %>
+