Class: Integral::Page

Inherits:
ApplicationRecord show all
Defined in:
app/models/integral/page.rb

Overview

Represents a public viewable page

Constant Summary collapse

PATH_REGEX =

Validates format of a path Examples: Good: /foo, /foo/bar, /123/456 Bad: //, foo, /foo bar, /foo?y=123, /foo$

/\A\/[\/.a-zA-Z0-9-]+\z/

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.available_templatesArray

Returns contains available template label and key pairs.

Returns:

  • (Array)

    contains available template label and key pairs



88
89
90
91
92
93
94
95
96
97
98
# File 'app/models/integral/page.rb', line 88

def self.available_templates
  templates = [:default]
  templates.concat Integral.additional_page_templates

  available_templates = []
  templates.each do |template|
    available_templates << [I18n.t("integral.backend.pages.templates.#{template}"), template]
  end

  available_templates
end

.listable_optionsHash

Returns listable options to be used within a RecordSelector widget.

Returns:

  • (Hash)

    listable options to be used within a RecordSelector widget



63
64
65
66
67
68
69
70
# File 'app/models/integral/page.rb', line 63

def self.listable_options
  {
    icon: 'file',
    record_title: I18n.t('integral.backend.record_selector.pages.record'),
    selector_path: Engine.routes.url_helpers.backend_pages_path,
    selector_title: I18n.t('integral.backend.record_selector.pages.title')
  }
end

Instance Method Details

#ancestorsArray

Returns list of Pages which parent the instance, used for breadcrumbing.

Returns:

  • (Array)

    list of Pages which parent the instance, used for breadcrumbing



111
112
113
114
115
116
117
118
119
120
121
122
# File 'app/models/integral/page.rb', line 111

def ancestors
  children = Page.where(parent_id: id)

  return [] if children.empty?

  descendants = children.to_a
  children.each do |page|
    descendants.concat page.ancestors
  end

  descendants
end

#available_parentsObject

Return all available parents TODO: Update parent behaviour What happens when parent is deleted or goes from published to draft. Possibly allow it but show warnings on the dashboard.



41
42
43
44
45
46
47
48
# File 'app/models/integral/page.rb', line 41

def available_parents
  if persisted?
    unavailable_ids = ancestors.map(&:id)
    unavailable_ids << id
  end

  Page.published.where.not(id: unavailable_ids).order(:title)
end

Returns containing all page breadcrumbs within a hash made up of path and title.

Returns:

  • (Array)

    containing all page breadcrumbs within a hash made up of path and title



101
102
103
104
105
106
107
108
# File 'app/models/integral/page.rb', line 101

def breadcrumbs
  crumb = [{
    path: path,
    title: title
  }]

  parent ? parent.breadcrumbs.concat(crumb) : crumb
end

#to_cardHash

Returns the instance as a card.

Returns:

  • (Hash)

    the instance as a card



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

def to_card
  # subtitle = self.published_at.present? ? I18n.t('integral.blog.posted_ago', time: time_ago_in_words(self.published_at)) : I18n.t('integral.records.status.draft')
  # image_url = image.file.url(:medium) if image
  {
    # image: image_url,
    description: title,
    url: "#{Rails.application.routes.default_url_options[:host]}#{path}",
    attributes: [
      { key: I18n.t('integral.records.attributes.status'), value: I18n.t("integral.records.status.#{status}") },
      { key: I18n.t('integral.records.attributes.updated_at'), value: I18n.l(updated_at) }
    ]
  }
end

#to_list_itemHash

Returns the instance as a list item.

Returns:

  • (Hash)

    the instance as a list item



51
52
53
54
55
56
57
58
59
60
# File 'app/models/integral/page.rb', line 51

def to_list_item
  {
    id: id,
    title: title,
    # subtitle: '',
    description: description,
    image: image&.url,
    url: "#{Rails.application.routes.default_url_options[:host]}#{path}"
  }
end