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.
28 lines
702 B
Ruby
28 lines
702 B
Ruby
class Widget::Card < ActiveRecord::Base
|
|
include Imageable
|
|
belongs_to :page, class_name: 'SiteCustomization::Page', foreign_key: 'site_customization_page_id'
|
|
|
|
# table_name must be set before calls to 'translates'
|
|
self.table_name = "widget_cards"
|
|
|
|
translates :label, touch: true
|
|
translates :title, touch: true
|
|
translates :description, touch: true
|
|
translates :link_text, touch: true
|
|
include Globalizable
|
|
|
|
def self.header
|
|
where(header: true)
|
|
end
|
|
|
|
def self.body
|
|
where(header: false, site_customization_page_id: nil).order(:created_at)
|
|
end
|
|
|
|
#add widget cards to custom pages
|
|
def self.page(page_id)
|
|
where(site_customization_page_id: page_id)
|
|
end
|
|
|
|
end
|