Class: Page

Inherits:
Forest::ApplicationRecord show all
Includes:
Blockable, Statusable
Defined in:
app/models/page.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Forest::ApplicationRecord

cache_key, cache_key_name, csv_columns, #expire_cache_key, expire_cache_key, statusable?, #statusable?, to_csv_template, #to_label, #to_select2_response, #to_select2_selection, versionable, #versionable

Class Method Details

.resource_descriptionObject



32
33
34
# File 'app/models/page.rb', line 32

def self.resource_description
  "Pages offer a flexible and modular way to present information."
end

Instance Method Details

#all_associated_pagesObject



86
87
88
# File 'app/models/page.rb', line 86

def all_associated_pages
  @_all_associated_pages ||= page_ancestors.concat(page_ancestors.each.collect(&:page_descendents)).flatten
end

#descendent_of?(page_or_page_slug) ⇒ Boolean



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

def descendent_of?(page_or_page_slug)
  if page_or_page_slug.is_a?(Page)
    page_ancestors.any? { |p| p == page_or_page_slug }
  else
    page_ancestors.any? { |p| p.slug == page_or_page_slug }
  end
end

#generate_slugObject



36
37
38
# File 'app/models/page.rb', line 36

def generate_slug
  self.slug = title.parameterize if self.slug.blank?
end

#page_ancestorsObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/models/page.rb', line 44

def page_ancestors
  # TODO: these recursive function aren't performant and should be cached in the view
  @_page_ancestors ||= begin
    ancestors ||= []
    page = self
    parent = page.parent_page unless page.parent_page == page
    while parent
      ancestors << parent
      page = parent
      parent = page.try :parent_page
    end
    ancestors
  end
end

#page_descendentsObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'app/models/page.rb', line 59

def page_descendents
  # TODO: these recursive function aren't performant and should be cached in the view
  @_page_descendents ||= begin
    descendents = []
    children = self.immediate_children

    while children.present?
      descendents.concat children
      children = children.collect(&:immediate_children).reject(&:blank?).flatten
    end

    descendents
  end
end

#page_rootObject



74
75
76
# File 'app/models/page.rb', line 74

def page_root
  @_page_root ||= page_ancestors.find { |p| p.parent_page_id.nil? }
end

#remove_page_hierarchy!Object



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

def remove_page_hierarchy!
  pages_to_touch = Page.where(id: [self.id, *self.page_descendents.collect(&:id)])
  self.immediate_children.update_all(parent_page_id: nil)
  pages_to_touch.update_all(updated_at: DateTime.now)
end

#select2_formatObject



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

def select2_format
  ((page_ancestors.length + 1).times.collect{}.join('&mdash; ') + title).as_json
end

#to_friendly_paramObject



40
41
42
# File 'app/models/page.rb', line 40

def to_friendly_param
  path
end

#typical_media_itemsObject

Returns a collection of media items that represent the basic images on the page. Override this method in your host app if your pages often contain images stored under other attribute names.



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'app/models/page.rb', line 102

def typical_media_items
  media_item_ids = []
  media_item_ids.push(featured_image_id) if featured_image_id.present?

  blocks.each do |b|
    media_item_ids.push(b.media_item_id) if b.try(:media_item_id).present?
    media_item_ids.concat(b.media_item_ids) if b.try(:media_item_ids).present?
  end

  if media_item_ids.present?
    MediaItem.where(id: media_item_ids.uniq)
  else
    MediaItem.none
  end
end