Make widget cards polymorphic

So now we'll be able to add them to other sections.

We're also adding a `dependent: :destroy` relation to models having
cards since it doesn't make sense to have cards around when their page
has been destroyed.
This commit is contained in:
Javi Martín
2021-01-07 18:48:09 +01:00
parent c66a5a30ef
commit ee29ca43a5
10 changed files with 33 additions and 24 deletions

View File

@@ -0,0 +1,7 @@
module Cardable
extend ActiveSupport::Concern
included do
has_many :cards, class_name: "Widget::Card", as: :cardable, dependent: :destroy
end
end

View File

@@ -1,10 +1,6 @@
class SiteCustomization::Page < ApplicationRecord class SiteCustomization::Page < ApplicationRecord
VALID_STATUSES = %w[draft published].freeze VALID_STATUSES = %w[draft published].freeze
has_many :cards, include Cardable
class_name: "Widget::Card",
foreign_key: "site_customization_page_id",
inverse_of: :page
translates :title, touch: true translates :title, touch: true
translates :subtitle, touch: true translates :subtitle, touch: true
translates :content, touch: true translates :content, touch: true

View File

@@ -1,9 +1,6 @@
class Widget::Card < ApplicationRecord class Widget::Card < ApplicationRecord
include Imageable include Imageable
belongs_to :page, belongs_to :cardable, polymorphic: true
class_name: "SiteCustomization::Page",
foreign_key: "site_customization_page_id",
inverse_of: :cards
translates :label, touch: true translates :label, touch: true
translates :title, touch: true translates :title, touch: true
@@ -18,6 +15,6 @@ class Widget::Card < ApplicationRecord
end end
def self.body def self.body
where(header: false, site_customization_page_id: nil).order(:created_at) where(header: false, cardable_id: nil).order(:created_at)
end end
end end

View File

@@ -271,7 +271,7 @@ resolve "Audit" do |audit|
end end
resolve "Widget::Card" do |card, options| resolve "Widget::Card" do |card, options|
[*resource_hierarchy_for(card.page), card] [*resource_hierarchy_for(card.cardable), card]
end end
resolve "Budget::Group" do |group, options| resolve "Budget::Group" do |group, options|

View File

@@ -0,0 +1,8 @@
class MakeCardsPolymorphic < ActiveRecord::Migration[5.2]
def change
change_table :widget_cards do |t|
t.rename :site_customization_page_id, :cardable_id
t.string :cardable_type, default: "SiteCustomization::Page"
end
end
end

View File

@@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2020_12_16_132642) do ActiveRecord::Schema.define(version: 2021_01_06_132909) do
# These are extensions that must be enabled in order to support this database # These are extensions that must be enabled in order to support this database
enable_extension "pg_trgm" enable_extension "pg_trgm"
@@ -1647,9 +1647,10 @@ ActiveRecord::Schema.define(version: 2020_12_16_132642) do
t.boolean "header", default: false t.boolean "header", default: false
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
t.integer "site_customization_page_id" t.integer "cardable_id"
t.integer "columns", default: 4 t.integer "columns", default: 4
t.index ["site_customization_page_id"], name: "index_widget_cards_on_site_customization_page_id" t.string "cardable_type", default: "SiteCustomization::Page"
t.index ["cardable_id"], name: "index_widget_cards_on_cardable_id"
end end
create_table "widget_feeds", id: :serial, force: :cascade do |t| create_table "widget_feeds", id: :serial, force: :cascade do |t|

View File

@@ -34,7 +34,7 @@ describe Widget::Card do
header = create(:widget_card, header: true) header = create(:widget_card, header: true)
card1 = create(:widget_card, header: false) card1 = create(:widget_card, header: false)
card2 = create(:widget_card, header: false) card2 = create(:widget_card, header: false)
page_card = create(:widget_card, header: false, page: create(:site_customization_page)) page_card = create(:widget_card, header: false, cardable: create(:site_customization_page))
expect(Widget::Card.body).to match_array [card1, card2] expect(Widget::Card.body).to match_array [card1, card2]
expect(Widget::Card.body).not_to include(header) expect(Widget::Card.body).not_to include(header)

View File

@@ -172,7 +172,7 @@ describe "Polymorphic routes" do
it "routes site customization page widget cards" do it "routes site customization page widget cards" do
page = create(:site_customization_page) page = create(:site_customization_page)
card = create(:widget_card, page: page) card = create(:widget_card, cardable: page)
expect(admin_polymorphic_path(card)).to eq admin_site_customization_page_widget_card_path(page, card) expect(admin_polymorphic_path(card)).to eq admin_site_customization_page_widget_card_path(page, card)
end end

View File

@@ -175,9 +175,9 @@ describe "Cards", :admin do
end end
scenario "Show" do scenario "Show" do
card_1 = create(:widget_card, page: custom_page, title: "Card large", columns: 8) card_1 = create(:widget_card, cardable: custom_page, title: "Card large", columns: 8)
card_2 = create(:widget_card, page: custom_page, title: "Card medium", columns: 4) card_2 = create(:widget_card, cardable: custom_page, title: "Card medium", columns: 4)
card_3 = create(:widget_card, page: custom_page, title: "Card small", columns: 2) card_3 = create(:widget_card, cardable: custom_page, title: "Card small", columns: 2)
visit custom_page.url visit custom_page.url
@@ -189,8 +189,8 @@ describe "Cards", :admin do
end end
scenario "Show label only if it is present" do scenario "Show label only if it is present" do
card_1 = create(:widget_card, page: custom_page, title: "Card one", label: "My label") card_1 = create(:widget_card, cardable: custom_page, title: "Card one", label: "My label")
card_2 = create(:widget_card, page: custom_page, title: "Card two") card_2 = create(:widget_card, cardable: custom_page, title: "Card two")
visit custom_page.url visit custom_page.url
@@ -204,7 +204,7 @@ describe "Cards", :admin do
end end
scenario "Edit", :js do scenario "Edit", :js do
create(:widget_card, page: custom_page, title: "Original title") create(:widget_card, cardable: custom_page, title: "Original title")
visit admin_site_customization_page_widget_cards_path(custom_page) visit admin_site_customization_page_widget_cards_path(custom_page)
@@ -227,7 +227,7 @@ describe "Cards", :admin do
end end
scenario "Destroy", :js do scenario "Destroy", :js do
create(:widget_card, page: custom_page, title: "Card title") create(:widget_card, cardable: custom_page, title: "Card title")
visit admin_site_customization_page_widget_cards_path(custom_page) visit admin_site_customization_page_widget_cards_path(custom_page)

View File

@@ -104,7 +104,7 @@ describe "Custom Pages" do
scenario "Show widget cards for that page" do scenario "Show widget cards for that page" do
custom_page = create(:site_customization_page, :published) custom_page = create(:site_customization_page, :published)
create(:widget_card, page: custom_page, title: "Card Highlights") create(:widget_card, cardable: custom_page, title: "Card Highlights")
visit custom_page.url visit custom_page.url