diff --git a/app/models/communication/website/github_file.rb b/app/models/communication/website/github_file.rb index f5da2f81eee59be25c49f2502fa7a366a645c137..11b428692fc6215520e806c6974678ef362e0c00 100644 --- a/app/models/communication/website/github_file.rb +++ b/app/models/communication/website/github_file.rb @@ -27,7 +27,7 @@ class Communication::Website::GithubFile < ApplicationRecord after_destroy :remove_from_github def publish - return unless github.valid? + return unless valid? && github.valid? add_to_batch(github) if github.commit_batch(github_commit_message) update_column :github_path, manifest_data[:generated_path] @@ -36,6 +36,7 @@ class Communication::Website::GithubFile < ApplicationRecord handle_asynchronously :publish, queue: 'default' def add_to_batch(github) + return unless valid? github.add_to_batch github_params add_media_to_batch(github) end @@ -120,4 +121,9 @@ class Communication::Website::GithubFile < ApplicationRecord item[:identifier] == manifest_identifier } end + + def valid? + return about.published? if about_type == 'Communication::Website::Post' + true + end end diff --git a/app/models/communication/website/post.rb b/app/models/communication/website/post.rb index e47443cfeb2f6fd029f4ada61c380a44a6e5c4d8..e3bc1d9be9e97568da4b0c59ca8bf2681b8517b3 100644 --- a/app/models/communication/website/post.rb +++ b/app/models/communication/website/post.rb @@ -55,6 +55,8 @@ class Communication::Website::Post < ApplicationRecord validates :title, presence: true validates :slug, uniqueness: { scope: :communication_website_id } + before_validation :set_published_at, if: :published_changed? + scope :ordered, -> { order(published_at: :desc, created_at: :desc) } scope :recent, -> { order(published_at: :desc).limit(5) } @@ -72,4 +74,8 @@ class Communication::Website::Post < ApplicationRecord def slug_unavailable?(slug) self.class.unscoped.where(communication_website_id: self.communication_website_id, slug: slug).where.not(id: self.id).exists? end + + def set_published_at + self.published_at = published? ? Time.zone.now : nil + end end