Class: Page

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/page.rb

Overview

Schema Information

Table name: pages

 id               :integer(4)      not null, primary key
 title            :string(255)
 parent_id        :integer(4)
 starttime        :datetime
 endtime          :datetime
 sorting          :integer(4)
            :boolean(1)      default(TRUE)
 hidden_in_menu   :boolean(1)      default(FALSE)
 subtitle         :string(255)
 navigation_title :string(255)
 description      :text
 abstract         :text
 author           :string(255)
 author_email     :string(255)
 target           :string(255)
 type             :string(255)     default("ContentPage")
 shortcut_page_id :integer(4)
 shortcut_mode    :string(255)     default("first_subpage")
 ext_url          :string(255)
 created_by       :integer(4)
 created_at       :datetime
 updated_at       :datetime
 deleted_at       :datetime
 ancestry         :string(255)
 template_id      :integer(4)
 tags             :string(255)
 url              :string(255)

available methods for templating:

  • title (Page title)
  • type (Page type - e.g. ShortcutPage, ContentPage)
  • starttime
  • endtime
  • hidden
  • hidden_in_menu
  • subtitle
  • navigation_title
  • description
  • abstract
  • author
  • author_email
  • target
  • created_at
  • updated_at
  • ancestry
  • tags

Direct Known Subclasses

ContentPage, ShortcutPage

Class Method Summary

Instance Method Summary

Class Method Details

+ (Object) find_by_path(url)

returns nil if the page could not be found return the page object requested by the path



76
77
78
79
80
81
82
83
84
85
86
# File 'app/models/page.rb', line 76

def find_by_path(url)
  # return the first page on the root level, if the url is empty
  return Page.roots.sorted.first if url.empty?
  
  # get the first page of the path
  @current_page = Page.roots.find_by_url(url.shift)
  # go through each path segment
  url.each { |page_title| @current_page = @current_page.children.find_by_url(page_title) }
  
  @current_page
end

Instance Method Details

- (Object) absolute_url



97
98
99
# File 'app/models/page.rb', line 97

def absolute_url
  "#{Settings.website.base_url}#{path}"
end

- (Object) dummy_type

is getting used, because type is a Rails keyword



111
112
113
# File 'app/models/page.rb', line 111

def dummy_type
  type
end

- (Object) dummy_type=(type)

is getting used, because type is a Rails keyword



106
107
108
# File 'app/models/page.rb', line 106

def dummy_type=(type)
  self[:type] = type
end

- (Object) edit_fields

the labels will be used as locale keys required attributes:

  • label
  • field_type
  • value

optional attributes:

  • options

available field types:

  • text_field
  • text_area

returns the input fields and labels, that should be displayed in the "settings" tab, as an hash



137
138
139
# File 'app/models/page.rb', line 137

def edit_fields
  []
end

- (Object) icon

returns the icon of the specific page type can be overwritten in the kind class



122
123
124
# File 'app/models/page.rb', line 122

def icon
  "/images/icons/pages/#{self.class.to_s.underscore}.png"
end

- (Boolean) is_leaf?

a leaf does not have any children

Returns:

  • (Boolean)


142
143
144
# File 'app/models/page.rb', line 142

def is_leaf?
  children.empty?
end


101
102
103
# File 'app/models/page.rb', line 101

def link
  "<a href='#{path}'>#{title}</a>"
end

- (Object) path

returns the path to the page for example: /home/about-us/



92
93
94
95
# File 'app/models/page.rb', line 92

def path
  path = path_ids.collect{ |id| Page.find(id).url  }
  "/#{path.join('/')}/"
end

- (Object) text

alias for title - needed for ExtJS



116
117
118
# File 'app/models/page.rb', line 116

def text
  title
end

- (Object) update_sorting(drop_page_id, position)

updates the sorting of pages after adding or moving a page possible positions are

  • above
  • append
  • below


151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'app/models/page.rb', line 151

def update_sorting(drop_page_id, position)
  drop_page = (drop_page_id=="root") ? nil : Page.find(drop_page_id)
  if drop_page
    # if the page gets appended by another, it will get sorting=1
    if position=="append"
      update_attributes :parent_id => drop_page.id, :sorting => 1
    else
      drop_page.sorting += 1 if position=="below"
      sorting = drop_page.sorting
      
      pages = drop_page.parent_id ? Page.children_of(drop_page.parent_id) : Page.roots
      pages.having_sorting_bigger_than(drop_page.sorting).each{ |page| page.update_attributes :sorting => page.sorting+1 }
      
      update_attributes :parent_id => drop_page.parent_id, :sorting => sorting
    end
  else
    sorting = 1
    pages = Page.roots
    pages.having_sorting_bigger_than(sorting).each{ |page| page.update_attributes :sorting => page.sorting+1 }
    
    update_attributes :parent_id => nil, :sorting => sorting
  end
end