Class: Landable::Page
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Landable::Page
- 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
- .by_path(path) ⇒ Object
- .by_path!(path) ⇒ Object
- .example(attrs) ⇒ Object
- .generate_sitemap(options = {}) ⇒ Object
- .missing ⇒ Object
- .with_fuzzy_path(path) ⇒ Object
Instance Method Summary collapse
- #body_strip_search ⇒ Object
- #content_type ⇒ Object
- #deactivate ⇒ Object
- #directory_after(prefix) ⇒ Object
- #downcase_path! ⇒ Object
- #forbid_changing_path ⇒ Object
- #hero_asset_existence ⇒ Object
- #hero_asset_name ⇒ Object
- #hero_asset_name=(name) ⇒ Object
- #hero_asset_url ⇒ Object
- #html? ⇒ Boolean
- #page_name_byte_size ⇒ Object
- #path=(name) ⇒ Object
- #path_extension ⇒ Object
- #preview_path ⇒ Object
- #preview_url ⇒ Object
- #publish!(options) ⇒ Object
- #published? ⇒ Boolean
- #redirect? ⇒ Boolean
- #revert_to!(revision) ⇒ Object
- #to_liquid ⇒ Object
Methods included from Errors
Methods included from Librarian
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( = {}) 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 [:exclude_categories].to_a.include? page.category.try(:name) markup.url do |p| p.loc "#{[:protocol]}://#{[:host]}#{page.path}" p.lastmod page.updated_at.to_time.iso8601 p.changefreq 'weekly' p.priority '1' end end if [:sitemap_additional_paths].present? [:sitemap_additional_paths].each do |page| markup.url do |p| p.loc "#{[:protocol]}://#{[:host]}#{page}" p.changefreq 'weekly' p.priority '1' end end end end end |
.missing ⇒ Object
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_search ⇒ Object
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. end |
#content_type ⇒ Object
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 |
#deactivate ⇒ Object
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: .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_path ⇒ Object
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_existence ⇒ Object
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_name ⇒ Object
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_url ⇒ Object
179 180 181 |
# File 'app/models/landable/page.rb', line 179 def hero_asset_url hero_asset.try(:public_url) end |
#html? ⇒ Boolean
144 145 146 |
# File 'app/models/landable/page.rb', line 144 def html? content_type == 'text/html' end |
#page_name_byte_size ⇒ Object
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_extension ⇒ Object
119 120 121 |
# File 'app/models/landable/page.rb', line 119 def path_extension path.match(/\.(\w{2,})$/).try(:[], 1) if path end |
#preview_path ⇒ Object
209 210 211 |
# File 'app/models/landable/page.rb', line 209 def preview_path public_preview_page_path(self) end |
#preview_url ⇒ Object
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!() transaction do published_revision.unpublish! if published_revision revision = revisions.create! update_attributes!(published_revision: revision, is_publishable: false) end end |
#published? ⇒ Boolean
191 192 193 |
# File 'app/models/landable/page.rb', line 191 def published? published_revision.present? end |
#redirect? ⇒ 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. = revision. self.redirect_url = revision.redirect_url save! end |
#to_liquid ⇒ Object
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 |