diff --git a/app/models/widget/card.rb b/app/models/widget/card.rb index fa8b2f8e7..835defe6c 100644 --- a/app/models/widget/card.rb +++ b/app/models/widget/card.rb @@ -9,6 +9,7 @@ class Widget::Card < ApplicationRecord include Globalizable validates_translation :title, presence: true + validates :link_url, presence: true, if: -> { !header? || link_text.present? } def self.header where(header: true) diff --git a/app/views/shared/_header.html.erb b/app/views/shared/_header.html.erb index e3eeff304..06e0fbc54 100644 --- a/app/views/shared/_header.html.erb +++ b/app/views/shared/_header.html.erb @@ -1,13 +1,17 @@ <% if header.present? %> -
+
"> <%= header.label %>

<%= header.title %>

+

<%= header.description %>

-
"> - <%= link_to header.link_text, header.link_url, class: "button expanded large" %> -
+ + <% if header.link_text.present? && header.link_url.present? %> +
"> + <%= link_to header.link_text, header.link_url, class: "button expanded large" %> +
+ <% end %>
<% if header.image.present? %> diff --git a/db/dev_seeds/sdg.rb b/db/dev_seeds/sdg.rb index f7fe40a3a..8381f01fb 100644 --- a/db/dev_seeds/sdg.rb +++ b/db/dev_seeds/sdg.rb @@ -33,6 +33,7 @@ end section "Creating SDG homepage cards" do SDG::Phase.all.each do |phase| - Widget::Card.create!(cardable: phase, title: "#{phase.title} card") + Widget::Card.create!(cardable: phase, title: "#{phase.title} card", + link_text: "Link Text", link_url: "/any_path") end end diff --git a/spec/models/widget/card_spec.rb b/spec/models/widget/card_spec.rb index 2dea5b38e..bb530cd50 100644 --- a/spec/models/widget/card_spec.rb +++ b/spec/models/widget/card_spec.rb @@ -17,6 +17,38 @@ describe Widget::Card do it "is not valid without a title" do expect(build(:widget_card, title: "")).not_to be_valid end + + context "regular cards" do + it "is not valid without a link_url" do + card = build(:widget_card, header: false, link_url: nil) + + expect(card).not_to be_valid + end + end + + context "header cards" do + it "is valid with no link_url and no link_text" do + header = build(:widget_card, :header, link_text: nil, link_url: nil) + + expect(header).to be_valid + end + + it "is not valid with link_text and no link_url" do + header = build(:widget_card, :header, link_text: "link text", link_url: nil) + + expect(header).not_to be_valid + expect(header.errors.count).to be 1 + expect(header.errors[:link_url].count).to be 1 + expect(header.errors[:link_url].first).to eq "can't be blank" + end + + it "is valid if link_text and link_url are both provided" do + header = build(:widget_card, :header, link_text: "Text link", + link_url: "https://consulproject.org") + + expect(header).to be_valid + end + end end describe "#header" do diff --git a/spec/system/admin/widgets/cards_spec.rb b/spec/system/admin/widgets/cards_spec.rb index 1621d90a6..4f7477142 100644 --- a/spec/system/admin/widgets/cards_spec.rb +++ b/spec/system/admin/widgets/cards_spec.rb @@ -172,6 +172,7 @@ describe "Cards", :admin do href: admin_site_customization_page_widget_cards_path(custom_page)) fill_in "Title", with: "Card for a custom page" + fill_in "Link URL", with: "/any_path" click_button "Create card" expect(page).to have_current_path admin_site_customization_page_widget_cards_path(custom_page) diff --git a/spec/system/home_spec.rb b/spec/system/home_spec.rb index f1482e2d9..6a2a36456 100644 --- a/spec/system/home_spec.rb +++ b/spec/system/home_spec.rb @@ -174,4 +174,30 @@ describe "Home" do expect(page).not_to have_css(".title", text: "Featured") end + + describe "Header Card" do + scenario "if there is header card with link, the link content is rendered" do + create(:widget_card, :header, link_text: "Link text", link_url: "consul.dev") + + visit root_path + + expect(page).to have_link "Link text", href: "consul.dev" + end + + scenario "if there is header card without link, the link content is not rendered" do + create(:widget_card, :header, link_text: nil, link_url: nil) + + visit root_path + + within(".header-card") { expect(page).not_to have_link } + end + + scenario "if there is header card without link and with text, the link content is not rendered" do + create(:widget_card, :header, link_text: "", link_url: "", link_text_es: "Link ES", title_es: "ES") + + visit root_path(locale: :es) + + within(".header-card") { expect(page).not_to have_link } + end + end end diff --git a/spec/system/sdg_management/homepage_spec.rb b/spec/system/sdg_management/homepage_spec.rb index 6f8428b25..1fb339d5a 100644 --- a/spec/system/sdg_management/homepage_spec.rb +++ b/spec/system/sdg_management/homepage_spec.rb @@ -22,6 +22,7 @@ describe "SDG homepage configuration" do click_link "Create planning card" within(".translatable-fields") { fill_in "Title", with: "My planning card" } + fill_in "Link URL", with: "/any_path" click_button "Create card" within(".planning-cards") do @@ -54,6 +55,7 @@ describe "SDG homepage configuration" do click_link "Create header" within(".translatable-fields") { fill_in "Title", with: "My header" } + fill_in "Link URL", with: "/any_path" click_button "Create card" within(".sdg-header") do