From 8945058a654472b44c7f8259c6f97abcfc1e0fe6 Mon Sep 17 00:00:00 2001 From: taitus Date: Mon, 21 Mar 2022 00:45:46 +0100 Subject: [PATCH 1/3] Extract the load_map logic from the :load_heading callback Create a new callback for load_map. In this way we simplify a bit the logic of :load_heading callback and unify the logic of the map loading. --- app/controllers/budgets/investments_controller.rb | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/app/controllers/budgets/investments_controller.rb b/app/controllers/budgets/investments_controller.rb index 3953def8c..07434952d 100644 --- a/app/controllers/budgets/investments_controller.rb +++ b/app/controllers/budgets/investments_controller.rb @@ -20,6 +20,7 @@ module Budgets before_action :load_ballot, only: [:index, :show] before_action :load_heading, only: [:index, :show] + before_action :load_map, only: [:index] before_action :set_random_seed, only: :index before_action :load_categories, only: :index before_action :set_default_investment_filter, only: :index @@ -134,10 +135,8 @@ module Budgets if params[:heading_id].present? @heading = @budget.headings.find_by_slug_or_id! params[:heading_id] @assigned_heading = @ballot&.heading_for_group(@heading.group) - load_map elsif @budget.single_heading? @heading = @budget.headings.first - load_map end end @@ -180,7 +179,7 @@ module Budgets end def load_map - @map_location = MapLocation.load_from_heading(@heading) + @map_location = MapLocation.load_from_heading(@heading) if @heading.present? end end end From 78ab37db095035e5a61002518d5745c76052d958 Mon Sep 17 00:00:00 2001 From: taitus Date: Mon, 21 Mar 2022 01:38:02 +0100 Subject: [PATCH 2/3] Reduce duplicated lines on :investments method This way we also create a new method :investments_with_filters that we can call to retrieve the investments when we have not a @current_order. --- app/controllers/budgets/investments_controller.rb | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/app/controllers/budgets/investments_controller.rb b/app/controllers/budgets/investments_controller.rb index 07434952d..dd2fd1146 100644 --- a/app/controllers/budgets/investments_controller.rb +++ b/app/controllers/budgets/investments_controller.rb @@ -160,13 +160,15 @@ module Budgets @view = (params[:view] == "minimal") ? "minimal" : "default" end + def investments_with_filters + @budget.investments.apply_filters_and_search(@budget, params, @current_filter) + end + def investments if @current_order == "random" - @budget.investments.apply_filters_and_search(@budget, params, @current_filter) - .sort_by_random(session[:random_seed]) + investments_with_filters.sort_by_random(session[:random_seed]) else - @budget.investments.apply_filters_and_search(@budget, params, @current_filter) - .send("sort_by_#{@current_order}") + investments_with_filters.send("sort_by_#{@current_order}") end end From 0c9a46221e32e6a5442b6334e8531a68877afd79 Mon Sep 17 00:00:00 2001 From: Julian Herrero Date: Mon, 17 Jan 2022 16:47:37 +0700 Subject: [PATCH 3/3] Show assigned heading on investment show --- .../budgets/investments/ballot_component.rb | 9 ++++++--- app/views/budgets/investments/_ballot.html.erb | 3 +-- .../budgets/investments/ballot_component_spec.rb | 3 +-- spec/system/budgets/investments_spec.rb | 16 ++++++++++++++++ 4 files changed, 24 insertions(+), 7 deletions(-) diff --git a/app/components/budgets/investments/ballot_component.rb b/app/components/budgets/investments/ballot_component.rb index 539e80afd..cf34802b7 100644 --- a/app/components/budgets/investments/ballot_component.rb +++ b/app/components/budgets/investments/ballot_component.rb @@ -1,12 +1,11 @@ class Budgets::Investments::BallotComponent < ApplicationComponent - attr_reader :investment, :investment_ids, :ballot, :assigned_heading + attr_reader :investment, :investment_ids, :ballot delegate :current_user, :heading_link, :link_to_verify_account, to: :helpers - def initialize(investment:, investment_ids:, ballot:, assigned_heading:) + def initialize(investment:, investment_ids:, ballot:) @investment = investment @investment_ids = investment_ids @ballot = ballot - @assigned_heading = assigned_heading end private @@ -42,6 +41,10 @@ class Budgets::Investments::BallotComponent < ApplicationComponent budget_ballot_path(budget)) end + def assigned_heading + ballot.heading_for_group(investment.group) + end + def cannot_vote_text if reason.present? && !voted? t("budgets.ballots.reasons_for_not_balloting.#{reason}", diff --git a/app/views/budgets/investments/_ballot.html.erb b/app/views/budgets/investments/_ballot.html.erb index 143072cb8..967ba9524 100644 --- a/app/views/budgets/investments/_ballot.html.erb +++ b/app/views/budgets/investments/_ballot.html.erb @@ -1,6 +1,5 @@ <%= render Budgets::Investments::BallotComponent.new( investment: investment, investment_ids: investment_ids, - ballot: ballot, - assigned_heading: @assigned_heading + ballot: ballot ) %> diff --git a/spec/components/budgets/investments/ballot_component_spec.rb b/spec/components/budgets/investments/ballot_component_spec.rb index 797c67986..347380954 100644 --- a/spec/components/budgets/investments/ballot_component_spec.rb +++ b/spec/components/budgets/investments/ballot_component_spec.rb @@ -8,8 +8,7 @@ describe Budgets::Investments::BallotComponent do Budgets::Investments::BallotComponent.new( investment: investment, investment_ids: [], - ballot: Budget::Ballot.where(budget: budget, user: controller.current_user).first_or_create!, - assigned_heading: nil + ballot: Budget::Ballot.where(budget: budget, user: controller.current_user).first_or_create! ) end diff --git a/spec/system/budgets/investments_spec.rb b/spec/system/budgets/investments_spec.rb index 0dec3fd34..5917bf48f 100644 --- a/spec/system/budgets/investments_spec.rb +++ b/spec/system/budgets/investments_spec.rb @@ -1389,6 +1389,22 @@ describe "Budget Investments" do expect(page).to have_content "€10,000" end + scenario "Show message if user already voted in other heading" do + group = create(:budget_group, budget: budget, name: "Global Group") + heading = create(:budget_heading, group: group, name: "Heading 1") + investment = create(:budget_investment, :selected, heading: heading) + heading2 = create(:budget_heading, group: group, name: "Heading 2") + investment2 = create(:budget_investment, :selected, heading: heading2) + user = create(:user, :level_two, ballot_lines: [investment]) + + login_as(user) + visit budget_investment_path(budget, investment2) + + expect(page).to have_selector(".participation-not-allowed", + text: "You have already voted a different heading: Heading 1", + visible: :hidden) + end + scenario "Sidebar in show should display vote text" do investment = create(:budget_investment, :selected, budget: budget) visit budget_investment_path(budget, investment)