This repository has been archived by the owner on Mar 27, 2023. It is now read-only.
/
page.rb
145 lines (121 loc) · 4.27 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# frozen_string_literal: true
# == Schema Information
#
# Table name: pages
#
# id :integer not null, primary key
# language_id :integer
# campaign_id :integer
# title :string not null
# slug :string not null
# created_at :datetime
# updated_at :datetime
# compiled_html :text
# status :string default("pending")
# messages :text
# content :text default("")
# featured :boolean default(FALSE)
# liquid_layout_id :integer
# follow_up_liquid_layout_id :integer
# action_count :integer default(0)
# primary_image_id :integer
# ak_petition_resource_uri :string
# ak_donation_resource_uri :string
# follow_up_plan :integer default(0), not null
# follow_up_page_id :integer
# javascript :text
# publish_status :integer default(1), not null
# optimizely_status :integer default(0), not null
# canonical_url :string
# allow_duplicate_actions :boolean default(FALSE)
#
class Page < ActiveRecord::Base
extend FriendlyId
has_paper_trail
enum follow_up_plan: [:with_liquid, :with_page] # TODO: - :with_link
enum publish_status: [:published, :unpublished, :archived]
enum optimizely_status: [:optimizely_enabled, :optimizely_disabled]
belongs_to :language
belongs_to :campaign # Note that some pages do not necessarily belong to campaigns
belongs_to :liquid_layout
belongs_to :follow_up_page, class_name: 'Page'
belongs_to :follow_up_liquid_layout, class_name: 'LiquidLayout'
belongs_to :primary_image, class_name: 'Image'
has_many :tags, through: :pages_tags
has_many :actions
has_many :pages_tags, dependent: :destroy
has_many :images, dependent: :destroy
has_many :links, dependent: :destroy
scope :language, -> (code) { code ? joins(:language).where(languages: { code: code }) : all }
scope :featured, -> { where(featured: true) }
validates :title, presence: true
validates :liquid_layout, presence: true
validates :publish_status, presence: true
validate :primary_image_is_owned
validates :canonical_url, allow_blank: true, format: { with: %r{\Ahttps{0,1}:\/\/.+\..+\z} }
after_save :switch_plugins
friendly_id :slug_candidates, use: [:finders, :slugged]
def liquid_data
attributes.merge(link_list: links.map(&:attributes))
end
def plugins
Plugins.registered.map do |plugin_class|
plugin_class.where(page_id: id).to_a
end.flatten.sort_by(&:created_at)
end
def plugin_names
plugins.map { |plugin| plugin.model_name.name.split('::')[1].downcase }
end
def tag_names
tags.map { |tag| tag.name.downcase }
end
def shares
[Share::Facebook, Share::Twitter, Share::Email].inject([]) do |variations, share_class|
variations += share_class.where(page_id: id)
end
end
def image_to_display
primary_image || images.first
end
def meta_tags
tag_names << plugin_names
end
def dup
clone = super
clone.assign_attributes(
primary_image: nil,
slug: nil,
action_count: 0
)
clone
end
def number_of_pages_with_matching_title
Page.where(title: title).count
end
def slug_candidates
[
:title,
[:title, :number_of_pages_with_matching_title]
]
end
def campaign_action_count
@campaign_action_count ||= if campaign
campaign.action_count
else
action_count
end
end
private
def switch_plugins
fields = %w(liquid_layout_id follow_up_liquid_layout_id follow_up_plan)
if fields.any? { |f| changed.include?(f) }
secondary = (follow_up_plan == 'with_liquid') ? follow_up_liquid_layout : nil
PagePluginSwitcher.new(self).switch(liquid_layout, secondary)
end
end
def primary_image_is_owned
unless primary_image_id.blank? || images.map(&:id).include?(primary_image_id)
errors.add(:primary_image_id, "is not one of the page's images")
end
end
end