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
|
|
|
|
where(repository_cells: { value_type: 'RepositoryTextValue' })
|
|
|
|
end),
|
|
|
|
optional: true, foreign_key: :value_id
|
|
|
|
belongs_to :repository_date_value,
|
|
|
|
(lambda do
|
|
|
|
where(repository_cells: { value_type: 'RepositoryDateValue' })
|
|
|
|
end),
|
|
|
|
optional: true, foreign_key: :value_id
|
|
|
|
belongs_to :repository_list_value,
|
|
|
|
(lambda do
|
|
|
|
where(repository_cells: { value_type: 'RepositoryListValue' })
|
|
|
|
end),
|
|
|
|
optional: true, foreign_key: :value_id
|
2018-03-10 00:04:54 +08:00
|
|
|
belongs_to :repository_asset_value,
|
|
|
|
(lambda do
|
|
|
|
where(repository_cells: { value_type: 'RepositoryAssetValue' })
|
|
|
|
end),
|
|
|
|
optional: true, foreign_key: :value_id
|
2017-05-16 21:27:36 +08:00
|
|
|
|
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
|
|
|
|
|
|
|
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
|