diff --git a/app/models/communication/website/git_file.rb b/app/models/communication/website/git_file.rb index 4cef3c3d00521f91174378570b745692c207b8e2..83607a85dba9224ae7f8ab8264d2f46e2dd155e1 100644 --- a/app/models/communication/website/git_file.rb +++ b/app/models/communication/website/git_file.rb @@ -25,8 +25,11 @@ class Communication::Website::GitFile < ApplicationRecord belongs_to :website, class_name: 'Communication::Website' belongs_to :about, polymorphic: true - def self.sync(website, object, identifier) + attr_accessor :will_be_destroyed + + def self.sync(website, object, identifier, destroy: false) git_file = where(website: website, about: object, identifier: identifier).first_or_create + git_file.will_be_destroyed = destroy website.git_repository.add_git_file git_file end @@ -43,7 +46,7 @@ class Communication::Website::GitFile < ApplicationRecord end def should_destroy? - path.nil? + will_be_destroyed || path.nil? end def path diff --git a/app/models/communication/website/page.rb b/app/models/communication/website/page.rb index 365463b5c4fa4c41190b7971ebd13893d1101900..abb50444ce5e347e34a4d564096e554d40ca2169 100644 --- a/app/models/communication/website/page.rb +++ b/app/models/communication/website/page.rb @@ -72,7 +72,7 @@ class Communication::Website::Page < ApplicationRecord scope :recent, -> { order(updated_at: :desc).limit(5) } def git_path_static - "content/pages/#{path}/_index.html".gsub(/\/+/, '/') + "content/pages/#{path}/_index.html".gsub(/\/+/, '/') if published end def git_dependencies_static diff --git a/app/models/communication/website/post.rb b/app/models/communication/website/post.rb index c852f337c6dd19eff4bce302c268bdaeb76e3790..ae04c83bcb7639df9574ef6c313355ab0b3fac66 100644 --- a/app/models/communication/website/post.rb +++ b/app/models/communication/website/post.rb @@ -68,7 +68,7 @@ class Communication::Website::Post < ApplicationRecord end def git_path_static - "content/posts/#{published_at.year}/#{published_at.strftime "%Y-%m-%d"}-#{slug}.html" + "content/posts/#{published_at.year}/#{published_at.strftime "%Y-%m-%d"}-#{slug}.html" if published_at end def git_dependencies_static diff --git a/app/models/concerns/with_git.rb b/app/models/concerns/with_git.rb index 2a495b996a79880d1a58941c921a205e20e30c84..f276cdb015a0e20223434fcc504952920cb9ca81 100644 --- a/app/models/concerns/with_git.rb +++ b/app/models/concerns/with_git.rb @@ -32,8 +32,18 @@ module WithGit end def destroy_and_sync - # TODO + destroy_from_git destroy + true + end + + def destroy_from_git + websites_with_fallback.each do |website| + identifiers.each do |identifier| + Communication::Website::GitFile.sync website, self, identifier, destroy: true + end + website.git_repository.sync! + end end def sync_with_git diff --git a/app/models/research/journal/article.rb b/app/models/research/journal/article.rb index 471619898b25d1859b2d4f873219867b121e5919..c3a6ca93859e3fe4263ec644258876da1e1d7a85 100644 --- a/app/models/research/journal/article.rb +++ b/app/models/research/journal/article.rb @@ -56,11 +56,7 @@ class Research::Journal::Article < ApplicationRecord end def git_path_static - if published_at - "content/articles/#{published_at.year}/#{published_at.strftime "%Y-%m-%d"}-#{slug}.html" - else - nil - end + "content/articles/#{published_at.year}/#{published_at.strftime "%Y-%m-%d"}-#{slug}.html" if published_at end def git_dependencies_static diff --git a/app/models/research/journal/volume.rb b/app/models/research/journal/volume.rb index 0537912b2f9c9fe85fdb2c36faec14f6a7eba934..90873539aabe364db41e93e02f0642933b6f8959 100644 --- a/app/models/research/journal/volume.rb +++ b/app/models/research/journal/volume.rb @@ -47,11 +47,7 @@ class Research::Journal::Volume < ApplicationRecord end def git_path_static - if published_at - "content/volumes/#{published_at.year}/#{published_at.strftime "%Y-%m-%d"}-#{slug}.html" - else - nil - end + "content/volumes/#{published_at.year}/#{published_at.strftime "%Y-%m-%d"}-#{slug}.html" if published_at end def git_dependencies_static diff --git a/app/services/git/repository.rb b/app/services/git/repository.rb index afbd697a9337b28c16027802edfd80d8a2987f93..41b23c809486c23048f2d0d3129ec28c723c6072 100644 --- a/app/services/git/repository.rb +++ b/app/services/git/repository.rb @@ -6,7 +6,10 @@ class Git::Repository end def add_git_file(git_file) - @commit_message = "[#{ git_file.about.class.name }] Save #{ git_file.about }" if git_files.empty? + if git_files.empty? + action = git_file.should_destroy? ? "Destroy" : "Save" + @commit_message = "[#{ git_file.about.class.name }] #{ action } #{ git_file.about }" + end git_files << git_file end