Add order field to widget cards

We will use this field to enter the position where the cards will be shown to the
user in the homepage.
This commit is contained in:
taitus
2023-10-10 08:33:03 +02:00
parent e9a7731f49
commit 9dd10cac19
4 changed files with 17 additions and 0 deletions

View File

@@ -10,6 +10,7 @@ class Widget::Card < ApplicationRecord
validates_translation :title, presence: true
validates :link_url, presence: true, if: -> { !header? || link_text.present? }
validates :order, numericality: { greater_than_or_equal_to: 1 }
def self.header
where(header: true)

View File

@@ -0,0 +1,5 @@
class AddOrderToWidgetCards < ActiveRecord::Migration[6.1]
def change
add_column :widget_cards, :order, :integer, null: false, default: 1
end
end

View File

@@ -1768,6 +1768,7 @@ ActiveRecord::Schema.define(version: 2023_10_12_141318) do
t.integer "cardable_id"
t.integer "columns", default: 4
t.string "cardable_type", default: "SiteCustomization::Page"
t.integer "order", default: 1, null: false
t.index ["cardable_id"], name: "index_widget_cards_on_cardable_id"
end

View File

@@ -18,6 +18,16 @@ describe Widget::Card do
expect(build(:widget_card, title: "")).not_to be_valid
end
describe "order" do
it "is not valid without an order" do
expect(build(:widget_card, order: nil)).not_to be_valid
end
it "is not valid with an order less than 1" do
expect(build(:widget_card, order: 0)).not_to be_valid
end
end
context "regular cards" do
it "is not valid without a link_url" do
card = build(:widget_card, header: false, link_url: nil)