2022-04-19 04:38:49 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class StepText < ApplicationRecord
|
|
|
|
include TinyMceImages
|
2022-06-02 17:15:32 +08:00
|
|
|
include ActionView::Helpers::TextHelper
|
2022-04-19 04:38:49 +08:00
|
|
|
|
|
|
|
auto_strip_attributes :text, nullify: false
|
|
|
|
validates :text, length: { maximum: Constants::RICH_TEXT_MAX_LENGTH }
|
|
|
|
|
|
|
|
belongs_to :step, inverse_of: :step_texts, touch: true
|
2022-07-20 16:14:48 +08:00
|
|
|
has_one :step_orderable_element, as: :orderable, dependent: :destroy
|
2022-06-02 17:15:32 +08:00
|
|
|
|
2022-06-08 21:55:50 +08:00
|
|
|
scope :asc, -> { order('step_texts.created_at ASC') }
|
|
|
|
|
2022-06-02 17:15:32 +08:00
|
|
|
def name
|
2022-06-02 17:32:31 +08:00
|
|
|
return if text.blank?
|
|
|
|
|
2022-06-02 17:15:32 +08:00
|
|
|
strip_tags(text.truncate(64))
|
|
|
|
end
|
2022-08-22 20:02:19 +08:00
|
|
|
|
|
|
|
def duplicate(step, position = nil)
|
2022-08-30 21:26:52 +08:00
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
new_step_text = step.step_texts.create!(
|
|
|
|
text: text
|
|
|
|
)
|
2022-08-22 20:02:19 +08:00
|
|
|
|
2022-08-30 21:26:52 +08:00
|
|
|
# Copy steps tinyMce assets
|
|
|
|
clone_tinymce_assets(new_step_text, step.protocol.team)
|
2022-08-22 20:02:19 +08:00
|
|
|
|
2022-08-30 21:26:52 +08:00
|
|
|
step.step_orderable_elements.create!(
|
|
|
|
position: position || step.step_orderable_elements.length,
|
|
|
|
orderable: new_step_text
|
|
|
|
)
|
2022-08-22 20:02:19 +08:00
|
|
|
|
2022-08-30 21:26:52 +08:00
|
|
|
new_step_text
|
|
|
|
end
|
2022-08-22 20:02:19 +08:00
|
|
|
end
|
2022-04-19 04:38:49 +08:00
|
|
|
end
|