Class: Refinery::Page
- Inherits:
-
Core::BaseModel
- Object
- Core::BaseModel
- Refinery::Page
- Extended by:
- FriendlyId
- Defined in:
- app/models/refinery/page.rb
Defined Under Namespace
Classes: Translation
Class Method Summary collapse
-
.by_slug(slug, conditions = {}) ⇒ Object
Finds pages by their slug.
-
.by_title(title) ⇒ Object
Finds pages by their title.
-
.fast_menu ⇒ Object
An optimised scope containing only live pages ordered for display in a menu.
-
.find_by_path(path) ⇒ Object
Find page by path, checking for scoping rules.
-
.find_by_path_or_id(path, id) ⇒ Object
Helps to resolve the situation where you have a path and an id and if the path is unfriendly then a different finder method is required than find_by_path.
-
.in_menu ⇒ Object
Shows all pages with :show_in_menu set to true, but it also rejects any page that has not been translated to the current locale.
-
.live ⇒ Object
Live pages are ‘allowed’ to be shown in the frontend of your website.
-
.per_page(dialog = false) ⇒ Object
Returns how many pages per page should there be when paginating pages.
- .rebuild_with_slug_nullification! ⇒ Object
-
.with_globalize(conditions = {}) ⇒ Object
Wrap up the logic of finding the pages based on the translations table.
Instance Method Summary collapse
-
#canonical ⇒ Object
The canonical page for this particular page.
-
#canonical_slug ⇒ Object
The canonical slug for this particular page.
-
#content_for(part_title) ⇒ Object
Accessor method to get a page part from a page.
-
#content_for?(part_title) ⇒ Boolean
Accessor method to test whether a page part exists and has content for this page.
-
#custom_slug_or_title ⇒ Object
Returns in cascading order: custom_slug or menu_title or title depending on which attribute is first found to be present for this page.
-
#deletable? ⇒ Boolean
Am I allowed to delete this page? If a link_url is set we don’t want to break the link so we don’t allow them to delete If deletable is set to false then we don’t allow this page to be deleted.
-
#destroy ⇒ Object
Before destroying a page we check to see if it’s a deletable page or not Refinery system pages are not deletable.
-
#destroy! ⇒ Object
If you want to destroy a page that is set to be not deletable this is the way to do it.
-
#in_menu? ⇒ Boolean
Return true if this page can be shown in the navigation.
- #link_url_localised? ⇒ Boolean
-
#live? ⇒ Boolean
Returns true if this page is “published”.
-
#nested_path ⇒ Object
Returns the string version of nested_url, i.e., the path that should be generated by the router.
- #nested_url ⇒ Object (also: #uncached_nested_url)
- #not_in_menu? ⇒ Boolean
-
#part_with_title(part_title) ⇒ Object
Accessor method to get a page part object from a page.
-
#path(options = {}) ⇒ Object
Used for the browser title to get the full path to this page It automatically prints out this page title and all of it’s parent page titles joined by a PATH_SEPARATOR.
-
#reposition_parts! ⇒ Object
Repositions the child page_parts that belong to this page.
-
#shown_siblings ⇒ Object
Returns all visible sibling pages that can be rendered for the menu.
- #to_refinery_menu_item ⇒ Object
- #translated_to_default_locale? ⇒ Boolean
- #url ⇒ Object
- #url_marketable ⇒ Object
- #url_normal ⇒ Object
Class Method Details
.by_slug(slug, conditions = {}) ⇒ Object
Finds pages by their slug. This method is necessary because pages are translated which means the slug attribute does not exist on the pages table thus requiring us to find the attribute on the translations table and then join to the pages table again to return the associated record.
108 109 110 111 112 113 |
# File 'app/models/refinery/page.rb', line 108 def by_slug(slug, conditions={}) with_globalize({ :locale => Refinery::I18n.frontend_locales.map(&:to_s), :slug => slug }.merge(conditions)) end |
.by_title(title) ⇒ Object
Finds pages by their title. This method is necessary because pages are translated which means the title attribute does not exist on the pages table thus requiring us to find the attribute on the translations table and then join to the pages table again to return the associated record.
100 101 102 |
# File 'app/models/refinery/page.rb', line 100 def by_title(title) with_globalize(:title => title) end |
.fast_menu ⇒ Object
An optimised scope containing only live pages ordered for display in a menu.
124 125 126 |
# File 'app/models/refinery/page.rb', line 124 def live..order(arel_table[:lft]).includes(:parent, :translations) end |
.find_by_path(path) ⇒ Object
Find page by path, checking for scoping rules
66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'app/models/refinery/page.rb', line 66 def find_by_path(path) if ::Refinery::Pages.scope_slug_by_parent # With slugs scoped to the parent page we need to find a page by its full path. # For example with about/example we would need to find 'about' and then its child # called 'example' otherwise it may clash with another page called /example. path = path.split('/').select(&:present?) page = by_slug(path.shift, :parent_id => nil).first page = page.children.by_slug(path.shift).first while page && path.any? else page = by_slug(path).first end page end |
.find_by_path_or_id(path, id) ⇒ Object
Helps to resolve the situation where you have a path and an id and if the path is unfriendly then a different finder method is required than find_by_path.
84 85 86 87 88 89 90 91 92 93 94 |
# File 'app/models/refinery/page.rb', line 84 def find_by_path_or_id(path, id) if path.present? if path.friendly_id? find_by_path(path) else find(path) end elsif id.present? find(id) end end |
.in_menu ⇒ Object
Shows all pages with :show_in_menu set to true, but it also rejects any page that has not been translated to the current locale. This works using a query against the translated content first and then using all of the page_ids we further filter against this model’s table.
119 120 121 |
# File 'app/models/refinery/page.rb', line 119 def where(:show_in_menu => true).with_globalize end |
.live ⇒ Object
Live pages are ‘allowed’ to be shown in the frontend of your website. By default, this is all pages that are not set as ‘draft’.
61 62 63 |
# File 'app/models/refinery/page.rb', line 61 def live where(:draft => false) end |
.per_page(dialog = false) ⇒ Object
Returns how many pages per page should there be when paginating pages
146 147 148 |
# File 'app/models/refinery/page.rb', line 146 def per_page(dialog = false) dialog ? Pages.pages_per_dialog : Pages.pages_per_admin_index end |
.rebuild_with_slug_nullification! ⇒ Object
150 151 152 153 |
# File 'app/models/refinery/page.rb', line 150 def rebuild_with_slug_nullification! rebuild_without_slug_nullification! nullify_duplicate_slugs_under_the_same_parent! end |
.with_globalize(conditions = {}) ⇒ Object
Wrap up the logic of finding the pages based on the translations table.
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'app/models/refinery/page.rb', line 129 def with_globalize(conditions = {}) conditions = {:locale => ::Globalize.locale.to_s}.merge(conditions) translations_conditions = {} translated_attrs = translated_attribute_names.map(&:to_s) | %w(locale) conditions.keys.each do |key| if translated_attrs.include? key.to_s translations_conditions["#{self.translation_class.table_name}.#{key}"] = conditions.delete(key) end end # A join implies readonly which we don't really want. where(conditions).joins(:translations).where(translations_conditions). readonly(false) end |
Instance Method Details
#canonical ⇒ Object
The canonical page for this particular page. Consists of:
* The default locale's translated slug
176 177 178 |
# File 'app/models/refinery/page.rb', line 176 def canonical Globalize.with_locale(::Refinery::I18n.default_frontend_locale) { url } end |
#canonical_slug ⇒ Object
The canonical slug for this particular page. This is the slug for the default frontend locale.
182 183 184 |
# File 'app/models/refinery/page.rb', line 182 def canonical_slug Globalize.with_locale(::Refinery::I18n.default_frontend_locale) { slug } end |
#content_for(part_title) ⇒ Object
326 327 328 |
# File 'app/models/refinery/page.rb', line 326 def content_for(part_title) part_with_title(part_title).try(:body) end |
#content_for?(part_title) ⇒ Boolean
337 338 339 |
# File 'app/models/refinery/page.rb', line 337 def content_for?(part_title) content_for(part_title).present? end |
#custom_slug_or_title ⇒ Object
Returns in cascading order: custom_slug or menu_title or title depending on which attribute is first found to be present for this page.
188 189 190 |
# File 'app/models/refinery/page.rb', line 188 def custom_slug_or_title custom_slug.presence || .presence || title.presence end |
#deletable? ⇒ Boolean
Am I allowed to delete this page? If a link_url is set we don’t want to break the link so we don’t allow them to delete If deletable is set to false then we don’t allow this page to be deleted. These are often Refinery system pages
195 196 197 |
# File 'app/models/refinery/page.rb', line 195 def deletable? deletable && link_url.blank? && .blank? end |
#destroy ⇒ Object
Before destroying a page we check to see if it’s a deletable page or not Refinery system pages are not deletable.
209 210 211 212 213 214 215 |
# File 'app/models/refinery/page.rb', line 209 def destroy return super if deletable? puts_destroy_help false end |
#destroy! ⇒ Object
If you want to destroy a page that is set to be not deletable this is the way to do it.
218 219 220 221 222 223 224 |
# File 'app/models/refinery/page.rb', line 218 def destroy! self. = nil self.link_url = nil self.deletable = true destroy end |
#in_menu? ⇒ Boolean
Return true if this page can be shown in the navigation. If it’s a draft or is set to not show in the menu it will return false.
293 294 295 |
# File 'app/models/refinery/page.rb', line 293 def live? && end |
#link_url_localised? ⇒ Boolean
245 246 247 248 249 |
# File 'app/models/refinery/page.rb', line 245 def link_url_localised? Refinery.deprecate "Refinery::Page#link_url_localised?", :when => '2.2', :replacement => "Refinery::Pages::Url::Localised#url" Pages::Url::Localised.new(self).url end |
#live? ⇒ Boolean
Returns true if this page is “published”
287 288 289 |
# File 'app/models/refinery/page.rb', line 287 def live? !draft? end |
#nested_path ⇒ Object
Returns the string version of nested_url, i.e., the path that should be generated by the router
282 283 284 |
# File 'app/models/refinery/page.rb', line 282 def nested_path ['', nested_url].join('/') end |
#nested_url ⇒ Object Also known as: uncached_nested_url
263 264 265 266 267 268 269 270 |
# File 'app/models/refinery/page.rb', line 263 def nested_url globalized_slug = Globalize.with_locale(slug_locale) { to_param.to_s } if ::Refinery::Pages.scope_slug_by_parent [parent.try(:nested_url), globalized_slug].compact.flatten else [globalized_slug] end end |
#not_in_menu? ⇒ Boolean
297 298 299 |
# File 'app/models/refinery/page.rb', line 297 def ! end |
#part_with_title(part_title) ⇒ Object
347 348 349 350 351 352 353 354 355 |
# File 'app/models/refinery/page.rb', line 347 def part_with_title(part_title) # self.parts is usually already eager loaded so we can now just grab # the first element matching the title we specified. self.parts.detect do |part| part.title.present? and # protecting against the problem that occurs when have nil title part.title == part_title.to_s or part.title.downcase.gsub(" ", "_") == part_title.to_s.downcase.gsub(" ", "_") end end |
#path(options = {}) ⇒ Object
Used for the browser title to get the full path to this page It automatically prints out this page title and all of it’s parent page titles joined by a PATH_SEPARATOR
228 229 230 231 232 233 234 235 236 237 238 239 |
# File 'app/models/refinery/page.rb', line 228 def path( = {}) # Override default options with any supplied. = {:reversed => true}.merge() if parent_id parts = [title, parent.path()] parts.reverse! if [:reversed] parts.join(' - ') else title end end |
#reposition_parts! ⇒ Object
Repositions the child page_parts that belong to this page. This ensures that they are in the correct 0,1,2,3,4… etc order.
201 202 203 204 205 |
# File 'app/models/refinery/page.rb', line 201 def reposition_parts! reload.parts.each_with_index do |part, index| part.update_attributes :position => index end end |
#shown_siblings ⇒ Object
Returns all visible sibling pages that can be rendered for the menu
302 303 304 |
# File 'app/models/refinery/page.rb', line 302 def shown_siblings siblings.reject(&:not_in_menu?) end |
#to_refinery_menu_item ⇒ Object
306 307 308 309 310 311 312 313 314 315 316 317 318 |
# File 'app/models/refinery/page.rb', line 306 def { :id => id, :lft => lft, :depth => depth, :menu_match => , :parent_id => parent_id, :rgt => rgt, :title => .presence || title.presence, :type => self.class.name, :url => url } end |
#translated_to_default_locale? ⇒ Boolean
169 170 171 |
# File 'app/models/refinery/page.rb', line 169 def translated_to_default_locale? persisted? && translations.where(:locale => Refinery::I18n.default_frontend_locale).any? end |
#url ⇒ Object
241 242 243 |
# File 'app/models/refinery/page.rb', line 241 def url Pages::Url.build(self) end |