Class: Integral::PostDecorator

Inherits:
BaseDecorator show all
Defined in:
app/decorators/integral/post_decorator.rb

Overview

Page view-level logic

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.collection_decorator_classObject

Enables pagination



7
8
9
# File 'app/decorators/integral/post_decorator.rb', line 7

def self.collection_decorator_class
  PaginatingDecorator
end

Instance Method Details

#activity_url(activity_id) ⇒ String

Returns URL to backend activity.

Returns:

  • (String)

    URL to backend activity



90
91
92
93
94
95
96
# File 'app/decorators/integral/post_decorator.rb', line 90

def activity_url(activity_id)
  if Integral.blog_enabled?
    Integral::Engine.routes.url_helpers.activity_backend_post_url(object.id, activity_id)
  else
    ''
  end
end

#avatarString

Returns avatar image.

Returns:

  • (String)

    avatar image



40
41
42
43
# File 'app/decorators/integral/post_decorator.rb', line 40

def avatar
  avatar_url = user&.avatar&.url(:thumbnail)
  h.image_tag avatar_url, class: 'user-avatar' unless avatar_url.nil?
end

#backend_urlString

Returns URL to backend post page.

Returns:

  • (String)

    URL to backend post page



81
82
83
84
85
86
87
# File 'app/decorators/integral/post_decorator.rb', line 81

def backend_url
  if Integral.blog_enabled?
    Integral::Engine.routes.url_helpers.backend_post_url(object.id)
  else
    ''
  end
end

#bodyString

Returns formatted body.

Returns:

  • (String)

    formatted body



99
100
101
# File 'app/decorators/integral/post_decorator.rb', line 99

def body
  object.body&.html_safe
end

#header_tagsObject

Tags to be used within the header of an article to describe the subject



46
47
48
49
50
51
52
53
54
55
# File 'app/decorators/integral/post_decorator.rb', line 46

def header_tags
  return I18n.t('integral.posts.show.subtitle') if object.tags_on('published').empty?

  header_tags = ''
  object.tags_on('published').each_with_index do |tag, i|
    header_tags += tag.name
    header_tags += ' | ' unless i == object.tags_on('published').size - 1
  end
  header_tags
end

#image(size = :small, fallback = true) ⇒ Object

Image for the post if present. Otherwise returns default image



66
67
68
69
70
71
# File 'app/decorators/integral/post_decorator.rb', line 66

def image(size = :small, fallback = true)
  image = object&.image&.url(size)
  return image if image.present?

  h.image_url('integral/defaults/no_image_available.jpg') if fallback
end

#preview_image(size = :small) ⇒ Object

Preview image for the post if present. Otherwise returns featured image



58
59
60
61
62
63
# File 'app/decorators/integral/post_decorator.rb', line 58

def preview_image(size = :small)
  preview_image = object&.preview_image&.url(size)
  return preview_image if preview_image.present?

  image(size, false)
end

#published_atObject

Date the post was published



74
75
76
77
78
# File 'app/decorators/integral/post_decorator.rb', line 74

def published_at
  return I18n.l(object.published_at, format: :blog) if object.published?

  'Not yet published'
end

#to_json_ldHash

Returns JSON-LD representing the instance.

Returns:

  • (Hash)

    JSON-LD representing the instance



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/decorators/integral/post_decorator.rb', line 12

def to_json_ld
  {
    "@type": 'blogPosting',
    "mainEntityOfPage": Integral::Engine.routes.url_helpers.post_url(object),
    "headline": title,
    "description": description,
    "datePublished": object.published_at,
    "dateModified": object.updated_at,
    "author": {
      "@type": 'Person',
      "name": object.author&.name
    },
    "image": [
      preview_image(:large),
      image(:large)
    ],
    "publisher": {
      "@type": 'Organization',
      "name": Integral::Settings.website_title,
      "logo": {
        "@type": 'ImageObject',
        "url": h.image_url('logo.png')
      }
    }
  }
end