From 68b32f2a84a8e1521114c216e2c0a9c11f9abee4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Thu, 14 Jan 2021 14:45:01 +0100 Subject: [PATCH 1/6] Remove duplication creating web sections in seeds --- db/dev_seeds/banners.rb | 8 +------- db/seeds.rb | 7 +------ db/web_sections.rb | 5 +++++ 3 files changed, 7 insertions(+), 13 deletions(-) create mode 100644 db/web_sections.rb diff --git a/db/dev_seeds/banners.rb b/db/dev_seeds/banners.rb index 1b208fa48..61cfbb00e 100644 --- a/db/dev_seeds/banners.rb +++ b/db/dev_seeds/banners.rb @@ -19,10 +19,4 @@ section "Creating banners" do end end -section "Creating web sections" do - WebSection.create!(name: "homepage") - WebSection.create!(name: "debates") - WebSection.create!(name: "proposals") - WebSection.create!(name: "budgets") - WebSection.create!(name: "help_page") -end +load Rails.root.join("db", "web_sections.rb") diff --git a/db/seeds.rb b/db/seeds.rb index dba4e37cb..28704c29d 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -7,12 +7,7 @@ if Administrator.count == 0 && !Rails.env.test? end Setting.reset_defaults - -WebSection.where(name: "homepage").first_or_create! -WebSection.where(name: "debates").first_or_create! -WebSection.where(name: "proposals").first_or_create! -WebSection.where(name: "budgets").first_or_create! -WebSection.where(name: "help_page").first_or_create! +load Rails.root.join("db", "web_sections.rb") # Default custom pages load Rails.root.join("db", "pages.rb") diff --git a/db/web_sections.rb b/db/web_sections.rb new file mode 100644 index 000000000..57b8a915a --- /dev/null +++ b/db/web_sections.rb @@ -0,0 +1,5 @@ +WebSection.where(name: "homepage").first_or_create! +WebSection.where(name: "debates").first_or_create! +WebSection.where(name: "proposals").first_or_create! +WebSection.where(name: "budgets").first_or_create! +WebSection.where(name: "help_page").first_or_create! From 885a9ad7657eae230d41a2c48549f58d815be354 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Thu, 14 Jan 2021 14:51:31 +0100 Subject: [PATCH 2/6] Simplify creating web sections in seeds --- db/web_sections.rb | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/db/web_sections.rb b/db/web_sections.rb index 57b8a915a..9683621e9 100644 --- a/db/web_sections.rb +++ b/db/web_sections.rb @@ -1,5 +1,3 @@ -WebSection.where(name: "homepage").first_or_create! -WebSection.where(name: "debates").first_or_create! -WebSection.where(name: "proposals").first_or_create! -WebSection.where(name: "budgets").first_or_create! -WebSection.where(name: "help_page").first_or_create! +%w[homepage debates proposals budgets help_page].each do |section| + WebSection.where(name: section).first_or_create! +end From 957a73f837b34f9f621e98f60887bb2663d9e397 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Thu, 14 Jan 2021 15:19:41 +0100 Subject: [PATCH 3/6] Extract banner to a component --- app/components/shared/banner_component.html.erb | 3 +++ app/components/shared/banner_component.rb | 16 ++++++++++++++++ app/helpers/banners_helper.rb | 7 ------- app/views/shared/_banner.html.erb | 5 ++--- 4 files changed, 21 insertions(+), 10 deletions(-) create mode 100644 app/components/shared/banner_component.html.erb create mode 100644 app/components/shared/banner_component.rb diff --git a/app/components/shared/banner_component.html.erb b/app/components/shared/banner_component.html.erb new file mode 100644 index 000000000..cbea18d0e --- /dev/null +++ b/app/components/shared/banner_component.html.erb @@ -0,0 +1,3 @@ + diff --git a/app/components/shared/banner_component.rb b/app/components/shared/banner_component.rb new file mode 100644 index 000000000..fcbb5a727 --- /dev/null +++ b/app/components/shared/banner_component.rb @@ -0,0 +1,16 @@ +class Shared::BannerComponent < ApplicationComponent + attr_reader :banner + + def initialize(banner) + @banner = banner + end + + private + + def link + link_to banner.target_url do + tag.h2(banner.title, style: "color:#{banner.font_color}") + + tag.h3(banner.description, style: "color:#{banner.font_color}") + end + end +end diff --git a/app/helpers/banners_helper.rb b/app/helpers/banners_helper.rb index 42cdb240d..973f353af 100644 --- a/app/helpers/banners_helper.rb +++ b/app/helpers/banners_helper.rb @@ -2,11 +2,4 @@ module BannersHelper def has_banners? @banners.present? && @banners.count > 0 end - - def banner_target_link(banner) - link_to banner.target_url do - tag.h2(banner.title, style: "color:#{banner.font_color}") + - tag.h3(banner.description, style: "color:#{banner.font_color}") - end - end end diff --git a/app/views/shared/_banner.html.erb b/app/views/shared/_banner.html.erb index d426ee3dd..358b6e2f3 100644 --- a/app/views/shared/_banner.html.erb +++ b/app/views/shared/_banner.html.erb @@ -1,6 +1,5 @@ <% if has_banners? || controller.class == Admin::BannersController %> <% banner ||= @banners.sample %> - + + <%= render Shared::BannerComponent.new(banner) %> <% end %> From c3147a1bb6c6cc863921567bee4e9b0dee345354 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Thu, 14 Jan 2021 15:22:20 +0100 Subject: [PATCH 4/6] Simplify conditions to render a banner --- app/components/shared/banner_component.html.erb | 8 +++++--- app/helpers/banners_helper.rb | 5 ----- app/views/shared/_banner.html.erb | 6 ++---- spec/components/shared/banner_component_spec.rb | 15 +++++++++++++++ 4 files changed, 22 insertions(+), 12 deletions(-) delete mode 100644 app/helpers/banners_helper.rb create mode 100644 spec/components/shared/banner_component_spec.rb diff --git a/app/components/shared/banner_component.html.erb b/app/components/shared/banner_component.html.erb index cbea18d0e..fe020302a 100644 --- a/app/components/shared/banner_component.html.erb +++ b/app/components/shared/banner_component.html.erb @@ -1,3 +1,5 @@ - +<% if banner %> + +<% end %> diff --git a/app/helpers/banners_helper.rb b/app/helpers/banners_helper.rb deleted file mode 100644 index 973f353af..000000000 --- a/app/helpers/banners_helper.rb +++ /dev/null @@ -1,5 +0,0 @@ -module BannersHelper - def has_banners? - @banners.present? && @banners.count > 0 - end -end diff --git a/app/views/shared/_banner.html.erb b/app/views/shared/_banner.html.erb index 358b6e2f3..c98b1185d 100644 --- a/app/views/shared/_banner.html.erb +++ b/app/views/shared/_banner.html.erb @@ -1,5 +1,3 @@ -<% if has_banners? || controller.class == Admin::BannersController %> - <% banner ||= @banners.sample %> +<% banner ||= @banners&.sample %> - <%= render Shared::BannerComponent.new(banner) %> -<% end %> +<%= render Shared::BannerComponent.new(banner) %> diff --git a/spec/components/shared/banner_component_spec.rb b/spec/components/shared/banner_component_spec.rb new file mode 100644 index 000000000..77168e63a --- /dev/null +++ b/spec/components/shared/banner_component_spec.rb @@ -0,0 +1,15 @@ +require "rails_helper" + +describe Shared::BannerComponent, type: :component do + it "renders given a banner" do + render_inline Shared::BannerComponent.new(create(:banner, title: "Vote now!")) + + expect(page.find(".banner")).to have_content "Vote now!" + end + + it "does not render anything given nil" do + render_inline Shared::BannerComponent.new(nil) + + expect(page).not_to have_css ".banner" + end +end From a7bbdb1bd09bbf1c424dbe891973b388888fcff8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Thu, 14 Jan 2021 16:06:34 +0100 Subject: [PATCH 5/6] Simplify rendering a banner Now the banner component accepts either a banner or a section and loads the banner if it's a section, so we don't have to add the `@banners` variable in several controllers. --- app/components/shared/banner_component.rb | 14 +++- app/controllers/budgets_controller.rb | 1 - .../concerns/commentable_actions.rb | 10 --- app/controllers/pages_controller.rb | 1 - app/controllers/welcome_controller.rb | 1 - app/views/admin/banners/_form.html.erb | 2 +- app/views/admin/banners/index.html.erb | 2 +- app/views/budgets/index.html.erb | 2 +- app/views/debates/index.html.erb | 2 +- app/views/pages/help/index.html.erb | 2 +- app/views/proposals/index.html.erb | 2 +- app/views/shared/_banner.html.erb | 3 - app/views/welcome/index.html.erb | 2 +- .../shared/banner_component_spec.rb | 64 +++++++++++++++++++ 14 files changed, 82 insertions(+), 26 deletions(-) delete mode 100644 app/views/shared/_banner.html.erb diff --git a/app/components/shared/banner_component.rb b/app/components/shared/banner_component.rb index fcbb5a727..de61e1602 100644 --- a/app/components/shared/banner_component.rb +++ b/app/components/shared/banner_component.rb @@ -1,8 +1,16 @@ class Shared::BannerComponent < ApplicationComponent - attr_reader :banner + attr_reader :banner_or_section - def initialize(banner) - @banner = banner + def initialize(banner_or_section) + @banner_or_section = banner_or_section + end + + def banner + @banner ||= if banner_or_section.respond_to?(:sections) + banner_or_section + else + Banner.in_section(banner_or_section).with_active.sample + end end private diff --git a/app/controllers/budgets_controller.rb b/app/controllers/budgets_controller.rb index 9fb0b0bf6..ec7e93f57 100644 --- a/app/controllers/budgets_controller.rb +++ b/app/controllers/budgets_controller.rb @@ -17,7 +17,6 @@ class BudgetsController < ApplicationController def index @finished_budgets = @budgets.finished.order(created_at: :desc) @budgets_coordinates = current_budget_map_locations - @banners = Banner.in_section("budgets").with_active end private diff --git a/app/controllers/concerns/commentable_actions.rb b/app/controllers/concerns/commentable_actions.rb index 8b2a6d3bc..5523285bf 100644 --- a/app/controllers/concerns/commentable_actions.rb +++ b/app/controllers/concerns/commentable_actions.rb @@ -16,7 +16,6 @@ module CommentableActions index_customization @tag_cloud = tag_cloud - @banners = Banner.in_section(section(resource_model.name)).with_active set_resource_votes(@resources) @@ -110,15 +109,6 @@ module CommentableActions nil end - def section(resource_name) - case resource_name - when "Proposal" - "proposals" - when "Debate" - "debates" - end - end - def featured_proposals @featured_proposals ||= [] end diff --git a/app/controllers/pages_controller.rb b/app/controllers/pages_controller.rb index a4b01cf09..03bb40828 100644 --- a/app/controllers/pages_controller.rb +++ b/app/controllers/pages_controller.rb @@ -6,7 +6,6 @@ class PagesController < ApplicationController def show @custom_page = SiteCustomization::Page.published.find_by(slug: params[:id]) - @banners = Banner.in_section("help_page").with_active if @custom_page.present? @cards = @custom_page.cards diff --git a/app/controllers/welcome_controller.rb b/app/controllers/welcome_controller.rb index 4e3081e73..9fa9bf1f2 100644 --- a/app/controllers/welcome_controller.rb +++ b/app/controllers/welcome_controller.rb @@ -11,7 +11,6 @@ class WelcomeController < ApplicationController @header = Widget::Card.header.first @feeds = Widget::Feed.active @cards = Widget::Card.body - @banners = Banner.in_section("homepage").with_active @remote_translations = detect_remote_translations(@feeds, @recommended_debates, @recommended_proposals) diff --git a/app/views/admin/banners/_form.html.erb b/app/views/admin/banners/_form.html.erb index 70e55079e..8737b760b 100644 --- a/app/views/admin/banners/_form.html.erb +++ b/app/views/admin/banners/_form.html.erb @@ -79,6 +79,6 @@
- <%= render "shared/banner", banner: @banner %> + <%= render Shared::BannerComponent.new(@banner) %>
<% end %> diff --git a/app/views/admin/banners/index.html.erb b/app/views/admin/banners/index.html.erb index 67b151d86..bd71afb5f 100644 --- a/app/views/admin/banners/index.html.erb +++ b/app/views/admin/banners/index.html.erb @@ -30,7 +30,7 @@ <%= t("admin.banners.index.preview") %> - <%= render "shared/banner", banner: banner %> + <%= render Shared::BannerComponent.new(banner) %> diff --git a/app/views/budgets/index.html.erb b/app/views/budgets/index.html.erb index 1beeb1179..4121e817d 100644 --- a/app/views/budgets/index.html.erb +++ b/app/views/budgets/index.html.erb @@ -1,4 +1,4 @@ -<%= render "shared/banner" %> +<%= render Shared::BannerComponent.new("budgets") %> <% provide :title do %><%= t("budgets.index.title") %><% end %> diff --git a/app/views/debates/index.html.erb b/app/views/debates/index.html.erb index 0d9651360..24142020b 100644 --- a/app/views/debates/index.html.erb +++ b/app/views/debates/index.html.erb @@ -40,7 +40,7 @@
- <%= render "shared/banner" %> + <%= render Shared::BannerComponent.new("debates") %> <% unless @search_terms || !has_featured? %> <%= render "featured_debates" %> diff --git a/app/views/pages/help/index.html.erb b/app/views/pages/help/index.html.erb index 505ff2196..34f441e69 100644 --- a/app/views/pages/help/index.html.erb +++ b/app/views/pages/help/index.html.erb @@ -3,7 +3,7 @@ <%= render "shared/canonical", href: help_url %> <% end %> -<%= render "shared/banner" %> +<%= render Shared::BannerComponent.new("help_page") %>
diff --git a/app/views/proposals/index.html.erb b/app/views/proposals/index.html.erb index c28790495..8ec111379 100644 --- a/app/views/proposals/index.html.erb +++ b/app/views/proposals/index.html.erb @@ -49,7 +49,7 @@
- <%= render "shared/banner" %> + <%= render Shared::BannerComponent.new("proposals") %> <% if show_featured_proposals? %>