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: FriendlyIdOptions, FriendlyIdPath, 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.
-
.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! ⇒ 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_slug) ⇒ Object
Accessor method to get a page part from a page.
-
#content_for?(part_slug) ⇒ 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.
-
#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)
-
#normalize_friendly_id(slug_string) ⇒ Object
Protects generated slugs from title if they are in the list of reserved words This applies mostly to plugin-generated pages.
- #not_in_menu? ⇒ Boolean
-
#part_with_slug(part_slug) ⇒ Object
Accessor method to get a page part object from a page.
-
#path(path_separator: ' - ', ancestors_first: true) ⇒ Object
Returns the full path to this page.
-
#reposition_parts! ⇒ Object
Repositions the child page_parts that belong to this page.
-
#should_generate_new_friendly_id? ⇒ Boolean
If title changes tell friendly_id to regenerate slug when saving record.
-
#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
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.
120 121 122 |
# File 'app/models/refinery/page.rb', line 120 def by_slug(slug, conditions = {}) Pages::Finder.by_slug(slug, 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.
112 113 114 |
# File 'app/models/refinery/page.rb', line 112 def by_title(title) Pages::Finder.by_title(title) end |
.fast_menu ⇒ Object
An optimised scope containing only live pages ordered for display in a menu.
133 134 135 |
# File 'app/models/refinery/page.rb', line 133 def live..order(arel_table[:lft]).includes(:parent, :translations) end |
.find_by_path(path) ⇒ Object
Find page by path, checking for scoping rules
84 85 86 |
# File 'app/models/refinery/page.rb', line 84 def find_by_path(path) Pages::Finder.by_path(path) 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.
91 92 93 |
# File 'app/models/refinery/page.rb', line 91 def find_by_path_or_id(path, id) Pages::Finder.by_path_or_id(path, id) 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.
raise ActiveRecord::RecordNotFound if not found.
100 101 102 103 104 105 106 |
# File 'app/models/refinery/page.rb', line 100 def find_by_path_or_id!(path, id) page = find_by_path_or_id(path, id) raise ActiveRecord::RecordNotFound unless page page 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.
128 129 130 |
# File 'app/models/refinery/page.rb', line 128 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’.
79 80 81 |
# File 'app/models/refinery/page.rb', line 79 def live where(:draft => false) end |
.per_page(dialog = false) ⇒ Object
Returns how many pages per page should there be when paginating pages
143 144 145 |
# File 'app/models/refinery/page.rb', line 143 def per_page(dialog = false) dialog ? Pages.pages_per_dialog : Pages.pages_per_admin_index end |
.rebuild! ⇒ Object
147 148 149 150 |
# File 'app/models/refinery/page.rb', line 147 def rebuild! super 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.
138 139 140 |
# File 'app/models/refinery/page.rb', line 138 def with_globalize(conditions = {}) Pages::Finder.with_globalize(conditions) end |
Instance Method Details
#canonical ⇒ Object
The canonical page for this particular page. Consists of:
* The current locale's translated slug
173 174 175 |
# File 'app/models/refinery/page.rb', line 173 def canonical Globalize.with_locale(::Refinery::I18n.current_frontend_locale) { url } end |
#canonical_slug ⇒ Object
The canonical slug for this particular page. This is the slug for the current frontend locale.
179 180 181 |
# File 'app/models/refinery/page.rb', line 179 def canonical_slug Globalize.with_locale(::Refinery::I18n.current_frontend_locale) { slug } end |
#content_for(part_slug) ⇒ Object
300 301 302 |
# File 'app/models/refinery/page.rb', line 300 def content_for(part_slug) part_with_slug(part_slug).try(:body) end |
#content_for?(part_slug) ⇒ Boolean
311 312 313 |
# File 'app/models/refinery/page.rb', line 311 def content_for?(part_slug) content_for(part_slug).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.
185 186 187 188 |
# File 'app/models/refinery/page.rb', line 185 def custom_slug_or_title (Refinery::Pages.use_custom_slugs && 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
193 194 195 |
# File 'app/models/refinery/page.rb', line 193 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.
207 208 209 210 211 212 213 |
# File 'app/models/refinery/page.rb', line 207 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.
216 217 218 219 220 |
# File 'app/models/refinery/page.rb', line 216 def destroy! self.update_attributes(:menu_match => nil, :link_url => nil, :deletable => true) self.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.
267 268 269 |
# File 'app/models/refinery/page.rb', line 267 def live? && end |
#live? ⇒ Boolean
Returns true if this page is “published”
261 262 263 |
# File 'app/models/refinery/page.rb', line 261 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
256 257 258 |
# File 'app/models/refinery/page.rb', line 256 def nested_path ['', nested_url].join('/') end |
#nested_url ⇒ Object Also known as: uncached_nested_url
236 237 238 239 240 241 242 243 244 |
# File 'app/models/refinery/page.rb', line 236 def nested_url Globalize.with_locale(slug_locale) do if ::Refinery::Pages.scope_slug_by_parent && !root? self_and_ancestors.includes(:translations).map(&:to_param) else [to_param.to_s] end end end |
#normalize_friendly_id(slug_string) ⇒ Object
Protects generated slugs from title if they are in the list of reserved words This applies mostly to plugin-generated pages. This only kicks in when Refinery::Pages.marketable_urls is enabled. Also check for global scoping, and if enabled, allow slashes in slug.
Returns the sluggified string
335 336 337 |
# File 'app/models/refinery/page.rb', line 335 def normalize_friendly_id(slug_string) FriendlyIdPath.normalize_friendly_id(slug_string) end |
#not_in_menu? ⇒ Boolean
271 272 273 |
# File 'app/models/refinery/page.rb', line 271 def ! end |
#part_with_slug(part_slug) ⇒ Object
321 322 323 324 325 326 327 |
# File 'app/models/refinery/page.rb', line 321 def part_with_slug(part_slug) # 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.slug_matches?(part_slug) end end |
#path(path_separator: ' - ', ancestors_first: true) ⇒ Object
Returns the full path to this page. This automatically prints out this page title and all parent page titles. The result is joined by the path_separator argument.
225 226 227 228 229 230 |
# File 'app/models/refinery/page.rb', line 225 def path(path_separator: ' - ', ancestors_first: true) return title if root? chain = ancestors_first ? self_and_ancestors : self_and_ancestors.reverse chain.map(&:title).join(path_separator) 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.
199 200 201 202 203 |
# File 'app/models/refinery/page.rb', line 199 def reposition_parts! reload.parts.each_with_index do |part, index| part.update_columns position: index end end |
#should_generate_new_friendly_id? ⇒ Boolean
If title changes tell friendly_id to regenerate slug when saving record
45 46 47 |
# File 'app/models/refinery/page.rb', line 45 def should_generate_new_friendly_id? changes.keys.include?("title") || changes.keys.include?("custom_slug") end |
#shown_siblings ⇒ Object
Returns all visible sibling pages that can be rendered for the menu
276 277 278 |
# File 'app/models/refinery/page.rb', line 276 def shown_siblings siblings.reject(&:not_in_menu?) end |
#to_refinery_menu_item ⇒ Object
280 281 282 283 284 285 286 287 288 289 290 291 292 |
# File 'app/models/refinery/page.rb', line 280 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
166 167 168 |
# File 'app/models/refinery/page.rb', line 166 def translated_to_default_locale? persisted? && translations.any?{ |t| t.locale == Refinery::I18n.default_frontend_locale} end |