2016-04-20 18:13:36 +02:00
|
|
|
module Project::OpenlabSync
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
2016-04-21 11:42:43 +02:00
|
|
|
include ActionView::Helpers::SanitizeHelper
|
|
|
|
|
2016-04-20 18:13:36 +02:00
|
|
|
after_create :openlab_create, if: :openlab_sync_active?
|
|
|
|
after_update :openlab_update, if: :openlab_sync_active?
|
|
|
|
after_destroy :openlab_destroy, if: :openlab_sync_active?
|
|
|
|
|
|
|
|
def openlab_create
|
2019-09-20 15:01:27 +02:00
|
|
|
OpenlabWorker.perform_in(2.seconds, :create, self.id) if self.published?
|
2016-04-20 18:13:36 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def openlab_update
|
|
|
|
if self.published?
|
|
|
|
if self.state_was == 'draft'
|
2016-04-21 11:42:43 +02:00
|
|
|
OpenlabWorker.perform_async(:create, self.id)
|
2016-04-20 18:13:36 +02:00
|
|
|
else
|
2016-04-21 11:42:43 +02:00
|
|
|
OpenlabWorker.perform_async(:update, self.id)
|
2016-04-20 18:13:36 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def openlab_destroy
|
2016-04-21 11:42:43 +02:00
|
|
|
OpenlabWorker.perform_async(:destroy, self.id)
|
2016-04-20 18:13:36 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def openlab_attributes
|
|
|
|
{
|
2016-04-21 11:42:43 +02:00
|
|
|
id: id, slug: slug, name: name, description: description, tags: tags,
|
2016-04-20 18:13:36 +02:00
|
|
|
machines: machines.map(&:name),
|
|
|
|
components: components.map(&:name),
|
|
|
|
themes: themes.map(&:name),
|
2019-07-10 10:58:51 +02:00
|
|
|
author: author&.user&.profile&.full_name,
|
2019-06-12 12:22:38 +02:00
|
|
|
collaborators: users.map { |u| u&.profile&.full_name },
|
2016-04-21 11:42:43 +02:00
|
|
|
steps_body: steps_body,
|
|
|
|
image_path: project_image&.attachment&.medium&.url,
|
2016-04-21 18:19:55 +02:00
|
|
|
project_path: "/#!/projects/#{slug}",
|
|
|
|
updated_at: updated_at.to_s(:iso8601),
|
|
|
|
created_at: created_at.to_s(:iso8601),
|
|
|
|
published_at: published_at.to_s(:iso8601)
|
2016-04-20 18:13:36 +02:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def steps_body
|
2016-04-21 11:42:43 +02:00
|
|
|
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
|
2016-04-20 18:13:36 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def openlab_sync_active?
|
|
|
|
self.class.openlab_sync_active?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class_methods do
|
|
|
|
def openlab_sync_active?
|
|
|
|
Rails.application.secrets.openlab_app_secret.present?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|