Class: Integral::Page

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

Instance Method Summary collapse

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



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?

  tags_sub = 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 (#{tags_sub}))) OR (lower(title) LIKE '%#{query}%' OR lower(path) LIKE '%#{query}%')")
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



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_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.



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

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



104
105
106
107
108
109
110
111
# File 'app/models/integral/page.rb', line 104

def breadcrumbs
  crumb = [{
    path: path,
    title: title
  }]

  parent ? parent.breadcrumbs.concat(crumb) : crumb
end

#tag_contextString

Returns Current tag context.

Returns:

  • (String)

    Current tag context



128
129
130
# File 'app/models/integral/page.rb', line 128

def tag_context
  "#{status}_#{locale}"
end

#tagsArray

Returns ActsAsTaggableOn::Tag tags associated with this post.

Returns:

  • (Array)

    ActsAsTaggableOn::Tag tags associated with this post



133
134
135
# File 'app/models/integral/page.rb', line 133

def tags
  tags_on(tag_context)
end

#to_list_itemHash

Returns the instance as a list item.

Returns:

  • (Hash)

    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&.attachment,
    url: "#{Rails.application.routes.default_url_options[:host]}#{path}"
  }
end