2019-12-09 20:38:40 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class RepositoryChecklistValue < ApplicationRecord
|
|
|
|
belongs_to :created_by, foreign_key: 'created_by_id', class_name: 'User',
|
2020-01-06 20:03:05 +08:00
|
|
|
inverse_of: :created_repository_checklist_values
|
2019-12-09 20:38:40 +08:00
|
|
|
belongs_to :last_modified_by, foreign_key: 'last_modified_by_id', class_name: 'User',
|
2020-01-06 20:03:05 +08:00
|
|
|
inverse_of: :modified_repository_checklist_values
|
2019-12-09 20:38:40 +08:00
|
|
|
has_one :repository_cell, as: :value, dependent: :destroy, inverse_of: :value
|
2020-01-14 23:55:10 +08:00
|
|
|
has_many :repository_checklist_items_values, dependent: :destroy
|
|
|
|
has_many :repository_checklist_items, -> { order('data ASC') }, through: :repository_checklist_items_values
|
2019-12-09 20:38:40 +08:00
|
|
|
accepts_nested_attributes_for :repository_cell
|
|
|
|
|
2020-01-10 18:09:47 +08:00
|
|
|
validates :repository_cell, presence: true
|
2020-01-14 23:55:10 +08:00
|
|
|
validates :repository_checklist_items, presence: true
|
2020-01-10 18:09:47 +08:00
|
|
|
|
2019-12-18 21:10:41 +08:00
|
|
|
SORTABLE_COLUMN_NAME = 'repository_checklist_items.data'
|
2019-12-09 20:38:40 +08:00
|
|
|
SORTABLE_VALUE_INCLUDE = { repository_checklist_value: :repository_checklist_items }.freeze
|
2020-02-03 22:20:01 +08:00
|
|
|
PRELOAD_INCLUDE = { repository_checklist_value: :repository_checklist_items }.freeze
|
2019-12-09 20:38:40 +08:00
|
|
|
|
2020-01-10 16:29:20 +08:00
|
|
|
def formatted(separator: ' | ')
|
|
|
|
repository_checklist_items.pluck(:data).join(separator)
|
|
|
|
end
|
|
|
|
|
|
|
|
def export_formatted
|
2020-01-22 20:15:29 +08:00
|
|
|
repository_checklist_items.pluck(:data).map { |d| d.tr("\n", ' ') }.join("\n")
|
2019-12-09 20:38:40 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def data
|
2020-01-14 23:55:10 +08:00
|
|
|
repository_checklist_items.map { |i| { value: i.id, label: i.data } }
|
2019-12-11 21:49:14 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def data_changed?(new_data)
|
2020-02-17 07:26:20 +08:00
|
|
|
if new_data.is_a?(String)
|
|
|
|
JSON.parse(new_data) != repository_checklist_items.pluck(:id)
|
|
|
|
else
|
|
|
|
new_data != repository_checklist_items.pluck(:id)
|
|
|
|
end
|
2019-12-11 21:49:14 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def update_data!(new_data, user)
|
2020-02-17 07:26:20 +08:00
|
|
|
item_ids = new_data.is_a?(String) ? JSON.parse(new_data) : new_data
|
2020-01-16 22:30:19 +08:00
|
|
|
return destroy! if item_ids.blank?
|
|
|
|
|
|
|
|
self.repository_checklist_items = repository_cell.repository_column
|
|
|
|
.repository_checklist_items
|
|
|
|
.where(id: item_ids)
|
2019-12-11 21:49:14 +08:00
|
|
|
self.last_modified_by = user
|
|
|
|
save!
|
|
|
|
end
|
|
|
|
|
2020-04-09 18:33:04 +08:00
|
|
|
def snapshot!(cell_snapshot)
|
|
|
|
value_snapshot = dup
|
|
|
|
item_values = repository_checklist_items.pluck(:data)
|
|
|
|
checklist_items_snapshot = cell_snapshot.repository_column
|
|
|
|
.repository_checklist_items
|
|
|
|
.select { |snapshot_item| item_values.include?(snapshot_item.data) }
|
|
|
|
|
|
|
|
value_snapshot.assign_attributes(
|
|
|
|
repository_cell: cell_snapshot,
|
|
|
|
repository_checklist_items: checklist_items_snapshot,
|
|
|
|
created_at: created_at,
|
|
|
|
updated_at: updated_at
|
|
|
|
)
|
|
|
|
value_snapshot.save!
|
|
|
|
end
|
|
|
|
|
2019-12-11 21:49:14 +08:00
|
|
|
def self.new_with_payload(payload, attributes)
|
2020-02-17 07:26:20 +08:00
|
|
|
item_ids = payload.is_a?(String) ? JSON.parse(payload) : payload
|
2019-12-20 22:42:54 +08:00
|
|
|
value = new(attributes)
|
2020-01-15 22:23:03 +08:00
|
|
|
value.repository_checklist_items = value.repository_cell
|
|
|
|
.repository_column
|
2020-01-16 22:30:19 +08:00
|
|
|
.repository_checklist_items
|
2020-02-17 07:26:20 +08:00
|
|
|
.where(id: item_ids)
|
2019-12-11 21:49:14 +08:00
|
|
|
value
|
2019-12-09 20:38:40 +08:00
|
|
|
end
|
2020-02-03 22:20:01 +08:00
|
|
|
|
2020-03-05 21:34:31 +08:00
|
|
|
def self.import_from_text(text, attributes, _options = {})
|
2020-02-03 22:20:01 +08:00
|
|
|
value = new(attributes)
|
2020-02-11 20:51:34 +08:00
|
|
|
column = attributes.dig(:repository_cell_attributes, :repository_column)
|
2020-02-03 22:20:01 +08:00
|
|
|
text.split("\n").each do |item_text|
|
2020-02-11 20:51:34 +08:00
|
|
|
checklist_item = column.repository_checklist_items.find { |item| item.data == item_text }
|
2020-02-03 22:20:01 +08:00
|
|
|
|
|
|
|
if checklist_item.blank?
|
|
|
|
checklist_item = column.repository_checklist_items.new(data: text,
|
|
|
|
created_by: value.created_by,
|
2020-04-09 23:11:58 +08:00
|
|
|
last_modified_by: value.last_modified_by)
|
2020-02-03 22:20:01 +08:00
|
|
|
|
|
|
|
return nil unless checklist_item.save
|
|
|
|
end
|
|
|
|
|
|
|
|
value.repository_checklist_items << checklist_item
|
|
|
|
end
|
|
|
|
|
|
|
|
value
|
|
|
|
end
|
2019-12-09 20:38:40 +08:00
|
|
|
end
|