Add link_url validation for cards when are not header cards

Currently it is not necessary to include the link_url field.

When we display these cards without link_url, they create an empty link that
redirects to the same page. I understand that this is not a desired behavior, so I
think it is better to add a validation in this case and force administrators to add a
link_url when creating a card.
This commit is contained in:
taitus
2021-06-25 13:07:59 +02:00
parent d2876f5565
commit 1d47a5c079
5 changed files with 14 additions and 1 deletions

View File

@@ -9,6 +9,7 @@ class Widget::Card < ApplicationRecord
include Globalizable
validates_translation :title, presence: true
validates :link_url, presence: true, if: -> { !header? }
def self.header
where(header: true)

View File

@@ -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

View File

@@ -17,6 +17,14 @@ 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
end
describe "#header" do

View File

@@ -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)

View File

@@ -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