2025-02-26 17:51:04 +01:00
|
|
|
# frozen_string_literal: true
|
2022-11-22 17:43:19 +01:00
|
|
|
|
2025-02-26 17:51:04 +01:00
|
|
|
# Module for synchronizing objects with OpenLab platform
|
2022-11-22 17:43:19 +01:00
|
|
|
module OpenlabSync
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
2025-02-26 17:51:04 +01:00
|
|
|
after_save :openlab_sync_after_save, if: :should_sync_with_openlab?
|
|
|
|
after_destroy :openlab_destroy, if: :should_sync_with_openlab?
|
|
|
|
|
|
|
|
def openlab_sync_after_save
|
|
|
|
if saved_change_to_state? && published?
|
|
|
|
# New publication - create in OpenLab
|
|
|
|
openlab_create
|
|
|
|
elsif published?
|
|
|
|
# Update existing publication
|
|
|
|
openlab_update
|
|
|
|
end
|
|
|
|
end
|
2022-11-22 17:43:19 +01:00
|
|
|
|
|
|
|
def openlab_create
|
2025-02-26 17:51:04 +01:00
|
|
|
OpenlabWorker.perform_async(:create, id)
|
2022-11-22 17:43:19 +01:00
|
|
|
end
|
|
|
|
|
2025-02-26 17:51:04 +01:00
|
|
|
def openlab_update
|
|
|
|
OpenlabWorker.perform_async(:update, id)
|
2022-11-22 17:43:19 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def openlab_destroy
|
|
|
|
OpenlabWorker.perform_async(:destroy, id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def openlab_attributes
|
|
|
|
OpenLabService.to_hash(self)
|
|
|
|
end
|
|
|
|
|
2025-02-26 17:51:04 +01:00
|
|
|
# Determines if the object should be synced with OpenLab
|
|
|
|
def should_sync_with_openlab?
|
|
|
|
openlab_sync_active? && published?
|
|
|
|
end
|
|
|
|
|
2022-11-22 17:43:19 +01:00
|
|
|
def openlab_sync_active?
|
|
|
|
self.class.openlab_sync_active?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class_methods do
|
|
|
|
def openlab_sync_active?
|
|
|
|
Setting.get('openlab_app_secret').present?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|