diff --git a/app/controllers/budgets/ballot/lines_controller.rb b/app/controllers/budgets/ballot/lines_controller.rb new file mode 100644 index 000000000..36d0f5a40 --- /dev/null +++ b/app/controllers/budgets/ballot/lines_controller.rb @@ -0,0 +1,80 @@ +module Budgets + module Ballot + class LinesController < ApplicationController + before_action :authenticate_user! + #before_action :ensure_final_voting_allowed + before_action :load_budget + before_action :load_ballot + + before_action :load_investments + + load_and_authorize_resource :budget + load_and_authorize_resource :ballot, class: "Budget::Ballot" + load_and_authorize_resource :line, through: :ballot, find_by: :investment_id, class: "Budget::Ballot::Line" + + def create + load_investment + load_heading + + if @line.save + #@ballot.set_geozone(@geozone) + #@current_user.update(representative_id: nil) + if request.get? + redirect_to @spending_proposal, notice: t('spending_proposals.notice.voted') + end + else + if request.get? + redirect_to @spending_proposal, notice: t('spending_proposals.notice.could_not_vote') + else + render :new + end + end + end + + def destroy + load_spending_proposal + load_spending_proposals + load_geozone + + @ballot_line.destroy + @ballot.reset_geozone + end + + private + + def ensure_final_voting_allowed + return head(:forbidden) unless @budget.balloting? + end + + def line_params + params.permit(:investment_id) + end + + def load_budget + @budget = Budget.find(params[:budget_id]) + end + + def load_ballot + @ballot = Budget::Ballot.where(user: current_user, budget: @budget).first_or_create + end + + def load_investment + @investment = Budget::Investment.find(params[:investment_id]) + end + + def load_investments + if params[:investments_ids].present? + @investments = Budget::Investment.where(id: params[:investments_ids]) + end + end + + def load_heading + @heading = @investment.heading + end + + def load_geozone + @geozone = @line.investment.geozone + end + end + end +end \ No newline at end of file diff --git a/app/controllers/budgets/ballots_controller.rb b/app/controllers/budgets/ballots_controller.rb new file mode 100644 index 000000000..1f435dc69 --- /dev/null +++ b/app/controllers/budgets/ballots_controller.rb @@ -0,0 +1,17 @@ +module Budgets + class BallotsController < ApplicationController + before_action :authenticate_user! + before_action :load_ballot + load_and_authorize_resource + + def show + end + + private + + def load_ballot + @ballot = Ballot.where(user: current_user).first_or_create + end + + end +end \ No newline at end of file diff --git a/app/helpers/ballots_helper.rb b/app/helpers/ballots_helper.rb new file mode 100644 index 000000000..ece1be9fa --- /dev/null +++ b/app/helpers/ballots_helper.rb @@ -0,0 +1,7 @@ +module BallotsHelper + + def progress_bar_width(amount_available, amount_spent) + (amount_spent/amount_available.to_f * 100).to_s + "%" + end + +end \ No newline at end of file diff --git a/app/helpers/budget_helper.rb b/app/helpers/budget_helper.rb index 8c19516e8..745ea88f6 100644 --- a/app/helpers/budget_helper.rb +++ b/app/helpers/budget_helper.rb @@ -20,4 +20,8 @@ module BudgetHelper budget_investment_path(investment, options) end end + + def display_budget_countdown?(budget) + budget.balloting? + end end diff --git a/app/models/abilities/common.rb b/app/models/abilities/common.rb index fb03cbea2..fddf4918b 100644 --- a/app/models/abilities/common.rb +++ b/app/models/abilities/common.rb @@ -45,9 +45,10 @@ module Abilities can :vote, SpendingProposal can :create, SpendingProposal - can :create, Budget::Investment, budget: { phase: "accepting" } - can :vote, Budget::Investment, budget: { phase: "selecting" } - can :create, Budget::Ballot, budget: { phase: "balloting" } + can :create, Budget::Investment, budget: { phase: "accepting" } + can :vote, Budget::Investment, budget: { phase: "selecting" } + can [:show, :create], Budget::Ballot, budget: { phase: "balloting" } + can [:create, :destroy], Budget::Ballot::Line#, budget: { phase: "balloting" } can :create, DirectMessage can :show, DirectMessage, sender_id: user.id diff --git a/app/models/abilities/everyone.rb b/app/models/abilities/everyone.rb index 561ac5994..30c1c3b55 100644 --- a/app/models/abilities/everyone.rb +++ b/app/models/abilities/everyone.rb @@ -6,7 +6,7 @@ module Abilities can [:read, :map], Debate can [:read, :map, :summary], Proposal can :read, Comment - can :read, Budget + can [:read, :welcome], Budget can :read, Budget::Investment can :read, SpendingProposal can :read, Legislation diff --git a/app/views/budgets/ballots/_add.html.erb b/app/views/budgets/ballot/_add.html.erb similarity index 100% rename from app/views/budgets/ballots/_add.html.erb rename to app/views/budgets/ballot/_add.html.erb diff --git a/app/views/budgets/ballots/_ballot.html.erb b/app/views/budgets/ballot/_ballot.html.erb similarity index 100% rename from app/views/budgets/ballots/_ballot.html.erb rename to app/views/budgets/ballot/_ballot.html.erb diff --git a/app/views/budgets/ballots/_investment.html.erb b/app/views/budgets/ballot/_investment.html.erb similarity index 100% rename from app/views/budgets/ballots/_investment.html.erb rename to app/views/budgets/ballot/_investment.html.erb diff --git a/app/views/budgets/ballots/_investment_for_sidebar.html.erb b/app/views/budgets/ballot/_investment_for_sidebar.html.erb similarity index 64% rename from app/views/budgets/ballots/_investment_for_sidebar.html.erb rename to app/views/budgets/ballot/_investment_for_sidebar.html.erb index c229cc93b..d9b3d9e69 100644 --- a/app/views/budgets/ballots/_investment_for_sidebar.html.erb +++ b/app/views/budgets/ballot/_investment_for_sidebar.html.erb @@ -1,10 +1,10 @@
+ <%= format_price(@budget, @ballot.amount_spent(@heading.id)) %> + + <%= t("spending_proposals.index.available") %> + <%= format_price(@budget, @ballot.amount_available(@heading)) %> + +
+ +- <%= format_price(investment.price) %> + <%= format_price(@budget, investment.price) %>
<% if @budget.balloting? %> <%= link_to t('budgets.ballots.show.remove'), - ballot_line_path(id: investment.id, + budget_ballot_line_path(id: investment.id, investments_ids: investment_ids), class: "delete small expanded", method: :delete, diff --git a/app/views/budgets/ballot/lines/_refresh_ballots.js.erb b/app/views/budgets/ballot/lines/_refresh_ballots.js.erb new file mode 100644 index 000000000..74e05639b --- /dev/null +++ b/app/views/budgets/ballot/lines/_refresh_ballots.js.erb @@ -0,0 +1,5 @@ +<% if @spending_proposals.present? %> + <% @spending_proposals.each do |spending_proposal| %> + $("#<%= dom_id(spending_proposal) %>_ballot").html('<%= j render("spending_proposals/ballot", spending_proposal: spending_proposal) %>'); + <% end %> +<% end %> \ No newline at end of file diff --git a/app/views/budgets/ballot/lines/create.js.erb b/app/views/budgets/ballot/lines/create.js.erb new file mode 100644 index 000000000..5680fb063 --- /dev/null +++ b/app/views/budgets/ballot/lines/create.js.erb @@ -0,0 +1,5 @@ +$("#progress_bar").html('<%= j render("budgets/ballot/progress_bar", ballot: @ballot) %>'); +$("#sidebar").html('<%= j render("budgets/investments/sidebar") %>'); +$("#<%= dom_id(@investment) %>_ballot").html('<%= j render("budgets/investments/ballot", investment: @investment) %>'); + +<%= render 'refresh_ballots' %> diff --git a/app/views/budgets/ballot/lines/destroy.js.erb b/app/views/budgets/ballot/lines/destroy.js.erb new file mode 100644 index 000000000..454011207 --- /dev/null +++ b/app/views/budgets/ballot/lines/destroy.js.erb @@ -0,0 +1,6 @@ +$("#progress_bar").html('<%= j render("spending_proposals/progress_bar", ballot: @ballot) %>'); +$("#sidebar").html('<%= j render("spending_proposals/sidebar") %>'); +$("#ballot").html('<%= j render("ballots/ballot") %>') +$("#<%= dom_id(@spending_proposal) %>_ballot").html('<%= j render("spending_proposals/ballot", spending_proposal: @spending_proposal) %>'); + +<%= render 'refresh_ballots' %> diff --git a/app/views/budgets/ballot/lines/new.js.erb b/app/views/budgets/ballot/lines/new.js.erb new file mode 100644 index 000000000..2e8254866 --- /dev/null +++ b/app/views/budgets/ballot/lines/new.js.erb @@ -0,0 +1,2 @@ +$("#<%= dom_id(@spending_proposal) %>_ballot").html('<%= j render("spending_proposals/ballot", spending_proposal: @spending_proposal) %>'); +$(".no-supports-allowed").show(); \ No newline at end of file diff --git a/app/views/budgets/ballots/show.html.erb b/app/views/budgets/ballot/show.html.erb similarity index 100% rename from app/views/budgets/ballots/show.html.erb rename to app/views/budgets/ballot/show.html.erb diff --git a/app/views/budgets/investments/_ballot.html.erb b/app/views/budgets/investments/_ballot.html.erb index 6634ae1fa..b4aa8af97 100644 --- a/app/views/budgets/investments/_ballot.html.erb +++ b/app/views/budgets/investments/_ballot.html.erb @@ -1,9 +1,13 @@ <% reason = investment.reason_for_not_being_ballotable_by(current_user, @ballot) %>