Class: Integral::Post

Inherits:
ApplicationRecord show all
Extended by:
FriendlyId
Includes:
ActionView::Helpers::DateHelper
Defined in:
app/models/integral/post.rb

Overview

Represents a user post

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.available_statuses(opts = { reverse: false }) ⇒ Array

Returns containing available human readable statuses against there numeric value.

Returns:

  • (Array)

    containing available human readable statuses against there numeric value



42
43
44
45
46
47
48
49
# File 'app/models/integral/post.rb', line 42

def self.available_statuses(opts = { reverse: false })
  statuses = [
    [I18n.t('integral.records.status.draft'), 0],
    [I18n.t('integral.records.status.published'), 1]
  ]

  opts[:reverse] ? statuses.each(&:reverse!) : statuses
end

.listable_optionsHash

Returns listable options to be used within a RecordSelector widget.

Returns:

  • (Hash)

    listable options to be used within a RecordSelector widget



93
94
95
96
97
98
99
100
# File 'app/models/integral/post.rb', line 93

def self.listable_options
  {
    icon: 'rss',
    record_title: I18n.t('integral.backend.record_selector.posts.record'),
    selector_path: Engine.routes.url_helpers.backend_posts_path,
    selector_title: I18n.t('integral.backend.record_selector.posts.title')
  }
end

Instance Method Details

#increment_count!(ip_address) ⇒ Object

Increments the view count of the post if a PostViewing is successfully added

Parameters:

  • ip_address (String)

    Viewers IP address



54
55
56
# File 'app/models/integral/post.rb', line 54

def increment_count!(ip_address)
  increment!(:view_count) if PostViewing.add(self, ip_address)
end

#tag_contextString

Returns Current tag context.

Returns:

  • (String)

    Current tag context



103
104
105
# File 'app/models/integral/post.rb', line 103

def tag_context
  status
end

#tagsArray

Returns ActsAsTaggableOn::Tag tags associated with this post.

Returns:

  • (Array)

    ActsAsTaggableOn::Tag tags associated with this post



108
109
110
# File 'app/models/integral/post.rb', line 108

def tags
  tags_on(tag_context)
end

#to_cardHash

Returns the instance as a card.

Returns:

  • (Hash)

    the instance as a card



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'app/models/integral/post.rb', line 72

def to_card
  image_url = featured_image.file.url if featured_image
  {
    image: image_url,
    description: title,
    url: url,
    attributes: [
      { key: I18n.t('integral.records.attributes.author'), value: author.name },
      { key: I18n.t('integral.records.attributes.views'), value: view_count },
      { 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_itemHash

Returns the instance as a list item.

Returns:

  • (Hash)

    the instance as a list item



59
60
61
62
63
64
65
66
67
68
69
# File 'app/models/integral/post.rb', line 59

def to_list_item
  subtitle = published_at.present? ? I18n.t('integral.blog.posted_ago', time: time_ago_in_words(published_at)) : I18n.t('integral.users.status.draft')
  {
    id: id,
    title: title,
    subtitle: subtitle,
    description: description,
    image: featured_image,
    url: Integral::Engine.routes.url_helpers.post_url(self)
  }
end

#urlString

Returns URL to frontend route.

Returns:

  • (String)

    URL to frontend route



88
89
90
# File 'app/models/integral/post.rb', line 88

def url
  Integral::Engine.routes.url_helpers.post_url(self)
end