diff --git a/app/models/communication/website.rb b/app/models/communication/website.rb index 91c21dd2cda91c29b63e377caed6c256df6d0402..31b134effe1672da24f45207acf0b84bb9b1fd71 100644 --- a/app/models/communication/website.rb +++ b/app/models/communication/website.rb @@ -106,6 +106,6 @@ class Communication::Website < ApplicationRecord end def github - @github ||= Github.with_site self + @github ||= Github.with_website self end end diff --git a/app/models/communication/website/post.rb b/app/models/communication/website/post.rb index c16e0a74fdfc5ccfd6d68109e8a18e7c39bbf639..72053a06e6b633f704d6cedad808b38d33fe8c7c 100644 --- a/app/models/communication/website/post.rb +++ b/app/models/communication/website/post.rb @@ -53,12 +53,12 @@ class Communication::Website::Post < ApplicationRecord foreign_key: 'communication_website_post_id', association_foreign_key: 'communication_website_category_id' - scope :ordered, -> { order(published_at: :desc, created_at: :desc) } - scope :recent, -> { order(published_at: :desc).limit(5) } - validates :title, presence: true validates :slug, uniqueness: { scope: :communication_website_id } + scope :ordered, -> { order(published_at: :desc, created_at: :desc) } + scope :recent, -> { order(published_at: :desc).limit(5) } + def github_path_generated "_posts/#{published_at.year}/#{published_at.strftime "%Y-%m-%d"}-#{slug}.html" end diff --git a/app/models/communication/website/with_media.rb b/app/models/communication/website/with_media.rb index 808ec780ae996b47d32fbb8d02ca3c16633f1fd1..3b354cdfc4c2c46e53659105b4053d405a5d7198 100644 --- a/app/models/communication/website/with_media.rb +++ b/app/models/communication/website/with_media.rb @@ -3,6 +3,7 @@ module Communication::Website::WithMedia included do after_save_commit :publish_media_to_github + after_destroy :remove_media_from_github end def active_storage_blobs @@ -15,4 +16,8 @@ module Communication::Website::WithMedia def publish_media_to_github active_storage_blobs.each { |blob| website.publish_blob(blob) } end + + def remove_media_from_github + active_storage_blobs.each { |blob| website.remove_blob(blob) } + end end diff --git a/app/models/communication/website/with_publishable_objects.rb b/app/models/communication/website/with_publishable_objects.rb index 52347ce3a209ff2608f647e2a57e8908eb957d12..98bb2b20e48d359fa461b9d504e38db55a4ce623 100644 --- a/app/models/communication/website/with_publishable_objects.rb +++ b/app/models/communication/website/with_publishable_objects.rb @@ -38,5 +38,23 @@ module Communication::Website::WithPublishableObjects end handle_asynchronously :publish_blob, queue: 'default' + def remove_object(object) + return unless github.valid? + if object.respond_to?(:github_path) + github_path = object.github_path + else + root_folder = "_#{object_model_name.pluralize.underscore}" + github_path = "#{root_folder}/#{object.id}.md" + end + github_commit_message = "[#{object_model_name}] Remove #{object.to_s}" + github.remove(github_path, github_commit_message) + end + + def remove_blob(blob) + return unless github.valid? + github_path = "_data/media/#{blob.id[0..1]}/#{blob.id}.yml" + github_commit_message = "[Medium] Remove ##{blob.id}" + github.remove(github_path, github_commit_message) + end end end diff --git a/app/models/concerns/with_github.rb b/app/models/concerns/with_github.rb index cc67a3b6db94ef6e545b20ded7e8357166053f47..ac068db7eed9fa3db0a19f70fffdff0da156baae 100644 --- a/app/models/concerns/with_github.rb +++ b/app/models/concerns/with_github.rb @@ -4,6 +4,7 @@ module WithGithub included do after_save_commit :publish_to_github after_touch :publish_to_github + after_destroy :remove_from_github end def force_publish! @@ -26,11 +27,15 @@ module WithGithub protected def github - @github ||= Github.with_site(website) + @github ||= Github.with_website(website) end def github_commit_message - "[#{self.class.name.demodulize}] Save #{ to_s }" + "[#{self.class.name.demodulize}] Save #{to_s}" + end + + def github_remove_commit_message + "[#{self.class.name.demodulize}] Remove #{to_s}" end def publish_to_github @@ -43,4 +48,9 @@ module WithGithub end end handle_asynchronously :publish_to_github, queue: 'default' + + def remove_from_github + return unless github.valid? + github.remove(github_path, github_remove_commit_message) + end end diff --git a/app/models/concerns/with_publication_to_websites.rb b/app/models/concerns/with_publication_to_websites.rb index f58d2d652924c6700185c572d5065400a5aa9254..337e6bb0d638e3f926a19c0c3265779d9e901193 100644 --- a/app/models/concerns/with_publication_to_websites.rb +++ b/app/models/concerns/with_publication_to_websites.rb @@ -2,15 +2,20 @@ module WithPublicationToWebsites extend ActiveSupport::Concern included do - after_save_commit :publish_to_every_websites + after_save_commit :publish_to_every_website + after_destroy :remove_from_every_website end protected - def publish_to_every_websites + def publish_to_every_website websites.each { |website| website.publish_object(self) } end + def remove_from_every_website + websites.each { |website| website.remove_object(self) } + end + # You can define a `github_path` method to re-define where to save the object markdown. # Check Communication::Website#publish_object for the default value. diff --git a/app/services/github.rb b/app/services/github.rb index c9729ee039e8fa1962f7766b8c08c1ea2d88ef27..84bb565b47b608d3b8c3dc01978b4c8bbbeb316a 100644 --- a/app/services/github.rb +++ b/app/services/github.rb @@ -1,8 +1,8 @@ class Github attr_reader :access_token, :repository - def self.with_site(site) - new site&.access_token, site&.repository + def self.with_website(website) + new website&.access_token, website&.repository end def initialize(access_token, repository) @@ -35,20 +35,6 @@ class Github false end - def send_file(attachment, path) - return if repository.blank? - commit_message = "[file] Save #{ path }" - path_without_slash = path[1..-1] - client.create_contents repository, - path_without_slash, - commit_message, - attachment.download, - sha: file_sha(path) - true - rescue - false - end - def add_to_batch( path: nil, previous_path: nil, data:) @@ -84,6 +70,13 @@ class Github @tree = nil end + def remove(path, commit_message) + client.delete_contents repository, path, commit_message, file_sha(path) + true + rescue + false + end + def read_file_at(path) data = client.content repository, path: path Base64.decode64 data.content