Not doing so has a few gotchas when working with relations, particularly with records which are not stored in the database. I'm excluding the related content file because it's got a very peculiar relationship with itself: the `has_one :opposite_related_content` has no inverse; the relation itself is its inverse. It's a false positive since the inverse condition is true: ``` content.opposite_related_content.opposite_related_content.object_id == content.object_id ```
30 lines
1.1 KiB
Ruby
30 lines
1.1 KiB
Ruby
class SiteCustomization::Page < ApplicationRecord
|
|
VALID_STATUSES = %w[draft published]
|
|
has_many :cards,
|
|
class_name: "Widget::Card",
|
|
foreign_key: "site_customization_page_id",
|
|
inverse_of: :page
|
|
|
|
translates :title, touch: true
|
|
translates :subtitle, touch: true
|
|
translates :content, touch: true
|
|
include Globalizable
|
|
|
|
validates_translation :title, presence: true
|
|
validates :slug, presence: true,
|
|
uniqueness: { case_sensitive: false },
|
|
format: { with: /\A[0-9a-zA-Z\-_]*\Z/, message: :slug_format }
|
|
validates :status, presence: true, inclusion: { in: VALID_STATUSES }
|
|
|
|
scope :published, -> { where(status: "published").sort_desc }
|
|
scope :sort_asc, -> { order("id ASC") }
|
|
scope :sort_desc, -> { order("id DESC") }
|
|
scope :with_more_info_flag, -> { where(status: "published", more_info_flag: true).sort_asc }
|
|
scope :with_same_locale, -> { joins(:translations).locale }
|
|
scope :locale, -> { where("site_customization_page_translations.locale": I18n.locale) }
|
|
|
|
def url
|
|
"/#{slug}"
|
|
end
|
|
end
|