diff --git a/app/models/communication/website/git_file.rb b/app/models/communication/website/git_file.rb index 0a38acbe9b01ff83ec1b0ac33c4cab4516c01b73..e88ce09b89e8803acb9a9df2a071313a3c125800 100644 --- a/app/models/communication/website/git_file.rb +++ b/app/models/communication/website/git_file.rb @@ -45,12 +45,14 @@ class Communication::Website::GitFile < ApplicationRecord def should_update? !should_destroy? && ( - different_path? || different_sha? + different_path? || + different_sha? ) end def should_destroy? - will_be_destroyed || path.nil? + will_be_destroyed || + path.nil? end def path @@ -71,8 +73,16 @@ class Communication::Website::GitFile < ApplicationRecord protected # Real sha on the git repo + def git_sha_for(path) + website.git_repository.git_sha path + end + + def previous_git_sha + @previous_git_sha ||= git_sha_for(previous_path) + end + def git_sha - @git_sha ||= website.git_repository.git_sha previous_path + @git_sha ||= git_sha_for(path) end # Based on content, with the provider's algorithm (sha1 or sha256) @@ -81,14 +91,17 @@ class Communication::Website::GitFile < ApplicationRecord end def exists_on_git? - !git_sha.nil? + previous_git_sha.present? || # The file exists where it was last time + ( + previous_path.nil? && # Never saved in the database + git_sha.present? # but it exists in the git repo + ) end def synchronized_with_git? - exists_on_git? && - ( - git_sha == previous_sha || git_sha == previous_sha256 - ) + exists_on_git? && # File exists + previous_path == path && # at the same place + previous_git_sha == previous_sha # with the same content end def different_path? diff --git a/test/fixtures/communication/website/posts.yml b/test/fixtures/communication/website/posts.yml index 14b3a688e24252e2ba26b5f7cfd8e5358c0451f6..c2154ce20abbe871edfe13965a4d0f83f08efe5f 100644 --- a/test/fixtures/communication/website/posts.yml +++ b/test/fixtures/communication/website/posts.yml @@ -36,3 +36,5 @@ test_post: website: website_with_github title: Test slug: test + published: true + published_at: 2010-11-28 00:00:00 diff --git a/test/models/communication/website/git_file_test.rb b/test/models/communication/website/git_file_test.rb index 4f80fa1cba391474b02e72eb01f2cc67ab431bc1..bc94fbe1c2407a011308db2491ce5fc4a8c39879 100644 --- a/test/models/communication/website/git_file_test.rb +++ b/test/models/communication/website/git_file_test.rb @@ -23,14 +23,8 @@ require "test_helper" class Communication::Website::GitFileTest < ActiveSupport::TestCase - test "github should_create?" do + test "should_create? for a new file" do file = communication_website_git_files(:git_file_1) - # This is wrong! Fixtures must be prepared properly - assert_not file.should_create? - end - - test "github correct sha" do - file = communication_website_git_files(:git_file_1) - assert_equal file.sha, '5d387e7e2da68026aeb0cfc2e6a67f509a3e1ff6' + assert file.should_create? end end