Class: Virgo::Post
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Virgo::Post
show all
- Extended by:
- FriendlyId
- Includes:
- ActionView::Helpers::SanitizeHelper, Common::SlugHistory, Common::Uuid, Recommendations, Search
- Defined in:
- app/models/virgo/post.rb,
app/models/concerns/virgo/post/search.rb,
app/models/concerns/virgo/post/recommendations.rb
Defined Under Namespace
Modules: Recommendations, Search
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Instance Attribute Details
#ordered_tag_ids ⇒ Object
Returns the value of attribute ordered_tag_ids.
13
14
15
|
# File 'app/models/virgo/post.rb', line 13
def ordered_tag_ids
@ordered_tag_ids
end
|
Class Method Details
.cache_key ⇒ Object
213
214
215
|
# File 'app/models/virgo/post.rb', line 213
def self.cache_key
"posts-#{maximum(:updated_at).try(:to_i)}-#{all.size}"
end
|
.category_feature(category) ⇒ Object
114
115
116
|
# File 'app/models/virgo/post.rb', line 114
def self.category_feature(category)
category.posts.publicly_viewable.where(feature_on_category_page: true).order(publish_at: :asc).last
end
|
.front_page_feature ⇒ Object
118
119
120
|
# File 'app/models/virgo/post.rb', line 118
def self.front_page_feature
posts.publicly_viewable.where(feature_on_front_page: true).order(publish_at: :asc).last
end
|
.publish_scheduled! ⇒ Object
172
173
174
175
176
177
178
|
# File 'app/models/virgo/post.rb', line 172
def self.publish_scheduled!
candidates = Post.where.not(live: true).where("publish_at <= ?", Time.now.to_datetime)
candidates.each do |post|
post.update(live: true)
end
end
|
.status_names ⇒ Object
158
159
160
161
162
163
164
165
166
|
# File 'app/models/virgo/post.rb', line 158
def self.status_names
{
'Draft' => :draft,
'Assigned' => :assigned,
'Published' => :published,
'Killed' => :killed,
'Hidden' => :hidden
}
end
|
Instance Method Details
#apply_tag_ordering! ⇒ Object
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
|
# File 'app/models/virgo/post.rb', line 248
def apply_tag_ordering!
if ordered_tag_ids.present?
if ordered_tag_ids.first.is_a?(String)
cleaned = ordered_tag_ids.first.gsub("[", "").gsub("]", "").split(",").map(&:to_i)
ordered_tag_ids = cleaned
end
ordered_tag_ids.each_with_index do |tag_id, index|
existing = post_tags.find_by(tag_id: tag_id)
if existing
existing.update!(position: index)
else
tag = Tag.find_by(id: tag_id)
if tag
post_tags.create!(tag_id: tag_id, position: index)
end
end
end
post_tags.where.not(tag_id: ordered_tag_ids).map &:destroy
end
true
end
|
#assigned? ⇒ Boolean
134
135
136
|
# File 'app/models/virgo/post.rb', line 134
def assigned?
status == :assigned
end
|
#attempt_edit_lock(user) ⇒ Object
201
202
203
204
205
206
207
|
# File 'app/models/virgo/post.rb', line 201
def attempt_edit_lock(user)
if is_mid_edit? && editing_user != user
false
else
lock_for_editing!(user) if persisted?
end
end
|
#calc_popularity(opt = nil) ⇒ Object
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
|
# File 'app/models/virgo/post.rb', line 224
def calc_popularity(opt=nil)
unless publish_at.nil?
days_ago_max = (Time.now - Post.minimum(:publish_at))/86400
days_ago = (Time.now - publish_at)/86400
age_percentage = (1 - (days_ago/(days_ago_max || 1.0)))
age_percentage = 10 if publish_at.to_date == Date.today
age_factor = 0.1 + (0.9*age_percentage)
basis = opt == :increment ? (view_count + 1) : view_count
basis * age_factor
else
0
end
end
|
#description ⇒ Object
279
280
281
282
283
284
285
286
287
|
# File 'app/models/virgo/post.rb', line 279
def description
if meta_description_tag_value.present?
meta_description_tag_value
elsif excerpt.present?
view_help.truncate(excerpt, length: 160)
else
view_help.truncate(view_help.strip_tags(rendered_body || ''), length: 160)
end
end
|
168
169
170
|
# File 'app/models/virgo/post.rb', line 168
def
"post_#{id}"
end
|
#draft? ⇒ Boolean
130
131
132
|
# File 'app/models/virgo/post.rb', line 130
def draft?
status == :draft
end
|
#edit_lock_timeout ⇒ Object
209
210
211
|
# File 'app/models/virgo/post.rb', line 209
def edit_lock_timeout
Rails.application.config.edit_lock_timeout
end
|
#has_citation? ⇒ Boolean
180
181
182
|
# File 'app/models/virgo/post.rb', line 180
def has_citation?
citation_name.present? && citation_url.present?
end
|
#hidden? ⇒ Boolean
146
147
148
|
# File 'app/models/virgo/post.rb', line 146
def hidden?
status == :hidden
end
|
#is_mid_edit? ⇒ Boolean
i.e. if the lock timeout is “15 seconds” but the post’s “editing_timestamp” has been bumped less than 15 seconds ago, we say the post is “mid-edit”
193
194
195
|
# File 'app/models/virgo/post.rb', line 193
def is_mid_edit?
editing_timestamp && editing_timestamp > edit_lock_timeout.ago
end
|
#killed? ⇒ Boolean
142
143
144
|
# File 'app/models/virgo/post.rb', line 142
def killed?
status == :killed
end
|
#lock_for_editing!(user) ⇒ Object
197
198
199
|
# File 'app/models/virgo/post.rb', line 197
def lock_for_editing!(user)
update!(editing_timestamp: Time.now.to_datetime, editing_user: user)
end
|
#page? ⇒ Boolean
150
151
152
|
# File 'app/models/virgo/post.rb', line 150
def page?
post_type == :page
end
|
#permalink ⇒ Object
122
123
124
|
# File 'app/models/virgo/post.rb', line 122
def permalink
urls.post_detail_url(self, protocol: 'https')
end
|
#post? ⇒ Boolean
154
155
156
|
# File 'app/models/virgo/post.rb', line 154
def post?
post_type == :post
end
|
#primary_category ⇒ Object
275
276
277
|
# File 'app/models/virgo/post.rb', line 275
def primary_category
categories.first
end
|
#published? ⇒ Boolean
138
139
140
|
# File 'app/models/virgo/post.rb', line 138
def published?
status == :published
end
|
#rendered_body ⇒ Object
126
127
128
|
# File 'app/models/virgo/post.rb', line 126
def rendered_body
@rendered_body ||= Shortcode.process(body || '').html_safe
end
|
#thumb_image ⇒ Object
289
290
291
292
293
294
295
|
# File 'app/models/virgo/post.rb', line 289
def thumb_image
if thumbnail_image.present?
thumbnail_image
else
featured_image
end
end
|
#track_view! ⇒ Object
217
218
219
220
221
222
|
# File 'app/models/virgo/post.rb', line 217
def track_view!
update_columns({
view_count: (view_count + 1),
popularity: calc_popularity(:increment)
})
end
|
#unscoped_versions ⇒ Object
need to explicitly unscope this because papertrail imposes a forward chronological order with a default scope
186
187
188
|
# File 'app/models/virgo/post.rb', line 186
def unscoped_versions
PaperTrail::Version.unscoped.where(item_id: id)
end
|