2019-10-08 19:38:57 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class RepositoryStatusValue < ApplicationRecord
|
|
|
|
belongs_to :repository_status_item
|
|
|
|
belongs_to :created_by, foreign_key: 'created_by_id', class_name: 'User', optional: true,
|
2019-12-10 16:40:40 +08:00
|
|
|
inverse_of: :created_repository_status_value
|
2019-10-08 19:38:57 +08:00
|
|
|
belongs_to :last_modified_by, foreign_key: 'last_modified_by_id', class_name: 'User', optional: true,
|
2019-12-10 16:40:40 +08:00
|
|
|
inverse_of: :modified_repository_status_value
|
2019-10-08 19:38:57 +08:00
|
|
|
has_one :repository_cell, as: :value, dependent: :destroy, inverse_of: :value
|
2019-11-06 18:44:00 +08:00
|
|
|
accepts_nested_attributes_for :repository_cell
|
|
|
|
|
2020-01-10 18:09:47 +08:00
|
|
|
validates :repository_cell, :repository_status_item, presence: true
|
2019-11-06 18:44:00 +08:00
|
|
|
|
|
|
|
SORTABLE_COLUMN_NAME = 'repository_status_items.status'
|
|
|
|
SORTABLE_VALUE_INCLUDE = { repository_status_value: :repository_status_item }.freeze
|
2020-02-03 22:20:01 +08:00
|
|
|
PRELOAD_INCLUDE = { repository_status_value: :repository_status_item }.freeze
|
2019-11-06 18:44:00 +08:00
|
|
|
|
|
|
|
def formatted
|
|
|
|
data
|
|
|
|
end
|
|
|
|
|
2019-12-08 23:53:59 +08:00
|
|
|
def data_changed?(new_data)
|
|
|
|
new_data.to_i != repository_status_item_id
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_data!(new_data, user)
|
|
|
|
self.repository_status_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
|
2020-05-14 17:47:19 +08:00
|
|
|
status_item = cell_snapshot.repository_column
|
2020-05-14 18:57:16 +08:00
|
|
|
.repository_status_items
|
|
|
|
.find { |item| item.data == repository_status_item.data }
|
2020-04-09 18:33:04 +08:00
|
|
|
value_snapshot.assign_attributes(
|
|
|
|
repository_cell: cell_snapshot,
|
2020-05-14 17:47:19 +08:00
|
|
|
repository_status_item: status_item,
|
2020-04-09 18:33:04 +08:00
|
|
|
created_at: created_at,
|
|
|
|
updated_at: updated_at
|
|
|
|
)
|
|
|
|
value_snapshot.save!
|
|
|
|
end
|
|
|
|
|
2019-11-06 18:44:00 +08:00
|
|
|
def data
|
|
|
|
return nil unless repository_status_item
|
|
|
|
|
2020-05-14 17:47:19 +08:00
|
|
|
repository_status_item.data
|
2019-11-06 18:44:00 +08:00
|
|
|
end
|
2019-12-09 20:48:24 +08:00
|
|
|
|
|
|
|
def self.new_with_payload(payload, attributes)
|
|
|
|
value = new(attributes)
|
|
|
|
value.repository_status_item = value.repository_cell
|
|
|
|
.repository_column
|
|
|
|
.repository_status_items
|
|
|
|
.find(payload)
|
|
|
|
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-02-03 22:20:01 +08:00
|
|
|
icon = text[0]
|
2020-05-14 23:59:25 +08:00
|
|
|
status = text[1..-1]&.strip
|
|
|
|
return nil if status.nil? || icon.nil?
|
|
|
|
|
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)
|
|
|
|
status_item = column.repository_status_items.find { |item| item.status == status }
|
2020-02-03 22:20:01 +08:00
|
|
|
|
|
|
|
if status_item.blank?
|
|
|
|
status_item = column.repository_status_items.new(icon: icon,
|
|
|
|
status: status,
|
|
|
|
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 status_item.save
|
|
|
|
end
|
|
|
|
|
|
|
|
value.repository_status_item = status_item
|
|
|
|
value
|
|
|
|
end
|
|
|
|
|
2020-01-10 16:29:20 +08:00
|
|
|
alias export_formatted formatted
|
2019-10-08 19:38:57 +08:00
|
|
|
end
|