Class: Page

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
vendor/plugins/pages/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_SEPERATOR =

when collecting the pages path how is each of the pages seperated?

" - "

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.per_page(dialog = false) ⇒ Object

Returns how many pages per page should there be when paginating pages



158
159
160
# File 'vendor/plugins/pages/app/models/page.rb', line 158

def self.per_page(dialog = false)
  dialog ? PAGES_PER_DIALOG : PAGES_PER_ADMIN_INDEX
end

.top_level(include_children = false) ⇒ Object

Returns all the top level pages, usually to render the top level navigation.



117
118
119
120
121
122
# File 'vendor/plugins/pages/app/models/page.rb', line 117

def self.top_level(include_children = false)
  include_associations = [:parts]
  include_associations.push(:slugs) if self.class.methods.include? "find_one_with_friendly"
  include_associations.push(:children) if include_children
  find_all_by_parent_id(nil,:conditions => {:show_in_menu => true, :draft => false}, :order => "position ASC", :include => include_associations)
end

Instance Method Details

#[](part_title) ⇒ Object

Accessor method to get a page part from a page. Example:

Page.first[:body]

Will return the body page part of the first page.



130
131
132
133
134
135
136
137
138
139
140
141
# File 'vendor/plugins/pages/app/models/page.rb', line 130

def [](part_title)
  # don't want to override a super method when trying to call a page part.
  # the way that we call page parts seems flawed, will probably revert to page.parts[:title] in a future release.
  if (super_value = super).blank?
    # self.parts is already eager loaded so we can now just grab the first element matching the title we specified.
    part = self.parts.detect {|part| (part.title == part_title.to_s) || (part.title.downcase.gsub(" ", "_") == part_title.to_s.downcase.gsub(" ", "_")) }

    return part.body unless part.nil?
  end

  super_value
end

#all_page_part_contentObject

Used to index all the content on this page so it can be easily searched.



153
154
155
# File 'vendor/plugins/pages/app/models/page.rb', line 153

def all_page_part_content
  self.parts.collect {|p| p.body}.join(" ")
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

Returns:

  • (Boolean)


31
32
33
# File 'vendor/plugins/pages/app/models/page.rb', line 31

def deletable?
  self.deletable && self.link_url.blank? and self.menu_match.blank?
end

#destroyObject

Before destroying a page we check to see if it’s a deletable page or not Refinery system pages are not deletable.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'vendor/plugins/pages/app/models/page.rb', line 37

def destroy
  if self.deletable?
    super
  else
    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 self.link_url.present?
      puts "unset .menu_match," if self.menu_match.present?
      puts "set .deletable to true" unless self.deletable
    end

    return false
  end
end

#destroy!Object

If you want to destroy a page that is set to be not deletable this is the way to do it.



54
55
56
57
58
59
60
61
# File 'vendor/plugins/pages/app/models/page.rb', line 54

def destroy!
  self.update_attributes({
    :menu_match => nil,
    :link_url => nil,
    :deletable => true
  })
  self.destroy
end

#home?Boolean

Returns true if this page is the home page or links to it.

Returns:

  • (Boolean)


107
108
109
# File 'vendor/plugins/pages/app/models/page.rb', line 107

def home?
  self.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. If any of the page’s ancestors aren’t to be shown in the menu then this page is not either.

Returns:

  • (Boolean)


102
103
104
# File 'vendor/plugins/pages/app/models/page.rb', line 102

def in_menu?
  self.live? && self.show_in_menu? && !self.ancestors.any? { |a| !a.in_menu? }
end

#indented_titleObject



63
64
65
# File 'vendor/plugins/pages/app/models/page.rb', line 63

def indented_title
  "#{"--" * self.ancestors.size} #{self.title}".chomp
end

#live?Boolean

Returns true if this page is “published”

Returns:

  • (Boolean)


95
96
97
# File 'vendor/plugins/pages/app/models/page.rb', line 95

def live?
  not self.draft?
end

#path(reverse = true) ⇒ 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_SEPERATOR



69
70
71
72
73
74
75
76
77
# File 'vendor/plugins/pages/app/models/page.rb', line 69

def path(reverse = true)
  unless self.parent.nil?
    parts = [self.title, self.parent.path(reverse)]
    parts.reverse! if reverse
    parts.join(PATH_SEPERATOR)
  else
    self.title
  end
end

#shown_siblingsObject

Returns all visible sibling pages that can be rendered for the menu



112
113
114
# File 'vendor/plugins/pages/app/models/page.rb', line 112

def shown_siblings
  self.siblings.reject { |sibling| not sibling.in_menu? }
end

#title_with_metaObject

In the admin area we use a slightly different title to inform the which pages are draft or hidden pages



144
145
146
147
148
149
150
# File 'vendor/plugins/pages/app/models/page.rb', line 144

def title_with_meta
  title = self.title
  title << " <em>(hidden)</em>" unless self.show_in_menu?
  title << " <em>(draft)</em>" if self.draft?

  title.strip
end

#urlObject

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 Us” 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 “/inquiries/new”



86
87
88
89
90
91
92
# File 'vendor/plugins/pages/app/models/page.rb', line 86

def url
  if self.link_url.present?
    self.link_url =~ /^\// ? {:controller => self.link_url} : self.link_url
  elsif self.to_param.present?
    {:controller => "/pages", :action => "show", :id => self.to_param}
  end
end