Class: Landable::Page

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
ActionView::Helpers::TagHelper, HasAssets, HasTemplates, Librarian, Errors, TableName
Defined in:
app/models/landable/page.rb

Defined Under Namespace

Modules: Errors

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Errors

#error, #error?

Methods included from Librarian

#nuke!, #reactivate

Class Method Details

.by_path(path) ⇒ Object



64
65
66
# File 'app/models/landable/page.rb', line 64

def by_path(path)
  where(path: path).first || missing
end

.by_path!(path) ⇒ Object



68
69
70
# File 'app/models/landable/page.rb', line 68

def by_path!(path)
  where(path: path).first!
end

.example(attrs) ⇒ Object



78
79
80
81
82
83
84
85
# File 'app/models/landable/page.rb', line 78

def example(attrs)
  defaults = {
    title: 'Example page',
    body:  '<div>Example page contents would live here</div>'
  }

  new defaults.merge(attrs)
end

.generate_sitemap(options = {}) ⇒ Object



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
# File 'app/models/landable/page.rb', line 87

def generate_sitemap(options = {})
  pages = Landable::Page.sitemappable
  xml = Builder::XmlMarkup.new(indent: 2)
  xml.instruct! :xml, encoding: 'UTF-8'
  xml.urlset(xmlns: 'http://www.sitemaps.org/schemas/sitemap/0.9') do |markup|
    pages.each do |page|
      next if options[:exclude_categories].to_a.include? page.category.try(:name)
      markup.url do |p|
        p.loc "#{options[:protocol]}://#{options[:host]}#{page.path}"
        p.lastmod page.updated_at.to_time.iso8601
        p.changefreq 'weekly'
        p.priority '1'
      end
    end

    if options[:sitemap_additional_paths].present?
      options[:sitemap_additional_paths].each do |page|
        markup.url do |p|
          p.loc "#{options[:protocol]}://#{options[:host]}#{page}"
          p.changefreq 'weekly'
          p.priority '1'
        end
      end
    end
  end
end

.missingObject



60
61
62
# File 'app/models/landable/page.rb', line 60

def missing
  new(status_code: 410)
end

.with_fuzzy_path(path) ⇒ Object



72
73
74
75
76
# File 'app/models/landable/page.rb', line 72

def with_fuzzy_path(path)
  select("*, similarity(path, #{Page.sanitize path}) _sml")
    .where('path LIKE ?', "%#{path}%")
    .order('_sml DESC, path ASC')
end

Instance Method Details

#body_strip_searchObject



221
222
223
224
225
226
227
# File 'app/models/landable/page.rb', line 221

def body_strip_search
  RenderService.call(self)
rescue ::Liquid::Error
  errors[:body] = 'contains a Liquid syntax error'
rescue StandardError => error
  errors[:body] = 'had a problem: ' + error.message
end

#content_typeObject



123
124
125
126
127
128
129
130
131
132
133
134
# File 'app/models/landable/page.rb', line 123

def content_type
  case path_extension
  when nil, 'htm', 'html'
    'text/html'
  when 'json'
    'application/json'
  when 'xml'
    'application/xml'
  else
    'text/plain'
  end
end

#deactivateObject



136
137
138
139
140
141
142
# File 'app/models/landable/page.rb', line 136

def deactivate
  update_attribute(:status_code, 410)

  publish!(author_id: updated_by_author.id, notes: 'This page has been trashed')

  super
end

#directory_after(prefix) ⇒ Object



148
149
150
151
152
153
154
155
156
# File 'app/models/landable/page.rb', line 148

def directory_after(prefix)
  remainder = path.gsub(%r{^#{prefix}\/?}, '')
  segments  = remainder.split('/', 2)
  if segments.length == 1
    nil
  else
    segments.first
  end
end

#downcase_path!Object



115
116
117
# File 'app/models/landable/page.rb', line 115

def downcase_path!
  path.try :downcase!
end

#forbid_changing_pathObject



217
218
219
# File 'app/models/landable/page.rb', line 217

def forbid_changing_path
  errors[:path] = 'can not be changed!' if self.path_changed?
end

#hero_asset_existenceObject



234
235
236
237
238
# File 'app/models/landable/page.rb', line 234

def hero_asset_existence
  return true if @hero_asset_name.blank?
  return if Asset.find_by_name(@hero_asset_name)
  errors[:hero_asset_name] = "System can't find an asset with this name"
end

#hero_asset_nameObject



169
170
171
# File 'app/models/landable/page.rb', line 169

def hero_asset_name
  hero_asset.try(:name)
end

#hero_asset_name=(name) ⇒ Object



173
174
175
176
177
# File 'app/models/landable/page.rb', line 173

def hero_asset_name=(name)
  @hero_asset_name = name
  asset = Asset.find_by_name(name)
  self.hero_asset_id = asset.try(:asset_id)
end

#hero_asset_urlObject



179
180
181
# File 'app/models/landable/page.rb', line 179

def hero_asset_url
  hero_asset.try(:public_url)
end

#html?Boolean

Returns:

  • (Boolean)


144
145
146
# File 'app/models/landable/page.rb', line 144

def html?
  content_type == 'text/html'
end

#page_name_byte_sizeObject



229
230
231
232
# File 'app/models/landable/page.rb', line 229

def page_name_byte_size
  return unless page_name.present? && page_name.bytesize > 100
  errors[:page_name] = 'Invalid PageName, bytesize is too big!'
end

#path=(name) ⇒ Object



162
163
164
165
166
167
# File 'app/models/landable/page.rb', line 162

def path=(name)
  # if not present, add a leading slash for a non-empty path
  name = name.gsub(%r{^\/?(.*)}, '/\1') if name && !name.empty?

  self[:path] = name
end

#path_extensionObject



119
120
121
# File 'app/models/landable/page.rb', line 119

def path_extension
  path.match(/\.(\w{2,})$/).try(:[], 1) if path
end

#preview_pathObject



209
210
211
# File 'app/models/landable/page.rb', line 209

def preview_path
  public_preview_page_path(self)
end

#preview_urlObject



213
214
215
# File 'app/models/landable/page.rb', line 213

def preview_url
  public_preview_page_url(self)
end

#publish!(options) ⇒ Object



183
184
185
186
187
188
189
# File 'app/models/landable/page.rb', line 183

def publish!(options)
  transaction do
    published_revision.unpublish! if published_revision
    revision = revisions.create! options
    update_attributes!(published_revision: revision, is_publishable: false)
  end
end

#published?Boolean

Returns:

  • (Boolean)


191
192
193
# File 'app/models/landable/page.rb', line 191

def published?
  published_revision.present?
end

#redirect?Boolean

Returns:

  • (Boolean)


158
159
160
# File 'app/models/landable/page.rb', line 158

def redirect?
  status_code == 301 || status_code == 302
end

#revert_to!(revision) ⇒ Object



195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'app/models/landable/page.rb', line 195

def revert_to!(revision)
  self.title          = revision.title
  self.path           = revision.path
  self.body           = revision.body
  self.head_content   = revision.head_content
  self.category_id    = revision.category_id
  self.theme_id       = revision.theme_id
  self.status_code    = revision.status_code
  self.meta_tags      = revision.meta_tags
  self.redirect_url   = revision.redirect_url

  save!
end

#to_liquidObject



240
241
242
243
244
245
246
247
248
# File 'app/models/landable/page.rb', line 240

def to_liquid
  {
    'title' => title,
    'url' => path,
    'hero_asset' => hero_asset ? true : false,
    'hero_asset_url' => hero_asset_url,
    'abstract' => abstract
  }
end