Class: Post

Inherits:
ApplicationRecord show all
Includes:
Checkable, CommentableItem, Elasticsearch::Model, Elasticsearch::Model::Callbacks, HasOwner, Toggleable, VotableItem
Defined in:
app/models/post.rb

Overview

Post entry

Constant Summary collapse

ALT_LIMIT =
255
BODY_LIMIT =
16_777_215
IMAGE_NAME_LIMIT =
500
LEAD_LIMIT =
5000
META_LIMIT =
250
SLUG_LIMIT =
200
SLUG_PATTERN =
/\A[a-z0-9][-_.a-z0-9]*[a-z0-9]\z/.freeze
SLUG_PATTERN_HTML =
'^[a-zA-Z0-9][-_.a-zA-Z0-9]*[a-zA-Z0-9]$'
TIME_RANGE =
(0..1440).freeze
TITLE_LIMIT =
255
URL_PATTERN =
%r{https?://([^/]+)/?.*}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.archive_dates(array) ⇒ Object

Parameters:

  • array (Array)


141
142
143
144
145
# File 'app/models/post.rb', line 141

def self.archive_dates(array)
  dates = Hash.new { |h, k| h[k] = Hash.new { [] } }
  array.each { |date| dates[date.year][date.month] <<= date.day }
  dates
end

.author_idsObject



136
137
138
# File 'app/models/post.rb', line 136

def self.author_ids
  visible.pluck(:user_id).uniq
end

.creation_parametersObject



132
133
134
# File 'app/models/post.rb', line 132

def self.creation_parameters
  entity_parameters + %i[post_type_id]
end

.entity_parametersObject



118
119
120
121
122
123
124
125
126
# File 'app/models/post.rb', line 118

def self.entity_parameters
  main_data = %i[body language_id lead original_title post_layout_id publication_time region_id slug title]
  image_data = %i[image image_alt_text image_source_link image_source_name image_name]
   = %i[rating source_name source_link meta_title meta_description meta_keywords time_required]
  flags_data = %i[allow_comments allow_votes explicit show_owner visible translation]
  author_data = %i[author_name author_title author_url translator_name]

  main_data + image_data +  + author_data + flags_data
end

.items_per_pageObject



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

def self.items_per_page
  12
end

.page_for_administration(page = 1, filter = {}) ⇒ Object

Parameters:

  • page (Integer) (defaults to: 1)
  • filter (Hash) (defaults to: {})


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

def self.page_for_administration(page = 1, filter = {})
  filtered(filter).list_for_administration.page(page)
end

.page_for_owner(user, page = 1) ⇒ Object

Parameters:

  • user (User)
  • page (Integer) (defaults to: 1)


114
115
116
# File 'app/models/post.rb', line 114

def self.page_for_owner(user, page = 1)
  list_for_owner(user).page(page)
end

.page_for_visitors(page = 1, per_page = Post.items_per_page) ⇒ Object

Parameters:

  • page (Integer) (defaults to: 1)
  • per_page (Integer) (defaults to: Post.items_per_page)


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

def self.page_for_visitors(page = 1, per_page = Post.items_per_page)
  list_for_visitors.page(page).per(per_page)
end

Instance Method Details

#author!(default_name = '') ⇒ Object

Get the most suitable author name for post



159
160
161
162
163
164
165
166
167
# File 'app/models/post.rb', line 159

def author!(default_name = '')
  return default_name unless show_owner?

  if author_name.blank?
    user.profile_name
  else
    author_name
  end
end

#cache_tags!Object



271
272
273
# File 'app/models/post.rb', line 271

def cache_tags!
  update! tags_cache: .order('slug asc').map(&:name)
end

#commentable_by?(user) ⇒ Boolean

Returns:

  • (Boolean)


275
276
277
# File 'app/models/post.rb', line 275

def commentable_by?(user)
  allow_comments? && !user.nil?
end

#editable_by?(user) ⇒ Boolean

Deprecated.

use component handler

Parameters:

  • user (User)

Returns:

  • (Boolean)


245
246
247
# File 'app/models/post.rb', line 245

def editable_by?(user)
  Biovision::Components::BaseComponent.handler('posts', user).editable?(self)
end

#editorial_memberEditorialMember|nil

Get editorial member instance for this post

If user can be shown and is member of editorial, this method returns instance of [EditorialMember] through user for this post of nil otherwise

Returns:



194
195
196
197
198
# File 'app/models/post.rb', line 194

def editorial_member
  return unless show_owner? && author_name.blank?

  EditorialMember.owned_by(user).first
end

#enclosuresObject



182
183
184
185
186
# File 'app/models/post.rb', line 182

def enclosures
  body.scan(/<img[^>]+>/).map do |image|
    image.scan(/src="([^"]+)"/)[0][0]
  end
end

#has_image_data?Boolean

Returns:

  • (Boolean)


249
250
251
# File 'app/models/post.rb', line 249

def has_image_data?
  !image_name.blank? || !image_source_name.blank? || !image_source_link.blank?
end

#has_source_data?Boolean

Returns:

  • (Boolean)


253
254
255
# File 'app/models/post.rb', line 253

def has_source_data?
  !source_name.blank? || !source_link.blank?
end

#lead!Object

Lead or the first passage of body



148
149
150
151
152
153
154
155
156
# File 'app/models/post.rb', line 148

def lead!
  if lead.blank?
    pattern = %r{<p>(.+?)</p>}
    passage = body.match(pattern)
    (passage.nil? ? body.gsub(/<[^>]+>/, '') : passage[1]).to_s[0..499]
  else
    lead
  end
end

#linked_posts(quantity = 5) ⇒ Object

List of linked posts for visitors

Parameters:

  • quantity (Integer) (defaults to: 5)


203
204
205
206
207
208
209
210
211
212
# File 'app/models/post.rb', line 203

def linked_posts(quantity = 5)
  result = []
  post_links.ordered_by_priority.first(quantity).each do |link|
    result << link.other_post if link.other_post.visible_to_visitors?
  end
  excluded = result.map(&:id)
  delta = quantity - result.count
  result += similar_posts(delta, excluded) if result.count < quantity
  result
end

#localeObject



231
232
233
# File 'app/models/post.rb', line 231

def locale
  language&.code.to_s
end

#post_categoryObject



227
228
229
# File 'app/models/post.rb', line 227

def 
  post_categories.first
end

#similar_posts(quantity = 3, excluded = []) ⇒ Object

Parameters:

  • quantity (Integer) (defaults to: 3)
  • excluded (Array) (defaults to: [])


216
217
218
219
220
221
222
223
224
225
# File 'app/models/post.rb', line 216

def similar_posts(quantity = 3, excluded = [])
  result = []

  collection = Post.joins(:post_post_categories).where(post_post_categories: { post_category_id:  }).distinct.exclude_ids(excluded + [id])
  collection.visible.popular.first(quantity).each do |post|
    result << post
  end

  result
end

#tagged_path(tag_name, locale = I18n.default_locale) ⇒ Object

Parameters:

  • tag_name (String)
  • locale (Symbol) (defaults to: I18n.default_locale)


177
178
179
180
# File 'app/models/post.rb', line 177

def tagged_path(tag_name, locale = I18n.default_locale)
  prefix = locale.nil? || locale == I18n.default_locale ? '' : "/#{locale}"
  "#{prefix}/#{post_type.url_part}/tagged/#{CGI.escape(tag_name)}"
end

#tags_stringObject



257
258
259
# File 'app/models/post.rb', line 257

def tags_string
  .ordered_by_slug.map(&:name).join(', ')
end

#tags_string=(input) ⇒ Object

Parameters:

  • input (String)


262
263
264
265
266
267
268
269
# File 'app/models/post.rb', line 262

def tags_string=(input)
  list = []
  input.split(/,\s*/).reject(&:blank?).each do |tag_name|
    list << PostTag.match_or_create_by_name(post_type_id, tag_name.squish)
  end
  self. = list.uniq
  cache_tags!
end

#translationsObject



235
236
237
# File 'app/models/post.rb', line 235

def translations
  post_translations.each.map { |l| [l.language.code, l.translated_post] }.to_h
end

#url(locale = I18n.default_locale) ⇒ Object

Parameters:

  • locale (Symbol) (defaults to: I18n.default_locale)


170
171
172
173
# File 'app/models/post.rb', line 170

def url(locale = I18n.default_locale)
  prefix = locale.nil? || locale == I18n.default_locale ? '' : "/#{locale}"
  "#{prefix}/#{post_type.url_part}/#{id}-#{slug}"
end

#visible_to_visitors?Boolean

Returns:

  • (Boolean)


239
240
241
# File 'app/models/post.rb', line 239

def visible_to_visitors?
  visible? && !deleted? && approved?
end