From fb8c476fb21c521835425d8d0752d95f5f081410 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Wed, 17 Mar 2021 17:58:15 +0100 Subject: [PATCH] Extract component for ballot investment Using component inheritance we can remove duplication and share the same view. --- app/assets/stylesheets/application.scss | 2 +- .../budgets/ballot/investment.scss | 51 ++++++++++++++++++ app/assets/stylesheets/participation.scss | 52 ------------------- .../ballot/investment_component.html.erb} | 9 ++-- .../budgets/ballot/investment_component.rb | 25 +++++++++ .../investment_for_sidebar_component.rb | 23 ++++++++ app/views/budgets/ballot/_ballot.html.erb | 5 +- app/views/budgets/ballot/_investment.html.erb | 14 ----- .../budgets/investments/_sidebar.html.erb | 9 ++-- 9 files changed, 111 insertions(+), 79 deletions(-) create mode 100644 app/assets/stylesheets/budgets/ballot/investment.scss rename app/{views/budgets/ballot/_investment_for_sidebar.html.erb => components/budgets/ballot/investment_component.html.erb} (61%) create mode 100644 app/components/budgets/ballot/investment_component.rb create mode 100644 app/components/budgets/ballot/investment_for_sidebar_component.rb delete mode 100644 app/views/budgets/ballot/_investment.html.erb diff --git a/app/assets/stylesheets/application.scss b/app/assets/stylesheets/application.scss index 93d6472cd..380278a1a 100644 --- a/app/assets/stylesheets/application.scss +++ b/app/assets/stylesheets/application.scss @@ -33,7 +33,7 @@ @import "sticky_overrides"; @import "tags"; @import "admin/**/*"; -@import "budgets/*"; +@import "budgets/**/*"; @import "sdg/**/*"; @import "sdg_management/*"; @import "sdg_management/**/*"; diff --git a/app/assets/stylesheets/budgets/ballot/investment.scss b/app/assets/stylesheets/budgets/ballot/investment.scss new file mode 100644 index 000000000..bc33064c8 --- /dev/null +++ b/app/assets/stylesheets/budgets/ballot/investment.scss @@ -0,0 +1,51 @@ +.ballot-list li { + background: #f9f9f9; + line-height: $line-height; + margin-bottom: $line-height / 4; + padding: $line-height $line-height / 2; + position: relative; + + a { + color: $text; + } + + span { + display: block; + font-style: italic; + } + + .remove-investment-project { + display: block; + height: 0; + + .icon-x { + color: #9f9f9f; + font-size: rem-calc(24); + line-height: $line-height / 2; + position: absolute; + right: 6px; + text-decoration: none; + top: 6px; + + @include breakpoint(medium) { + font-size: $base-font-size; + } + } + } + + &:hover { + background: $budget; + color: #fff; + + a, + span { + color: #fff; + outline: 0; + text-decoration: none; + } + + .remove-investment-project .icon-x { + color: #fff; + } + } +} diff --git a/app/assets/stylesheets/participation.scss b/app/assets/stylesheets/participation.scss index 2b9313af6..94b7be37b 100644 --- a/app/assets/stylesheets/participation.scss +++ b/app/assets/stylesheets/participation.scss @@ -1321,58 +1321,6 @@ .ballot-list { list-style: none; margin-left: 0; - - li { - background: #f9f9f9; - line-height: $line-height; - margin-bottom: $line-height / 4; - padding: $line-height $line-height / 2; - position: relative; - - a { - color: $text; - } - - span { - display: block; - font-style: italic; - } - - .remove-investment-project { - display: block; - height: 0; - - .icon-x { - color: #9f9f9f; - font-size: rem-calc(24); - line-height: $line-height / 2; - position: absolute; - right: 6px; - text-decoration: none; - top: 6px; - - @include breakpoint(medium) { - font-size: $base-font-size; - } - } - } - - &:hover { - background: $budget; - color: #fff; - - a, - span { - color: #fff; - outline: 0; - text-decoration: none; - } - - .remove-investment-project .icon-x { - color: #fff; - } - } - } } .select-district a { diff --git a/app/views/budgets/ballot/_investment_for_sidebar.html.erb b/app/components/budgets/ballot/investment_component.html.erb similarity index 61% rename from app/views/budgets/ballot/_investment_for_sidebar.html.erb rename to app/components/budgets/ballot/investment_component.html.erb index a0c268a35..95fbb7179 100644 --- a/app/views/budgets/ballot/_investment_for_sidebar.html.erb +++ b/app/components/budgets/ballot/investment_component.html.erb @@ -1,10 +1,9 @@ -
  • - <%= investment.title %> +
  • + <%= investment_title %> <%= investment.formatted_price %> - <% if @budget.balloting? %> - <%= link_to budget_ballot_line_path(id: investment.id, - investments_ids: investment_ids), + <% if budget.balloting? %> + <%= link_to delete_path, title: t("budgets.ballots.show.remove"), class: "remove-investment-project", method: :delete, diff --git a/app/components/budgets/ballot/investment_component.rb b/app/components/budgets/ballot/investment_component.rb new file mode 100644 index 000000000..6b7d8e74a --- /dev/null +++ b/app/components/budgets/ballot/investment_component.rb @@ -0,0 +1,25 @@ +class Budgets::Ballot::InvestmentComponent < ApplicationComponent + attr_reader :investment + + def initialize(investment:) + @investment = investment + end + + private + + def budget + investment.budget + end + + def list_item_id + dom_id(investment) + end + + def investment_title + link_to investment.title, budget_investment_path(budget, investment) + end + + def delete_path + budget_ballot_line_path(budget, id: investment.id) + end +end diff --git a/app/components/budgets/ballot/investment_for_sidebar_component.rb b/app/components/budgets/ballot/investment_for_sidebar_component.rb new file mode 100644 index 000000000..33b0a9533 --- /dev/null +++ b/app/components/budgets/ballot/investment_for_sidebar_component.rb @@ -0,0 +1,23 @@ +class Budgets::Ballot::InvestmentForSidebarComponent < Budgets::Ballot::InvestmentComponent + with_collection_parameter :investment + attr_reader :investment_ids + + def initialize(investment:, investment_ids:) + super(investment: investment) + @investment_ids = investment_ids + end + + private + + def list_item_id + "#{super}_sidebar" + end + + def investment_title + investment.title + end + + def delete_path + budget_ballot_line_path(id: investment.id, investments_ids: investment_ids) + end +end diff --git a/app/views/budgets/ballot/_ballot.html.erb b/app/views/budgets/ballot/_ballot.html.erb index 8abf49a19..13d9eac88 100644 --- a/app/views/budgets/ballot/_ballot.html.erb +++ b/app/views/budgets/ballot/_ballot.html.erb @@ -40,8 +40,9 @@ <% end %> diff --git a/app/views/budgets/ballot/_investment.html.erb b/app/views/budgets/ballot/_investment.html.erb deleted file mode 100644 index 42fc8b44f..000000000 --- a/app/views/budgets/ballot/_investment.html.erb +++ /dev/null @@ -1,14 +0,0 @@ -
  • - <%= link_to investment.title, budget_investment_path(@budget, investment) %> - <%= investment.formatted_price %> - - <% if @budget.balloting? %> - <%= link_to budget_ballot_line_path(@budget, id: investment.id), - title: t("budgets.ballots.show.remove"), - class: "remove-investment-project", - method: :delete, - remote: true do %> - - <% end %> - <% end %> -
  • diff --git a/app/views/budgets/investments/_sidebar.html.erb b/app/views/budgets/investments/_sidebar.html.erb index 55bc46d66..233a455fa 100644 --- a/app/views/budgets/investments/_sidebar.html.erb +++ b/app/views/budgets/investments/_sidebar.html.erb @@ -59,11 +59,10 @@