Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions app/models/communication/website.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,20 @@
# default_language_id :uuid not null, indexed
# deuxfleurs_access_key_id :string
# locked_by_job_id :uuid
# synchronization_locked_by_id :uuid indexed
# university_id :uuid not null, indexed
#
# Indexes
#
# index_communication_websites_on_about (about_type,about_id)
# index_communication_websites_on_default_language_id (default_language_id)
# index_communication_websites_on_university_id (university_id)
# index_communication_websites_on_about (about_type,about_id)
# index_communication_websites_on_default_language_id (default_language_id)
# index_communication_websites_on_synchronization_locked_by_id (synchronization_locked_by_id)
# index_communication_websites_on_university_id (university_id)
#
# Foreign Keys
#
# fk_rails_2b6d929310 (default_language_id => languages.id)
# fk_rails_49c2afa13d (synchronization_locked_by_id => users.id) ON DELETE => nullify
# fk_rails_bb6a496c08 (university_id => universities.id)
#
class Communication::Website < ApplicationRecord
Expand Down
21 changes: 21 additions & 0 deletions app/models/communication/website/with_git_repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ module Communication::Website::WithGitRepository
extend ActiveSupport::Concern

included do
belongs_to :synchronization_locked_by,
class_name: 'User',
optional: true

has_many :website_git_files,
class_name: 'Communication::Website::GitFile',
dependent: :destroy
Expand Down Expand Up @@ -31,6 +35,19 @@ def git_repository
@git_repository ||= Git::Repository.new self
end

def synchronization_locked?
synchronization_locked_by_id.present?
end

def lock_synchronization!(user)
update_column :synchronization_locked_by_id, user.id
end

def unlock_synchronization!
update_column :synchronization_locked_by_id, nil
sync_with_git if desynchronized_generated_git_files.any?
end

def repository_url
git_repository.url
end
Expand All @@ -44,11 +61,13 @@ def identify_git_files_safely
end

def sync_with_git
return if synchronization_locked?
update_column(:last_sync_at, Time.now)
Communication::Website::SyncWithGitJob.perform_later(id)
end

def sync_with_git_safely
return if synchronization_locked?
return unless git_repository.valid?
git_repository.git_files = git_files.generated
.desynchronized_until(last_sync_at)
Expand Down Expand Up @@ -102,10 +121,12 @@ def should_clean_on_git?
end

def update_theme_version
return if synchronization_locked?
Communication::Website::UpdateThemeVersionJob.perform_later(id)
end

def update_theme_version_safely
return if synchronization_locked?
return unless git_repository.valid?
git_repository.update_theme_version!
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class AddSynchronizationLockedByToCommunicationWebsites < ActiveRecord::Migration[8.1]
def change
add_reference :communication_websites,
:synchronization_locked_by,
type: :uuid,
foreign_key: {
to_table: :users,
on_delete: :nullify
}
end
end
5 changes: 4 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions test/models/communication/website/with_git_repository_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
require "test_helper"

# rails test test/models/communication/website/with_git_repository_test.rb
class Communication::Website::WithGitRepositoryTest < ActiveSupport::TestCase
include ActiveJob::TestHelper

test "lock and unlock synchronization" do
refute website_with_github.synchronization_locked?
website_with_github.lock_synchronization! users(:admin)
assert website_with_github.synchronization_locked?
website_with_github.unlock_synchronization!
refute website_with_github.synchronization_locked?
end

test "jobs are enqueued only when unlocked" do
# Locked: nothing is enqueued
website_with_github.lock_synchronization! users(:admin)
assert_no_enqueued_jobs do
website_with_github.sync_with_git
website_with_github.sync_with_git_safely
website_with_github.update_theme_version
end

# Unlocking re-triggers a sync for desynchronized generated git files
assert_enqueued_jobs 1, only: Communication::Website::SyncWithGitJob do
website_with_github.unlock_synchronization!
end

# Unlocked: jobs run normally
assert_enqueued_jobs 1, only: Communication::Website::UpdateThemeVersionJob do
website_with_github.update_theme_version
end

end
end
Loading