2017-06-23 21:19:08 +08:00
|
|
|
class Checklist < ApplicationRecord
|
2016-07-21 19:11:15 +08:00
|
|
|
include SearchableModel
|
2016-09-21 21:35:23 +08:00
|
|
|
|
|
|
|
auto_strip_attributes :name, nullify: false
|
2016-10-05 23:45:20 +08:00
|
|
|
validates :name,
|
|
|
|
presence: true,
|
|
|
|
length: { maximum: Constants::TEXT_MAX_LENGTH }
|
2016-02-12 23:52:43 +08:00
|
|
|
validates :step, presence: true
|
|
|
|
|
2019-07-26 18:40:36 +08:00
|
|
|
belongs_to :step, inverse_of: :checklists, touch: true
|
2017-06-28 21:21:32 +08:00
|
|
|
belongs_to :created_by,
|
|
|
|
foreign_key: 'created_by_id',
|
|
|
|
class_name: 'User',
|
|
|
|
optional: true
|
|
|
|
belongs_to :last_modified_by,
|
|
|
|
foreign_key: 'last_modified_by_id',
|
|
|
|
class_name: 'User',
|
|
|
|
optional: true
|
2016-02-12 23:52:43 +08:00
|
|
|
has_many :checklist_items,
|
2016-09-27 19:00:51 +08:00
|
|
|
-> { order(:position) },
|
2016-02-12 23:52:43 +08:00
|
|
|
inverse_of: :checklist,
|
|
|
|
dependent: :destroy
|
|
|
|
has_many :report_elements,
|
|
|
|
inverse_of: :checklist,
|
|
|
|
dependent: :destroy
|
2022-07-20 16:14:48 +08:00
|
|
|
has_one :step_orderable_element, as: :orderable, dependent: :destroy
|
2016-02-12 23:52:43 +08:00
|
|
|
|
|
|
|
accepts_nested_attributes_for :checklist_items,
|
|
|
|
reject_if: :all_blank,
|
|
|
|
allow_destroy: true
|
|
|
|
|
2017-05-04 21:17:34 +08:00
|
|
|
scope :asc, -> { order('checklists.created_at ASC') }
|
2017-05-10 21:14:17 +08:00
|
|
|
|
2022-08-22 20:02:19 +08:00
|
|
|
def duplicate(step, user, position = nil)
|
2022-08-30 21:26:52 +08:00
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
new_checklist = step.checklists.create!(
|
|
|
|
name: name,
|
2022-08-22 20:02:19 +08:00
|
|
|
created_by: user,
|
|
|
|
last_modified_by: user
|
|
|
|
)
|
|
|
|
|
2022-08-30 21:26:52 +08:00
|
|
|
checklist_items.each do |item|
|
|
|
|
new_checklist.checklist_items.create!(
|
|
|
|
text: item.text,
|
|
|
|
checked: false,
|
|
|
|
position: item.position,
|
|
|
|
created_by: user,
|
|
|
|
last_modified_by: user
|
|
|
|
)
|
|
|
|
end
|
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_checklist
|
|
|
|
)
|
|
|
|
|
|
|
|
new_checklist
|
|
|
|
end
|
2022-08-22 20:02:19 +08:00
|
|
|
end
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|