Class: Post
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Post
- 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
- .archive_dates(array) ⇒ Object
- .author_ids ⇒ Object
- .creation_parameters ⇒ Object
- .entity_parameters ⇒ Object
- .items_per_page ⇒ Object
- .page_for_administration(page = 1, filter = {}) ⇒ Object
- .page_for_owner(user, page = 1) ⇒ Object
- .page_for_visitors(page = 1, per_page = Post.items_per_page) ⇒ Object
Instance Method Summary collapse
-
#author!(default_name = '') ⇒ Object
Get the most suitable author name for post.
- #cache_tags! ⇒ Object
- #commentable_by?(user) ⇒ Boolean
-
#editable_by?(user) ⇒ Boolean
deprecated
Deprecated.
use component handler
-
#editorial_member ⇒ EditorialMember|nil
Get editorial member instance for this post.
- #enclosures ⇒ Object
- #has_image_data? ⇒ Boolean
- #has_source_data? ⇒ Boolean
-
#lead! ⇒ Object
Lead or the first passage of body.
-
#linked_posts(quantity = 5) ⇒ Object
List of linked posts for visitors.
- #locale ⇒ Object
- #post_category ⇒ Object
- #similar_posts(quantity = 3, excluded = []) ⇒ Object
- #tagged_path(tag_name, locale = I18n.default_locale) ⇒ Object
- #tags_string ⇒ Object
- #tags_string=(input) ⇒ Object
- #translations ⇒ Object
- #url(locale = I18n.default_locale) ⇒ Object
- #visible_to_visitors? ⇒ Boolean
Class Method Details
.archive_dates(array) ⇒ Object
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_ids ⇒ Object
136 137 138 |
# File 'app/models/post.rb', line 136 def self. visible.pluck(:user_id).uniq end |
.creation_parameters ⇒ Object
132 133 134 |
# File 'app/models/post.rb', line 132 def self.creation_parameters entity_parameters + %i[post_type_id] end |
.entity_parameters ⇒ Object
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] = %i[author_name author_title author_url translator_name] main_data + image_data + + + flags_data end |
.items_per_page ⇒ Object
128 129 130 |
# File 'app/models/post.rb', line 128 def self.items_per_page 12 end |
.page_for_administration(page = 1, filter = {}) ⇒ Object
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
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
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 (default_name = '') return default_name unless show_owner? if .blank? user.profile_name else end end |
#cache_tags! ⇒ Object
271 272 273 |
# File 'app/models/post.rb', line 271 def update! tags_cache: .order('slug asc').map(&:name) end |
#commentable_by?(user) ⇒ 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
245 246 247 |
# File 'app/models/post.rb', line 245 def editable_by?(user) Biovision::Components::BaseComponent.handler('posts', user).editable?(self) end |
#editorial_member ⇒ EditorialMember|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
194 195 196 197 198 |
# File 'app/models/post.rb', line 194 def editorial_member return unless show_owner? && .blank? EditorialMember.owned_by(user).first end |
#enclosures ⇒ Object
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
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
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
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 |
#locale ⇒ Object
231 232 233 |
# File 'app/models/post.rb', line 231 def locale language&.code.to_s end |
#post_category ⇒ Object
227 228 229 |
# File 'app/models/post.rb', line 227 def post_category post_categories.first end |
#similar_posts(quantity = 3, excluded = []) ⇒ Object
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: post_category_ids }).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
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_string ⇒ Object
257 258 259 |
# File 'app/models/post.rb', line 257 def .ordered_by_slug.map(&:name).join(', ') end |
#tags_string=(input) ⇒ Object
262 263 264 265 266 267 268 269 |
# File 'app/models/post.rb', line 262 def (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 end |
#translations ⇒ Object
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
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
239 240 241 |
# File 'app/models/post.rb', line 239 def visible_to_visitors? visible? && !deleted? && approved? end |