Class: Postnhost::Article

Inherits:
ApplicationRecord show all
Includes:
ImageUploadable, ExcerptOptimizable, PrimaryLanguageGuard
Defined in:
app/models/postnhost/article.rb

Defined Under Namespace

Modules: ImageUploadable

Constant Summary collapse

STATUSES =
%w[published draft scheduled].freeze
SORT_OPTIONS =
{
  "created_at_desc" => { column: "created_at", direction: :desc },
  "created_at_asc" => { column: "created_at", direction: :asc }
}.freeze
DEFAULT_SORT =
"created_at_desc"

Constants included from InlineImageUploadable

InlineImageUploadable::IMAGE_MAX_SIZE, InlineImageUploadable::SUPPORTED_CONTENT_TYPES

Constants included from ExcerptOptimizable

ExcerptOptimizable::CUSTOM_EXCERPT_MAX_CHARS

Class Method Summary collapse

Instance Method Summary collapse

Methods included from InlineImageUploadable

#upload_inline_image

Class Method Details

.normalize_sort(sort) ⇒ Object



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

def self.normalize_sort(sort)
  sort.to_s.presence_in(SORT_OPTIONS.keys) || DEFAULT_SORT
end

.normalize_status(status) ⇒ Object



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

def self.normalize_status(status)
  status.to_s.presence_in(STATUSES)
end

Instance Method Details

#published?Boolean

Returns:

  • (Boolean)


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

def published?
  article_snapshot.present?
end

#schedule_publication!(scheduled_time) ⇒ Object



77
78
79
80
81
82
83
# File 'app/models/postnhost/article.rb', line 77

def schedule_publication!(scheduled_time)
  with_lock do
    cancel_scheduled_job
    job = Postnhost::ScheduledArticleJob.set(wait_until: scheduled_time).perform_later(id)
    update!(scheduled_at: scheduled_time, scheduled_job_id: job.job_id)
  end
end

#sync_publication_schedule!Object



92
93
94
95
96
97
98
99
100
# File 'app/models/postnhost/article.rb', line 92

def sync_publication_schedule!
  return unless saved_change_to_scheduled_at?

  if scheduled_at.present?
    schedule_publication!(scheduled_at)
  else
    unschedule_publication!
  end
end

#unpublished_changes?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'app/models/postnhost/article.rb', line 73

def unpublished_changes?
  article_snapshot&.differs_from?(self) || false
end

#unschedule_publication!Object



85
86
87
88
89
90
# File 'app/models/postnhost/article.rb', line 85

def unschedule_publication!
  with_lock do
    cancel_scheduled_job
    update!(scheduled_at: nil, scheduled_job_id: nil)
  end
end