Make card title mandatory

We didn't add any validation rules to the card model. At the very least,
the title should be mandatory.

The fact that the label field is marked as optional in the form but the
other fields are not probably means description and link should be
mandatory as well. However, since there might be institutions using
cards with descriptions but no link or cards with links but no
description, so we're keeping these fields optional for compatibility
reasons. We might change our minds in the future, though.
This commit is contained in:
Javi Martín
2021-01-06 14:12:38 +01:00
parent 36037bbc55
commit c1c84507b8
5 changed files with 30 additions and 2 deletions

View File

@@ -14,6 +14,8 @@ class Widget::Card < ApplicationRecord
translates :link_text, touch: true
include Globalizable
validates_translation :title, presence: true
def self.header
where(header: true)
end

View File

@@ -1,6 +1,7 @@
<%= render "shared/globalize_locales", resource: @card %>
<%= translatable_form_for [:admin, @card] do |f| %>
<%= render "shared/errors", resource: @card %>
<div class="row">
<%= f.translatable_fields do |translations_form| %>
@@ -49,7 +50,10 @@
</div>
</div>
<div class="column">
<%= f.submit(t("admin.homepage.#{action_name}.#{@card.header? ? "submit_header" : "submit_card"}"), class: "button success") %>
<%= f.submit(
t("admin.homepage.#{admin_submit_action(@card)}.#{@card.header? ? "submit_header" : "submit_card"}"),
class: "button success"
) %>
</div>
</div>
<% end %>

View File

@@ -9,6 +9,10 @@ describe Widget::Card do
it "is valid" do
expect(card).to be_valid
end
it "is not valid without a title" do
expect(build(:widget_card, title: "")).not_to be_valid
end
end
describe "#header" do

View File

@@ -56,7 +56,7 @@ module Notifications
def error_message(resource_model = nil)
resource_model ||= "(.*)"
field_check_message = "Please check the marked fields to know how to correct them:"
/\d errors? prevented this #{resource_model} from being saved. #{field_check_message}/
/\d errors? prevented this #{resource_model} from being saved.(\n| )#{field_check_message}/
end
def fill_in_admin_notification_form(options = {})

View File

@@ -27,6 +27,15 @@ describe "Cards", :admin do
end
end
scenario "Create with errors", :js do
visit admin_homepage_path
click_link "Create card"
click_button "Create card"
expect(page).to have_text error_message
expect(page).to have_button "Create card"
end
scenario "Index" do
3.times { create(:widget_card) }
@@ -130,6 +139,15 @@ describe "Cards", :admin do
end
end
scenario "Create with errors", :js do
visit admin_homepage_path
click_link "Create header"
click_button "Create header"
expect(page).to have_text error_message
expect(page).to have_button "Create header"
end
context "Page card" do
let!(:custom_page) { create(:site_customization_page, :published) }