Class: Integral::Page

Inherits:
ApplicationRecord show all
Includes:
LazyContentable
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$

%r{\A/[/.a-zA-Z0-9-]+\z}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods included from LazyContentable

#editor_body

Methods inherited from ApplicationRecord

available_statuses

Class Method Details

.available_templatesArray

Returns contains available template label and key pairs.

Returns:

  • (Array)

    contains available template label and key pairs



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

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

.integral_iconObject



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

def self.integral_icon
  'file'
end

.listable_optionsHash

Returns listable options to be used within a RecordSelector widget.

Returns:

  • (Hash)

    listable options to be used within a RecordSelector widget



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

def self.listable_options
  {
    icon: 'file',
    record_title: I18n.t('integral.backend.record_selector.pages.record'),
    selector_path: Engine.routes.url_helpers.list_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



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

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.



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

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



118
119
120
121
122
123
124
125
# File 'app/models/integral/page.rb', line 118

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



90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'app/models/integral/page.rb', line 90

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



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

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