- <%= render "shared/banner" %>
+ <%= render Shared::BannerComponent.new("proposals") %>
<% if show_featured_proposals? %>
diff --git a/app/views/shared/_banner.html.erb b/app/views/shared/_banner.html.erb
deleted file mode 100644
index d426ee3dd..000000000
--- a/app/views/shared/_banner.html.erb
+++ /dev/null
@@ -1,6 +0,0 @@
-<% if has_banners? || controller.class == Admin::BannersController %>
- <% banner ||= @banners.sample %>
-
- <%= sanitize banner_target_link(banner), attributes: %w[href style] %>
-
-<% end %>
diff --git a/app/views/welcome/index.html.erb b/app/views/welcome/index.html.erb
index d6d37a4ee..8c2611397 100644
--- a/app/views/welcome/index.html.erb
+++ b/app/views/welcome/index.html.erb
@@ -4,7 +4,7 @@
<%= render "shared/canonical", href: root_url %>
<% end %>
-<%= render "shared/banner" %>
+<%= render Shared::BannerComponent.new("homepage") %>
<% provide :social_media_meta_tags do %>
<%= render "shared/social_media_meta_tags",
diff --git a/config/locales/en/admin.yml b/config/locales/en/admin.yml
index 81348ecf6..44a41d71b 100644
--- a/config/locales/en/admin.yml
+++ b/config/locales/en/admin.yml
@@ -35,6 +35,7 @@ en:
proposals: Proposals
budgets: Participatory budgeting
help_page: Help page
+ sdg: SDG
edit:
editing: Edit banner
form:
diff --git a/config/locales/es/admin.yml b/config/locales/es/admin.yml
index 5e54a3f0f..6ef2d0af4 100644
--- a/config/locales/es/admin.yml
+++ b/config/locales/es/admin.yml
@@ -35,6 +35,7 @@ es:
proposals: Propuestas
budgets: Presupuestos participativos
help_page: Página de ayuda
+ sdg: ODS
edit:
editing: Editar el banner
form:
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..766902e4b
--- /dev/null
+++ b/db/web_sections.rb
@@ -0,0 +1,3 @@
+%w[homepage debates proposals budgets help_page sdg].each do |section|
+ WebSection.where(name: section).first_or_create!
+end
diff --git a/lib/tasks/db.rake b/lib/tasks/db.rake
index 622f9e9c5..fdbffcd97 100644
--- a/lib/tasks/db.rake
+++ b/lib/tasks/db.rake
@@ -9,6 +9,7 @@ namespace :db do
task load_sdg: :environment do
ApplicationLogger.new.info "Adding Sustainable Development Goals content"
load(Rails.root.join("db", "sdg.rb"))
+ WebSection.where(name: "sdg").first_or_create!
end
desc "Calculates the TSV column for all polls and legislation processes"
diff --git a/spec/components/shared/banner_component_spec.rb b/spec/components/shared/banner_component_spec.rb
new file mode 100644
index 000000000..9c0ccda27
--- /dev/null
+++ b/spec/components/shared/banner_component_spec.rb
@@ -0,0 +1,79 @@
+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 "renders given a section" do
+ create(:banner, web_sections: [WebSection.find_by!(name: "debates")], title: "Debate banner")
+ create(:banner, web_sections: [WebSection.find_by!(name: "proposals")], title: "Proposal banner")
+
+ render_inline Shared::BannerComponent.new("debates")
+
+ expect(page.find(".banner")).to have_content "Debate banner"
+ expect(page).not_to have_content "Proposal banner"
+ end
+
+ context "several banners available in the same section" do
+ before do
+ create(:banner,
+ web_sections: [WebSection.find_by!(name: "debates")],
+ title: "First banner",
+ description: "First description",
+ target_url: "/first_target"
+ )
+
+ create(:banner,
+ web_sections: [WebSection.find_by!(name: "debates")],
+ title: "Second banner",
+ description: "Second description",
+ target_url: "/second_target"
+ )
+ end
+
+ it "only renders one banner" do
+ render_inline Shared::BannerComponent.new("debates")
+
+ expect(page).to have_css ".banner", count: 1
+ end
+
+ it "consistently renders one banner" do
+ render_inline Shared::BannerComponent.new("debates")
+
+ if page.has_content?("First banner")
+ expect(page).to have_content "First description"
+ expect(page).to have_css "[href='/first_target']"
+ expect(page).not_to have_content "Second banner"
+ else
+ expect(page).to have_content "Second description"
+ expect(page).to have_css "[href='/second_target']"
+ end
+ end
+
+ it "only renders active banners" do
+ Banner.first.update!(post_ended_at: 1.day.ago)
+
+ render_inline Shared::BannerComponent.new("debates")
+
+ expect(page).to have_content "Second banner"
+ expect(page).not_to have_content "First banner"
+ end
+
+ it "does not render anything with no active banners" do
+ Banner.all.each { |banner| banner.update!(post_ended_at: 1.day.ago) }
+
+ render_inline Shared::BannerComponent.new("debates")
+
+ expect(page).not_to have_css ".banner"
+ end
+ end
+
+ it "does not render anything given nil" do
+ render_inline Shared::BannerComponent.new(nil)
+
+ expect(page).not_to have_css ".banner"
+ end
+end