Class: Skyline::Page

Inherits:
Article
  • Object
show all
Extended by:
ActiveSupport::Memoizable
Includes:
Positionable, SearchableItem
Defined in:
app/models/skyline/page.rb

Defined Under Namespace

Classes: Data

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SearchableItem

#add_index, #evaluate_if_option, #hash_searchable_fields, #remove_from_index, #solr_id, #solr_typecast

Methods inherited from Article

#data_class, #depublish, #depublishable?, #destroy, #editable_by?, #enable_locking?, #enable_multiple_variants?, #enable_publishing?, #preview_wrapper_page, #previewable?, publishable?, #published?, #renderable?, #renderable_scope, #right_prefix, #rollbackable?, #set_default_variant, #set_default_variant!, #site, #sites, #title, to_param

Class Method Details

.find_by_url(url_parts, root = nil) ⇒ Object



128
129
130
131
132
133
134
135
136
# File 'app/models/skyline/page.rb', line 128

def find_by_url(url_parts, root = nil)
  root ||= self.root
  return nil unless root
  return [root, []] if url_parts.empty?
  if child = root.children.find_by_url_part(url_parts.first)
    return self.find_by_url(url_parts[1..-1], child)
  end
  return [root, url_parts]
end

.group_by_parent_idObject

returns an Array of hashes

Returns

<Array>

Array of hashes grouped by parent_id



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'app/models/skyline/page.rb', line 73

def group_by_parent_id
  out = {}
  pages = self.connection.select_all("
    SELECT page.id,
           page.parent_id,
           IF(ISNULL(data.navigation_title) || data.navigation_title='', data.title, data.navigation_title) AS title,
           page.locked,
           page.published_publication_id,
           page.default_variant_id,
           published_publication.variant_id AS published_publication_variant_id,
           published_publication.version AS published_publication_version,
           default_variant.version AS default_variant_version
    FROM #{self.table_name} AS page
    LEFT JOIN skyline_page_data AS data ON data.id=page.default_variant_data_id
    LEFT JOIN skyline_article_versions AS published_publication ON published_publication.id=page.published_publication_id
    LEFT JOIN skyline_article_versions AS default_variant ON default_variant.id=default_variant_id
    WHERE page.type='Skyline::Page'
    ORDER BY page.position")

  pages.each do |o|
    class << o
      def id; self["id"].to_i; end
      def parent_id; self["parent_id"].blank? ? nil : self["parent_id"].to_i; end
      def title; self["title"].blank? ? "n/a" : self["title"]; end
      def published?; self["published_publication_id"].present?; end
      def identical_to_publication?
        self["published_publication_variant_id"] == self["default_variant_id"] && self["published_publication_version"] == self["default_variant_version"]
      end
    end

    out[o.parent_id] ||= []
    out[o.parent_id] << o
  end
  out
end

build menu of certain level

Parameters

level<Integer>

level of the menu that has to be returned

nesting<Array>

the nesting of page starting with the root node

Returns

<Array>

an array of pages to display in the menu



120
121
122
123
124
125
126
# File 'app/models/skyline/page.rb', line 120

def menu(level = 1, nesting = nil)
  menu = []
  menu << {:page => self.root, :children => []} if level == 1 && self.root.in_navigation?
  nesting ||= [self.root]
  menu += nesting[level-1].menu if nesting[level-1]
  menu
end

.reorder(pages) ⇒ Object



138
139
140
141
142
143
# File 'app/models/skyline/page.rb', line 138

def reorder(pages)
    return unless pages.kind_of?(Array)
    pages.each_with_index do |parent_id, position|
      self.connection.execute("UPDATE #{self.table_name} SET position=#{position.to_i} WHERE id=#{parent_id.to_i}")
    end     
end

.right_prefixObject



65
66
67
# File 'app/models/skyline/page.rb', line 65

def right_prefix
  "page"
end

.rootObject



109
110
111
# File 'app/models/skyline/page.rb', line 109

def root
  self.find_by_parent_id(nil)
end

Instance Method Details

#create_new!(position) ⇒ Object

create a new page on position

Parameters

position<Symbol>

Can either be :below, :above or :in

Returns

<Page>

The new page



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'app/models/skyline/page.rb', line 193

def create_new!(position)
   page = Skyline::Page.new
   
   self.class.transaction do
    case position
    when :below
      page.parent = self.parent
      page.position = self.position + 1
      self.class.connection.execute("UPDATE #{self.class.table_name} SET position=position+1 WHERE #{self.parent ? "parent_id=#{self.parent.id}" : 'parent_id IS NULL'} AND position>#{self.position}")
    when :above
      page.parent = self.parent
      page.position = self.position
      self.class.connection.execute("UPDATE #{self.class.table_name} SET position=position+1 WHERE #{self.parent ? "parent_id=#{self.parent.id}" : 'parent_id IS NULL'} AND position>=#{self.position}")
    when :in
      page.parent = self
    end    
    
    page.sites << self.site
    page.save
  end
  page
end

#destroyable?Boolean

Returns:

  • (Boolean)


178
179
180
# File 'app/models/skyline/page.rb', line 178

def destroyable?
  !self.persistent? && self.children.empty? && self.published_publication == nil
end

#keep_history?Boolean

Returns:

  • (Boolean)


182
183
184
# File 'app/models/skyline/page.rb', line 182

def keep_history?
  Skyline::Configuration.enable_publication_history
end


174
175
176
# File 'app/models/skyline/page.rb', line 174

def menu
 self.children.collect{|child| {:page => child, :children => child.menu} if child.published_publication_data.andand.in_navigation?}.compact
end

#move_behind=(parent_id) ⇒ Object



216
217
218
219
# File 'app/models/skyline/page.rb', line 216

def move_behind=(parent_id)
 @do_move_behind = true
 @move_behind = parent_id
end

#nestingObject



150
151
152
# File 'app/models/skyline/page.rb', line 150

def nesting
  self.root? ? [self] : self.parent.nesting + [self]
end

#parentsObject



155
156
157
# File 'app/models/skyline/page.rb', line 155

def parents
  self.nesting.dup[0..-2]
end

#pathObject

Returns the path (url excluding current url_path), with trailing / for the root page: nil pages directly in the root: / other pages, ie: /a/b/c/



163
164
165
166
167
# File 'app/models/skyline/page.rb', line 163

def path
  return nil if self.root?
  path = "/" + self.parents[1..-1].collect{|page| page.url_part}.join("/") + "/"
  path.squeeze("/")
end

#root?Boolean

Returns:

  • (Boolean)


146
147
148
# File 'app/models/skyline/page.rb', line 146

def root?
  !self.parent
end

#urlObject



169
170
171
172
# File 'app/models/skyline/page.rb', line 169

def url
  return "/" if self.root?
  self.path + self.url_part
end