2019-06-06 23:28:59 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-02-07 22:28:28 +08:00
|
|
|
class RepositoryListValue < ApplicationRecord
|
2018-08-24 20:25:06 +08:00
|
|
|
belongs_to :repository_list_item
|
2018-02-07 22:28:28 +08:00
|
|
|
belongs_to :created_by,
|
|
|
|
foreign_key: :created_by_id,
|
2018-02-09 21:11:56 +08:00
|
|
|
class_name: 'User'
|
2018-02-07 22:28:28 +08:00
|
|
|
belongs_to :last_modified_by,
|
|
|
|
foreign_key: :last_modified_by_id,
|
2018-02-09 21:11:56 +08:00
|
|
|
class_name: 'User'
|
2018-02-07 22:28:28 +08:00
|
|
|
has_one :repository_cell, as: :value, dependent: :destroy, inverse_of: :value
|
|
|
|
accepts_nested_attributes_for :repository_cell
|
|
|
|
|
|
|
|
validates :repository_cell, presence: true
|
2020-02-03 22:20:01 +08:00
|
|
|
validates :repository_list_item, presence: true
|
|
|
|
|
2018-08-24 17:26:49 +08:00
|
|
|
validates_inclusion_of :repository_list_item,
|
|
|
|
in: (lambda do |list_value|
|
2019-07-26 18:40:36 +08:00
|
|
|
list_value.repository_cell&.repository_column&.repository_list_items || []
|
2018-08-24 17:26:49 +08:00
|
|
|
end)
|
2018-02-07 22:28:28 +08:00
|
|
|
|
2019-06-06 23:28:59 +08:00
|
|
|
SORTABLE_COLUMN_NAME = 'repository_list_items.data'
|
2021-07-07 23:43:51 +08:00
|
|
|
EXTRA_SORTABLE_VALUE_INCLUDE = :repository_list_item
|
|
|
|
EXTRA_PRELOAD_INCLUDE = :repository_list_item
|
2019-06-06 23:28:59 +08:00
|
|
|
|
2018-02-09 18:38:52 +08:00
|
|
|
def formatted
|
2018-03-05 22:54:04 +08:00
|
|
|
data.to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
def data
|
|
|
|
return nil unless repository_list_item
|
2018-02-09 23:28:21 +08:00
|
|
|
repository_list_item.data
|
2018-02-07 22:28:28 +08:00
|
|
|
end
|
2018-08-23 20:52:00 +08:00
|
|
|
|
2018-08-24 17:26:49 +08:00
|
|
|
def data_changed?(new_data)
|
|
|
|
new_data.to_i != repository_list_item_id
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_data!(new_data, user)
|
|
|
|
self.repository_list_item_id = new_data.to_i
|
|
|
|
self.last_modified_by = user
|
|
|
|
save!
|
|
|
|
end
|
|
|
|
|
2020-04-09 18:33:04 +08:00
|
|
|
def snapshot!(cell_snapshot)
|
|
|
|
value_snapshot = dup
|
|
|
|
list_item = cell_snapshot.repository_column
|
|
|
|
.repository_list_items
|
|
|
|
.find { |item| item.data == repository_list_item.data }
|
|
|
|
value_snapshot.assign_attributes(
|
|
|
|
repository_cell: cell_snapshot,
|
|
|
|
repository_list_item: list_item,
|
|
|
|
created_at: created_at,
|
|
|
|
updated_at: updated_at
|
|
|
|
)
|
|
|
|
value_snapshot.save!
|
|
|
|
end
|
|
|
|
|
2018-08-23 20:52:00 +08:00
|
|
|
def self.new_with_payload(payload, attributes)
|
|
|
|
value = new(attributes)
|
|
|
|
value.repository_list_item = value.repository_cell
|
|
|
|
.repository_column
|
|
|
|
.repository_list_items
|
2020-11-27 21:59:17 +08:00
|
|
|
.find_by(id: payload)
|
2018-08-23 20:52:00 +08:00
|
|
|
value
|
|
|
|
end
|
2020-01-10 16:29:20 +08:00
|
|
|
|
2020-03-05 21:34:31 +08:00
|
|
|
def self.import_from_text(text, attributes, _options = {})
|
2020-10-12 20:42:47 +08:00
|
|
|
return nil if text.blank?
|
|
|
|
|
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)
|
|
|
|
list_item = column.repository_list_items.find { |item| item.data == text }
|
2020-02-03 22:20:01 +08:00
|
|
|
|
|
|
|
if list_item.blank?
|
|
|
|
list_item = column.repository_list_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 list_item.save
|
|
|
|
end
|
|
|
|
|
|
|
|
value.repository_list_item = list_item
|
|
|
|
value
|
|
|
|
end
|
|
|
|
|
2020-01-10 16:29:20 +08:00
|
|
|
alias export_formatted formatted
|
2018-02-07 22:28:28 +08:00
|
|
|
end
|