Class: Integral::Page
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Integral::Page
- 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
-
.available_templates ⇒ Array
Contains available template label and key pairs.
- .integral_icon ⇒ Object
-
.listable_options ⇒ Hash
Listable options to be used within a RecordSelector widget.
Instance Method Summary collapse
-
#ancestors ⇒ Array
List of Pages which parent the instance, used for breadcrumbing.
-
#available_parents ⇒ Object
Return all available parents TODO: Update parent behaviour What happens when parent is deleted or goes from published to draft.
-
#breadcrumbs ⇒ Array
Containing all page breadcrumbs within a hash made up of path and title.
-
#to_card ⇒ Hash
The instance as a card.
-
#to_list_item ⇒ Hash
The instance as a list item.
Methods included from LazyContentable
Methods inherited from ApplicationRecord
Class Method Details
.available_templates ⇒ Array
Returns 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_icon ⇒ Object
85 86 87 |
# File 'app/models/integral/page.rb', line 85 def self.integral_icon 'file' end |
.listable_options ⇒ Hash
Returns 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. { 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
#ancestors ⇒ Array
Returns 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_parents ⇒ Object
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 |
#breadcrumbs ⇒ Array
Returns 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 crumb = [{ path: path, title: title }] parent ? parent..concat(crumb) : crumb end |
#to_card ⇒ Hash
Returns 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.[: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_item ⇒ Hash
Returns 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.[:host]}#{path}" } end |