Add link_url validation for cards when are header cards
Add link_url presence validation only when link_text is provided only for header cards. In this case it makes sense to allow creating a "header card" without link_url, since we can show the header without link text and without link url and it still does its function.
This commit is contained in:
@@ -9,7 +9,7 @@ class Widget::Card < ApplicationRecord
|
||||
include Globalizable
|
||||
|
||||
validates_translation :title, presence: true
|
||||
validates :link_url, presence: true, if: -> { !header? }
|
||||
validates :link_url, presence: true, if: -> { !header? || link_text.present? }
|
||||
|
||||
def self.header
|
||||
where(header: true)
|
||||
|
||||
@@ -25,6 +25,30 @@ describe Widget::Card do
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user