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
|
2018-08-24 17:26:49 +08:00
|
|
|
validates_inclusion_of :repository_list_item,
|
|
|
|
in: (lambda do |list_value|
|
|
|
|
list_value.repository_cell
|
|
|
|
.repository_column
|
|
|
|
.repository_list_items
|
|
|
|
end)
|
2018-02-07 22:28:28 +08:00
|
|
|
|
2019-06-06 23:28:59 +08:00
|
|
|
SORTABLE_COLUMN_NAME = 'repository_list_items.data'
|
|
|
|
SORTABLE_VALUE_INCLUDE = { repository_list_value: :repository_list_item }.freeze
|
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
.find(payload)
|
|
|
|
value
|
|
|
|
end
|
2018-02-07 22:28:28 +08:00
|
|
|
end
|