Class: Snapshot::Article

Inherits:
ApplicationRecord
  • Object
show all
Defined in:
app/models/postnhost/snapshot/article.rb

Constant Summary collapse

MAX_SUGGESTIONS =
3
SCALAR_ATTRIBUTES =
%i[
  language_id title title_tag og_title schema_headline schema_article_type meta_description
  custom_excerpt auto_excerpt use_excerpt_as_meta_description content slug cover_image_alt top_pick
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.matching(query) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/models/postnhost/snapshot/article.rb', line 47

def self.matching(query)
  return all if query.blank?

  where(
    <<~SQL.squish,
      LOWER(postnhost_article_snapshots.title) LIKE :search OR
      LOWER(postnhost_article_snapshots.content) LIKE :search OR
      LOWER(postnhost_article_snapshots.meta_description) LIKE :search
    SQL
    search: "%#{sanitize_sql_like(query.downcase)}%"
  )
end

Instance Method Details

#cover_imageObject



64
65
66
# File 'app/models/postnhost/snapshot/article.rb', line 64

def cover_image
  @cover_image ||= PublishedCoverImage.new(article_id:, identifier: cover_image_identifier)
end

#cover_image?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'app/models/postnhost/snapshot/article.rb', line 68

def cover_image?
  cover_image.present?
end

#differs_from?(draft) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
75
76
77
78
# File 'app/models/postnhost/snapshot/article.rb', line 72

def differs_from?(draft)
  scalar_changes?(draft) ||
    cover_image_identifier != draft.cover_image.identifier.presence ||
    category_ids.sort != draft.category_ids.sort ||
    snapshot_author_ids != draft..order(:position, :id).pluck(:user_id) ||
    snapshot_suggestion_ids != draft.article_suggestions.order(:position, :id).pluck(:suggested_article_id)
end

#published?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'app/models/postnhost/snapshot/article.rb', line 60

def published?
  true
end

#suggestions_for(language:, limit: MAX_SUGGESTIONS) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'app/models/postnhost/snapshot/article.rb', line 80

def suggestions_for(language:, limit: MAX_SUGGESTIONS)
  @suggestions_by_language_and_limit ||= {}
  @suggestions_by_language_and_limit[[language.id, limit]] ||= begin
    manual = manual_suggestions(limit:)
    if manual.size >= limit
      preload_suggestion_associations(manual)
    else
      remaining = limit - manual.size
      candidates = automatic_suggestions(language:, exclude_ids: manual.map(&:article_id), limit: remaining * 3)
      suggestions = manual + candidates.sample(remaining, random: Random.new(article_id))
      preload_suggestion_associations(suggestions)
    end
  end
end