mirror of
https://github.com/LaCasemate/fab-manager.git
synced 2025-04-10 00:53:51 +02:00
almost finishes synchro with openlab system
This commit is contained in:
parent
711b18f6cd
commit
f65bb95575
@ -2,43 +2,51 @@ module Project::OpenlabSync
|
|||||||
extend ActiveSupport::Concern
|
extend ActiveSupport::Concern
|
||||||
|
|
||||||
included do
|
included do
|
||||||
|
include ActionView::Helpers::SanitizeHelper
|
||||||
|
|
||||||
after_create :openlab_create, if: :openlab_sync_active?
|
after_create :openlab_create, if: :openlab_sync_active?
|
||||||
after_update :openlab_update, if: :openlab_sync_active?
|
after_update :openlab_update, if: :openlab_sync_active?
|
||||||
after_destroy :openlab_destroy, if: :openlab_sync_active?
|
after_destroy :openlab_destroy, if: :openlab_sync_active?
|
||||||
|
|
||||||
def openlab_create
|
def openlab_create
|
||||||
OpenlabSync.perform_async(:create, self.id) if self.published?
|
OpenlabWorker.delay_for(2.seconds).perform_async(:create, self.id) if self.published?
|
||||||
end
|
end
|
||||||
|
|
||||||
def openlab_update
|
def openlab_update
|
||||||
if self.published?
|
if self.published?
|
||||||
if self.state_was == 'draft'
|
if self.state_was == 'draft'
|
||||||
OpenlabSync.perform_async(:create, self.id)
|
OpenlabWorker.perform_async(:create, self.id)
|
||||||
else
|
else
|
||||||
OpenlabSync.perform_async(:update, self.id)
|
OpenlabWorker.perform_async(:update, self.id)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def openlab_destroy
|
def openlab_destroy
|
||||||
OpenlabSync.perform_async(:destroy, self.id)
|
OpenlabWorker.perform_async(:destroy, self.id)
|
||||||
end
|
end
|
||||||
|
|
||||||
def openlab_attributes
|
def openlab_attributes
|
||||||
{
|
{
|
||||||
id: id, name: name, description: description, tags: tags,
|
id: id, slug: slug, name: name, description: description, tags: tags,
|
||||||
machines: machines.map(&:name),
|
machines: machines.map(&:name),
|
||||||
components: components.map(&:name),
|
components: components.map(&:name),
|
||||||
themes: themes.map(&:name),
|
themes: themes.map(&:name),
|
||||||
author: author.profile.full_name,
|
author: author&.profile&.full_name,
|
||||||
collaborators: users.map { |u| u.profile.full_name },
|
collaborators: users.map { |u| u.profile.full_name },
|
||||||
steps_body: steps_body
|
steps_body: steps_body,
|
||||||
|
image_path: project_image&.attachment&.medium&.url,
|
||||||
|
project_path: "/#!/projects/#{slug}"
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
def steps_body
|
def steps_body
|
||||||
|
concatenated_steps = project_steps.map { |s| "#{s.title} #{s.description}" }
|
||||||
|
.join(' ').gsub('</p>', ' </p>')
|
||||||
|
.gsub("\r\n", ' ').gsub("\n\r", ' ')
|
||||||
|
.gsub("\n", ' ').gsub("\r", ' ').gsub("\t", ' ')
|
||||||
|
|
||||||
|
strip_tags(concatenated_steps).strip
|
||||||
end
|
end
|
||||||
|
|
||||||
def openlab_sync_active?
|
def openlab_sync_active?
|
||||||
|
@ -3,7 +3,7 @@ class OpenlabWorker
|
|||||||
sidekiq_options queue: 'default', retry: true
|
sidekiq_options queue: 'default', retry: true
|
||||||
|
|
||||||
Logger = Sidekiq.logger.level == Logger::DEBUG ? Sidekiq.logger : nil
|
Logger = Sidekiq.logger.level == Logger::DEBUG ? Sidekiq.logger : nil
|
||||||
openlab_client = Openlab::Projects.new
|
OPENLAB_CLIENT = Openlab::Projects.new
|
||||||
|
|
||||||
def perform(action, project_id)
|
def perform(action, project_id)
|
||||||
logger.debug ["Openlab sync", action, "project ID: #{project_id}"]
|
logger.debug ["Openlab sync", action, "project ID: #{project_id}"]
|
||||||
@ -11,12 +11,14 @@ class OpenlabWorker
|
|||||||
case action.to_s
|
case action.to_s
|
||||||
when /create/
|
when /create/
|
||||||
project = Project.find(project_id)
|
project = Project.find(project_id)
|
||||||
openlab_client.create(project.openlab_attributes)
|
response = OPENLAB_CLIENT.create(project.openlab_attributes)
|
||||||
when /update/
|
when /update/
|
||||||
project = Project.find(project_id)
|
project = Project.find(project_id)
|
||||||
openlab_client.update(project_id, project.openlab_attributes)
|
response = OPENLAB_CLIENT.update(project_id, project.openlab_attributes)
|
||||||
when /destroy/
|
when /destroy/
|
||||||
openlab_client.destroy(project_id)
|
response = OPENLAB_CLIENT.destroy(project_id)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
logger.debug ["Openlab sync", "RESPONSE ERROR", response.inspect] unless response.success?
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
Openlab.configure do |config|
|
Openlab.configure do |config|
|
||||||
config.app_secret = Rails.application.secrets.openfablab_app_secret
|
config.app_secret = Rails.application.secrets.openlab_app_secret
|
||||||
config.base_uri = Rails.env.production? ? "https://urltochange.nawak" : "localhost:3300/api/v1"
|
config.base_uri = Rails.env.production? ? "https://urltochange.nawak" : "localhost:3300/api/v1"
|
||||||
end
|
end
|
||||||
|
13
lib/tasks/fablab/openlab.rake
Normal file
13
lib/tasks/fablab/openlab.rake
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
namespace :fablab do
|
||||||
|
namespace :openlab do
|
||||||
|
task bulk_export: :environment do
|
||||||
|
if Rails.application.secrets.openlab_app_secret.present?
|
||||||
|
Project.find_each do |project|
|
||||||
|
project.openlab_create
|
||||||
|
end
|
||||||
|
else
|
||||||
|
warn "Rails.application.secrets.openlab_app_secret not present. Export can't be done."
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
x
Reference in New Issue
Block a user