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

Class Method Details

.available_templatesArray

Returns contains available template label and key pairs.

Returns:

  • (Array)

    contains available template label and key pairs



95
96
97
98
99
100
101
102
103
104
105
# File 'app/models/integral/page.rb', line 95

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



70
71
72
73
74
75
76
77
# File 'app/models/integral/page.rb', line 70

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



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

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.



48
49
50
51
52
53
54
55
# File 'app/models/integral/page.rb', line 48

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



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

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



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'app/models/integral/page.rb', line 80

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



58
59
60
61
62
63
64
65
66
67
# File 'app/models/integral/page.rb', line 58

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