Class: Page
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Page
- Defined in:
- app/models/page.rb
Constant Summary collapse
- PAGES_PER_DIALOG =
when a dialog pops up to link to a page, how many pages per page should there be
14
- PAGES_PER_ADMIN_INDEX =
when listing pages out in the admin area, how many pages should show per page
20
- PATH_SEPARATOR =
when collecting the pages path how is each of the pages seperated?
" - "
Instance Attribute Summary collapse
-
#locale ⇒ Object
to hold temporarily.
Class Method Summary collapse
-
.default_parts ⇒ Object
Accessor to find out the default page parts created for each new page.
-
.different_frontend_locale? ⇒ Boolean
Wraps up all the checks that we need to do to figure out whether the current frontend locale is different to the current one set by ::I18n.locale.
- .expire_page_caching ⇒ Object
-
.per_page(dialog = false) ⇒ Object
Returns how many pages per page should there be when paginating pages.
- .use_marketable_urls? ⇒ Boolean
- .with_globalize(conditions = {}) ⇒ Object
Instance Method Summary collapse
- #[](part_title) ⇒ Object
-
#all_page_part_content ⇒ Object
Used to index all the content on this page so it can be easily searched.
- #cache_key ⇒ Object
-
#content_for(part_title) ⇒ Object
Accessor method to get a page part from a 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.
-
#home? ⇒ Boolean
Returns true if this page is the home page or links to 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
Returns an array with all ancestors to_param, allow with its own Ex: with an About page and a Mission underneath, Page.find(‘mission’).nested_url would return:.
-
#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
-
#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.
- #path_cache_key ⇒ Object
-
#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.
-
#title_with_meta ⇒ Object
In the admin area we use a slightly different title to inform the which pages are draft or hidden pages.
- #to_refinery_menu_item ⇒ Object
- #translation ⇒ Object
- #uncached_nested_url ⇒ Object
-
#url ⇒ Object
When this page is rendered in the navigation, where should it link? If a custom “link_url” is set, it uses that otherwise it defaults to a normal page URL.
- #url_cache_key ⇒ Object
- #url_marketable ⇒ Object
- #url_normal ⇒ Object
- #with_locale_param(url_hash) ⇒ Object
Instance Attribute Details
#locale ⇒ Object
to hold temporarily
56 57 58 |
# File 'app/models/page.rb', line 56 def locale @locale end |
Class Method Details
.default_parts ⇒ Object
Accessor to find out the default page parts created for each new page
286 287 288 |
# File 'app/models/page.rb', line 286 def default_parts RefinerySetting.find_or_set(:default_page_parts, ["Body", "Side Body"]) end |
.different_frontend_locale? ⇒ Boolean
Wraps up all the checks that we need to do to figure out whether the current frontend locale is different to the current one set by ::I18n.locale. This terminates in a false if i18n engine is not defined or enabled.
293 294 295 |
# File 'app/models/page.rb', line 293 def different_frontend_locale? ::Refinery.i18n_enabled? && ::Refinery::I18n.current_frontend_locale != ::I18n.locale end |
.expire_page_caching ⇒ Object
306 307 308 309 310 311 312 313 |
# File 'app/models/page.rb', line 306 def expire_page_caching begin Rails.cache.delete_matched(/.*pages.*/) rescue NotImplementedError Rails.cache.clear warn "**** [REFINERY] The cache store you are using is not compatible with Rails.cache#delete_matched - clearing entire cache instead ***" end end |
.per_page(dialog = false) ⇒ Object
Returns how many pages per page should there be when paginating pages
298 299 300 |
# File 'app/models/page.rb', line 298 def per_page(dialog = false) dialog ? PAGES_PER_DIALOG : PAGES_PER_ADMIN_INDEX end |
.use_marketable_urls? ⇒ Boolean
302 303 304 |
# File 'app/models/page.rb', line 302 def use_marketable_urls? RefinerySetting.find_or_set(:use_marketable_urls, true, :scoping => 'pages') end |
.with_globalize(conditions = {}) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 |
# File 'app/models/page.rb', line 88 def self.with_globalize(conditions = {}) conditions = {:locale => Globalize.locale}.merge(conditions) globalized_conditions = {} conditions.keys.each do |key| if (translated_attribute_names.map(&:to_s) | %w(locale)).include?(key.to_s) globalized_conditions["#{self.translation_class.table_name}.#{key}"] = conditions.delete(key) end end # A join implies readonly which we don't really want. joins(:translations).where(globalized_conditions).where(conditions).readonly(false) end |
Instance Method Details
#[](part_title) ⇒ Object
332 333 334 335 336 337 338 339 340 341 342 343 344 |
# File 'app/models/page.rb', line 332 def [](part_title) # Allow for calling attributes with [] shorthand (eg page[:parent_id]) return super if self.respond_to?(part_title.to_s.to_sym) or self.attributes.has_key?(part_title.to_s) Refinery.deprecate({ :what => "page[#{part_title.inspect}]", :when => '1.1', :replacement => "page.content_for(#{part_title.inspect})", :caller => caller }) content_for(part_title) end |
#all_page_part_content ⇒ Object
Used to index all the content on this page so it can be easily searched.
361 362 363 |
# File 'app/models/page.rb', line 361 def all_page_part_content parts.collect {|p| p.body}.join(" ") end |
#cache_key ⇒ Object
242 243 244 |
# File 'app/models/page.rb', line 242 def cache_key [Refinery.base_cache_key, ::I18n.locale, to_param].compact.join('/') end |
#content_for(part_title) ⇒ Object
Accessor method to get a page part from a page. Example:
Page.first.content_for(:body)
Will return the body page part of the first page.
322 323 324 325 326 327 328 329 330 |
# File 'app/models/page.rb', line 322 def content_for(part_title) part = 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 part.try(:body) 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
117 118 119 |
# File 'app/models/page.rb', line 117 def deletable? deletable && link_url.blank? and .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.
131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'app/models/page.rb', line 131 def destroy return super if deletable? unless Rails.env.test? # give useful feedback when trying to delete from console puts "This page is not deletable. Please use .destroy! if you really want it deleted " puts "unset .link_url," if link_url.present? puts "unset .menu_match," if .present? puts "set .deletable to true" unless deletable end return 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.
146 147 148 149 150 151 152 |
# File 'app/models/page.rb', line 146 def destroy! self. = nil self.link_url = nil self.deletable = true destroy end |
#home? ⇒ Boolean
Returns true if this page is the home page or links to it.
262 263 264 |
# File 'app/models/page.rb', line 262 def home? link_url == '/' 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.
253 254 255 |
# File 'app/models/page.rb', line 253 def live? && end |
#link_url_localised? ⇒ Boolean
186 187 188 189 190 191 192 193 194 195 196 |
# File 'app/models/page.rb', line 186 def link_url_localised? return link_url unless ::Refinery.i18n_enabled? current_url = link_url if current_url =~ %r{^/} && ::Refinery::I18n.current_frontend_locale != ::Refinery::I18n.default_frontend_locale current_url = "/#{::Refinery::I18n.current_frontend_locale}#{current_url}" end current_url end |
#live? ⇒ Boolean
Returns true if this page is “published”
247 248 249 |
# File 'app/models/page.rb', line 247 def live? not draft? end |
#nested_path ⇒ Object
Returns the string version of nested_url, i.e., the path that should be generated by the router
230 231 232 |
# File 'app/models/page.rb', line 230 def nested_path Rails.cache.fetch(path_cache_key) { ['', nested_url].join('/') } end |
#nested_url ⇒ Object
Returns an array with all ancestors to_param, allow with its own Ex: with an About page and a Mission underneath, Page.find(‘mission’).nested_url would return:
['about', 'mission']
220 221 222 |
# File 'app/models/page.rb', line 220 def nested_url Rails.cache.fetch(url_cache_key) { uncached_nested_url } 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.
Returns the sluggified string
370 371 372 373 374 375 376 377 |
# File 'app/models/page.rb', line 370 def normalize_friendly_id(slug_string) slug_string.gsub!('_', '-') sluggified = super if self.class.use_marketable_urls? && self.class.friendly_id_config.reserved_words.include?(sluggified) sluggified << "-page" end sluggified end |
#not_in_menu? ⇒ Boolean
257 258 259 |
# File 'app/models/page.rb', line 257 def not 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
156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'app/models/page.rb', line 156 def path( = {}) # Override default options with any supplied. = {:reversed => true}.merge() unless parent_id.nil? parts = [title, parent.path()] parts.reverse! if [:reversed] parts.join(PATH_SEPARATOR) else title end end |
#path_cache_key ⇒ Object
234 235 236 |
# File 'app/models/page.rb', line 234 def path_cache_key [cache_key, 'nested_path'].join('#') 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.
123 124 125 126 127 |
# File 'app/models/page.rb', line 123 def reposition_parts! parts.each_with_index do |part, index| part.update_attribute(:position, index) end end |
#shown_siblings ⇒ Object
Returns all visible sibling pages that can be rendered for the menu
267 268 269 |
# File 'app/models/page.rb', line 267 def shown_siblings siblings.reject(&:not_in_menu?) end |
#title_with_meta ⇒ Object
In the admin area we use a slightly different title to inform the which pages are draft or hidden pages
347 348 349 350 351 352 353 354 355 356 357 358 |
# File 'app/models/page.rb', line 347 def title = if self.title.nil? [::Page::Translation.where(:page_id => self.id, :locale => Globalize.locale).first.try(:title).to_s] else [self.title.to_s] end title << "<em>(#{::I18n.t('hidden', :scope => 'admin.pages.page')})</em>" unless title << "<em>(#{::I18n.t('draft', :scope => 'admin.pages.page')})</em>" if draft? title.join(' ') end |
#to_refinery_menu_item ⇒ Object
271 272 273 274 275 276 277 278 279 280 281 282 |
# File 'app/models/page.rb', line 271 def { :id => id, :lft => lft, :menu_match => , :parent_id => parent_id, :rgt => rgt, :title => (page_title if respond_to?(:page_title)) || title, :type => self.class.name, :url => url } end |
#translation ⇒ Object
25 26 27 28 29 30 31 32 |
# File 'app/models/page.rb', line 25 def translation if @translation.nil? or @translation.try(:locale) != ::Globalize.locale @translation = translations.with_locale(::Globalize.locale).first @translation ||= translations.build(:locale => ::Globalize.locale) end @translation end |
#uncached_nested_url ⇒ Object
224 225 226 |
# File 'app/models/page.rb', line 224 def uncached_nested_url [parent.try(:nested_url), to_param].compact.flatten end |
#url ⇒ Object
When this page is rendered in the navigation, where should it link? If a custom “link_url” is set, it uses that otherwise it defaults to a normal page URL. The “link_url” is often used to link to a plugin rather than a page.
For example if I had a “Contact” page I don’t want it to just render a contact us page I want it to show the Inquiries form so I can collect inquiries. So I would set the “link_url” to “/contact”
176 177 178 179 180 181 182 183 184 |
# File 'app/models/page.rb', line 176 def url if link_url.present? link_url_localised? elsif self.class.use_marketable_urls? with_locale_param url_marketable elsif to_param.present? with_locale_param url_normal end end |
#url_cache_key ⇒ Object
238 239 240 |
# File 'app/models/page.rb', line 238 def url_cache_key [cache_key, 'nested_url'].join('#') end |
#url_marketable ⇒ Object
198 199 200 201 |
# File 'app/models/page.rb', line 198 def url_marketable # :id => nil is important to prevent any other params[:id] from interfering with this route. url_normal.merge(:path => nested_url, :id => nil) end |
#url_normal ⇒ Object
203 204 205 |
# File 'app/models/page.rb', line 203 def url_normal {:controller => '/pages', :action => 'show', :path => nil, :id => to_param} end |
#with_locale_param(url_hash) ⇒ Object
207 208 209 210 211 212 |
# File 'app/models/page.rb', line 207 def with_locale_param(url_hash) if self.class.different_frontend_locale? url_hash.update(:locale => ::Refinery::I18n.current_frontend_locale) end url_hash end |