2017-05-16 21:27:36 +08:00
|
|
|
class RepositoryCell < ActiveRecord::Base
|
2018-05-15 17:14:43 +08:00
|
|
|
attr_accessor :importing
|
|
|
|
|
2017-05-16 21:27:36 +08:00
|
|
|
belongs_to :repository_row
|
|
|
|
belongs_to :repository_column
|
2018-03-16 20:12:33 +08:00
|
|
|
belongs_to :value, polymorphic: true,
|
|
|
|
inverse_of: :repository_cell,
|
|
|
|
dependent: :destroy
|
2018-03-09 18:05:43 +08:00
|
|
|
belongs_to :repository_text_value,
|
|
|
|
(lambda do
|
2018-08-07 22:02:14 +08:00
|
|
|
includes(:repository_cell)
|
2018-08-07 20:19:49 +08:00
|
|
|
.where(repository_cells: { value_type: 'RepositoryTextValue' })
|
2018-03-09 18:05:43 +08:00
|
|
|
end),
|
|
|
|
optional: true, foreign_key: :value_id
|
|
|
|
belongs_to :repository_date_value,
|
|
|
|
(lambda do
|
2018-08-07 22:02:14 +08:00
|
|
|
includes(:repository_cell)
|
2018-08-07 20:19:49 +08:00
|
|
|
.where(repository_cells: { value_type: 'RepositoryDateValue' })
|
2018-03-09 18:05:43 +08:00
|
|
|
end),
|
|
|
|
optional: true, foreign_key: :value_id
|
|
|
|
belongs_to :repository_list_value,
|
|
|
|
(lambda do
|
2018-08-07 22:02:14 +08:00
|
|
|
includes(:repository_cell)
|
2018-08-07 20:19:49 +08:00
|
|
|
.where(repository_cells: { value_type: 'RepositoryListValue' })
|
2018-03-09 18:05:43 +08:00
|
|
|
end),
|
|
|
|
optional: true, foreign_key: :value_id
|
2018-03-10 00:04:54 +08:00
|
|
|
belongs_to :repository_asset_value,
|
|
|
|
(lambda do
|
2018-08-07 22:02:14 +08:00
|
|
|
includes(:repository_cell)
|
2018-08-07 20:19:49 +08:00
|
|
|
.where(repository_cells: { value_type: 'RepositoryAssetValue' })
|
2018-03-10 00:04:54 +08:00
|
|
|
end),
|
|
|
|
optional: true, foreign_key: :value_id
|
2017-05-16 21:27:36 +08:00
|
|
|
|
2018-08-23 20:52:00 +08:00
|
|
|
validates_inclusion_of :repository_column,
|
|
|
|
in: (lambda do |cell|
|
|
|
|
cell.repository_row.repository.repository_columns
|
|
|
|
end)
|
2017-06-06 23:35:29 +08:00
|
|
|
validates :repository_column, presence: true
|
2017-05-16 21:27:36 +08:00
|
|
|
validate :repository_column_data_type
|
2017-07-20 17:44:10 +08:00
|
|
|
validates :repository_row,
|
2018-05-15 17:14:43 +08:00
|
|
|
uniqueness: { scope: :repository_column },
|
|
|
|
unless: :importing
|
2017-05-16 21:27:36 +08:00
|
|
|
|
2018-08-24 17:26:49 +08:00
|
|
|
def self.create_with_value!(row, column, data, user)
|
2018-08-23 20:52:00 +08:00
|
|
|
cell = new(repository_row: row, repository_column: column)
|
|
|
|
cell.transaction do
|
|
|
|
value_klass = column.data_type.constantize
|
|
|
|
value = value_klass.new_with_payload(data, repository_cell: cell,
|
|
|
|
created_by: user,
|
|
|
|
last_modified_by: user)
|
|
|
|
cell.value = value
|
|
|
|
value.save!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-16 21:27:36 +08:00
|
|
|
private
|
|
|
|
|
|
|
|
def repository_column_data_type
|
|
|
|
if value_type != repository_column.data_type
|
|
|
|
errors.add(:value_type, 'must match column data type')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|