-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathpage.rb
82 lines (67 loc) · 1.93 KB
/
page.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
class Page < ApplicationRecord
TEMPLATES = {
'splash' => { hide_header: true, hide_footer: true },
'home' => { },
}
TAILWIND = 'tailwind'.freeze
belongs_to :website
after_save_commit :purge_website_cache
has_many :contents, class_name: 'Website::Content', as: :contentable, dependent: :destroy
scope :published, -> { where.not(published_body: nil).where(hide_page: false) }
scope :in_footer, -> { published.where.not(footer_category: [nil, ""]) }
validates :name, :slug, presence: true
validates :slug, uniqueness: { scope: :website_id }
attr_accessor :template
BLANK_SLUG = "0"
def published?
published_body.present? && !hide_page
end
def to_param
persisted? ? slug : BLANK_SLUG
end
def self.promote(page)
transaction do
page.website.pages.update(landing: false)
page.update(landing: !page.landing)
end
end
def self.from_template(key, attrs)
new(TEMPLATES[key].merge(template: key, name: key.titleize, slug: key, **attrs))
end
def unpublished_changes?
published_body != unpublished_body
end
def tailwind_css
contents.where(name: TAILWIND).pluck(:html).first
end
private
def purge_website_cache
website.save
end
end
# == Schema Information
#
# Table name: pages
#
# id :bigint(8) not null, primary key
# name :string
# slug :string
# website_id :bigint(8)
# published_body :text
# unpublished_body :text
# created_at :datetime not null
# updated_at :datetime not null
# landing :boolean default(FALSE), not null
# hide_header :boolean default(FALSE), not null
# hide_footer :boolean default(FALSE), not null
# hide_page :boolean default(FALSE), not null
# footer_category :string
#
# Indexes
#
# index_pages_on_website_id (website_id)
#
# Foreign Keys
#
# fk_rails_... (website_id => websites.id)
#