Class: Integral::Page
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Integral::Page
- 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
-
.available_templates ⇒ Array
Contains available template label and key pairs.
-
.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.
Class Method Details
.available_templates ⇒ Array
Returns 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_options ⇒ Hash
Returns 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. { 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
#ancestors ⇒ Array
Returns 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_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.
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 |
#breadcrumbs ⇒ Array
Returns 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 crumb = [{ path: path, title: title }] parent ? parent..concat(crumb) : crumb end |
#to_card ⇒ Hash
Returns 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.[: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.
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.[:host]}#{path}" } end |