Class: Landable::Page

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
ActionView::Helpers::TagHelper, HasAssets, 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



55
56
57
# File 'app/models/landable/page.rb', line 55

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

.by_path!(path) ⇒ Object



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

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

.example(attrs) ⇒ Object



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

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



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

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 |xml|
    pages.each do |page|
      next if options[:exclude_categories].to_a.include? page.category.try(:name)
      xml.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|
        xml.url do |p|
          p.loc "#{options[:protocol]}://#{options[:host]}#{page}"
          p.changefreq 'weekly'
          p.priority '1'
        end
      end
    end
  end
end

.missingObject



51
52
53
# File 'app/models/landable/page.rb', line 51

def missing
  new(status_code: 410)
end

.with_fuzzy_path(path) ⇒ Object



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

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



215
216
217
218
219
220
221
222
223
# File 'app/models/landable/page.rb', line 215

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

#content_typeObject



115
116
117
118
119
120
121
122
123
124
125
126
# File 'app/models/landable/page.rb', line 115

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



128
129
130
131
132
133
134
# File 'app/models/landable/page.rb', line 128

def deactivate
  self.update_attribute(:status_code, 410)
  
  publish!(author_id: updated_by_author.id, notes: "This page has been trashed")

  super
end

#directory_after(prefix) ⇒ Object



140
141
142
143
144
145
146
147
148
# File 'app/models/landable/page.rb', line 140

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

#downcase_path!Object



107
108
109
# File 'app/models/landable/page.rb', line 107

def downcase_path!
  path.try :downcase!
end

#forbid_changing_pathObject



211
212
213
# File 'app/models/landable/page.rb', line 211

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

#hero_asset_existenceObject



225
226
227
228
229
230
# File 'app/models/landable/page.rb', line 225

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

#hero_asset_nameObject



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

def hero_asset_name
  self.hero_asset.try(:name)
end

#hero_asset_name=(name) ⇒ Object



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

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



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

def hero_asset_url
  self.hero_asset.try(:public_url)
end

#html?Boolean

Returns:

  • (Boolean)


136
137
138
# File 'app/models/landable/page.rb', line 136

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

#path=(name) ⇒ Object



154
155
156
157
158
159
160
161
# File 'app/models/landable/page.rb', line 154

def path=(name)
  # if not present, add a leading slash for a non-empty path
  if name and not name.empty?
    name = name.gsub(/^\/?(.*)/, '/\1')
  end

  self[:path] = name
end

#path_extensionObject



111
112
113
# File 'app/models/landable/page.rb', line 111

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

#preview_pathObject



203
204
205
# File 'app/models/landable/page.rb', line 203

def preview_path
  public_preview_page_path(self)
end

#preview_urlObject



207
208
209
# File 'app/models/landable/page.rb', line 207

def preview_url
  public_preview_page_url(self)
end

#publish!(options) ⇒ Object



177
178
179
180
181
182
183
# File 'app/models/landable/page.rb', line 177

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)


185
186
187
# File 'app/models/landable/page.rb', line 185

def published?
  published_revision.present?
end

#redirect?Boolean

Returns:

  • (Boolean)


150
151
152
# File 'app/models/landable/page.rb', line 150

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

#revert_to!(revision) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'app/models/landable/page.rb', line 189

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



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

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