Fix managing widget cards for homepage

The condition `params[:page_id] != 0` didn't work properly when editing
the homepage because in that case the parameter was `nil`, and the line
`SiteCustomization::Page.find(@card.site_customization_page_id)` raised
an exception because it couldn't find a page with a `nil` ID.

Fixing the issue while maintaining the check against `0` lead to complex
code, and so allowing `nil` in the database and assuming cards with no
`site_customization_page_id` belonged in the homepage seemed to be the
easiest solution.
This commit is contained in:
Javi Martín
2019-01-18 14:06:40 +01:00
committed by Manu
parent 9c050ca6bd
commit 2926e4e375
5 changed files with 12 additions and 11 deletions

View File

@@ -4,10 +4,8 @@ class Admin::Widget::CardsController < Admin::BaseController
def new
if header_card?
@card = ::Widget::Card.new(header: header_card?)
elsif params[:page_id] != 0
@card = ::Widget::Card.new(site_customization_page_id: params[:page_id])
else
@card = ::Widget::Card.new
@card = ::Widget::Card.new(site_customization_page_id: params[:page_id])
end
end
@@ -59,7 +57,7 @@ class Admin::Widget::CardsController < Admin::BaseController
def redirect_to_customization_page_cards_or_homepage
notice = t("admin.site_customization.pages.cards.#{params[:action]}.notice")
if params[:page_id] != 0
if @card.site_customization_page_id
redirect_to admin_site_customization_page_cards_path(page), notice: notice
else
redirect_to admin_homepage_url, notice: notice

View File

@@ -16,7 +16,7 @@ class Widget::Card < ActiveRecord::Base
end
def self.body
where(header: false, site_customization_page_id: 0).order(:created_at)
where(header: false, site_customization_page_id: nil).order(:created_at)
end
#add widget cards to custom pages

View File

@@ -1,5 +1,5 @@
class AddSiteCustomizationPageToWidgetCards < ActiveRecord::Migration
def change
add_reference :widget_cards, :site_customization_page, index: true, default: 0
add_reference :widget_cards, :site_customization_page, index: true
end
end

View File

@@ -1503,7 +1503,7 @@ ActiveRecord::Schema.define(version: 20181218164126) do
t.boolean "header", default: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "site_customization_page_id", default: 0
t.integer "site_customization_page_id"
end
add_index "widget_cards", ["site_customization_page_id"], name: "index_widget_cards_on_site_customization_page_id", using: :btree

View File

@@ -26,11 +26,14 @@ describe Widget::Card do
it "returns cards for the homepage body" do
header = create(:widget_card, header: true)
card1 = create(:widget_card, header: false, title: "Card 1", site_customization_page_id: 0)
card2 = create(:widget_card, header: false, title: "Card 2", site_customization_page_id: 0)
card3 = create(:widget_card, header: false, title: "Card 3", site_customization_page_id: 0)
card1 = create(:widget_card, header: false)
card2 = create(:widget_card, header: false)
page_card = create(:widget_card, header: false, page: create(:site_customization_page))
expect(Widget::Card.body).to eq([card1, card2, card3])
expect(Widget::Card.body).to include(card1)
expect(Widget::Card.body).to include(card2)
expect(Widget::Card.body).not_to include(header)
expect(Widget::Card.body).not_to include(page_card)
end
end