Class: Integral::Page
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Integral::Page
- Includes:
- BlockEditor::Listable
- 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|\A[\/]\z}.freeze
Class Method Summary collapse
-
.available_templates ⇒ Array
Contains available template label and key pairs.
-
.search(query) ⇒ Object
Scopes TODO: Must be a better way of doing this - We’re searching by title OR path OR tags.
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.
-
#tag_context ⇒ String
Current tag context.
-
#tags ⇒ Array
ActsAsTaggableOn::Tag tags associated with this post.
-
#to_list_item ⇒ Hash
The instance as a list item.
Methods inherited from ApplicationRecord
Class Method Details
.available_templates ⇒ Array
Returns contains available template label and key pairs.
91 92 93 94 95 96 97 98 99 100 101 |
# File 'app/models/integral/page.rb', line 91 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 |
.search(query) ⇒ Object
Scopes TODO: Must be a better way of doing this - We’re searching by title OR path OR tags
57 58 59 60 61 62 63 |
# File 'app/models/integral/page.rb', line 57 def self.search(query) return all if query.blank? = query.split.map {|term| "LOWER(\"tags\".\"name\") ILIKE '#{term}' ESCAPE '!'" }.join(" OR ") where("EXISTS (SELECT * FROM \"taggings\" WHERE \"taggings\".\"taggable_id\" = \"integral_pages\".\"id\" AND \"taggings\".\"taggable_type\" = 'Integral::Page' AND \"taggings\".\"tag_id\" IN (SELECT \"tags\".\"id\" FROM \"tags\" WHERE (#{}))) OR (lower(title) LIKE '%#{query}%' OR lower(path) LIKE '%#{query}%')") end |
Instance Method Details
#ancestors ⇒ Array
Returns list of Pages which parent the instance, used for breadcrumbing.
114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'app/models/integral/page.rb', line 114 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.
69 70 71 72 73 74 75 76 |
# File 'app/models/integral/page.rb', line 69 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.
104 105 106 107 108 109 110 111 |
# File 'app/models/integral/page.rb', line 104 def crumb = [{ path: path, title: title }] parent ? parent..concat(crumb) : crumb end |
#tag_context ⇒ String
Returns Current tag context.
128 129 130 |
# File 'app/models/integral/page.rb', line 128 def tag_context "#{status}_#{locale}" end |
#tags ⇒ Array
Returns ActsAsTaggableOn::Tag tags associated with this post.
133 134 135 |
# File 'app/models/integral/page.rb', line 133 def (tag_context) end |
#to_list_item ⇒ Hash
Returns the instance as a list item.
79 80 81 82 83 84 85 86 87 88 |
# File 'app/models/integral/page.rb', line 79 def to_list_item { id: id, title: title, # subtitle: '', description: description, image: image&., url: "#{Rails.application.routes.[:host]}#{path}" } end |