2019-05-07 19:47:50 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-11-27 20:06:46 +08:00
|
|
|
class RepositoryCell < ApplicationRecord
|
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),
|
2020-02-17 07:26:20 +08:00
|
|
|
optional: true, foreign_key: :value_id, inverse_of: :repository_cell
|
2019-12-09 23:38:21 +08:00
|
|
|
belongs_to :repository_number_value,
|
|
|
|
(lambda do
|
|
|
|
includes(:repository_cell)
|
|
|
|
.where(repository_cells: { value_type: 'RepositoryNumberValue' })
|
|
|
|
end),
|
2020-02-17 07:26:20 +08:00
|
|
|
optional: true, foreign_key: :value_id, inverse_of: :repository_cell
|
2019-12-08 23:53:59 +08:00
|
|
|
belongs_to :repository_date_value,
|
2018-03-09 18:05:43 +08:00
|
|
|
(lambda do
|
2018-08-07 22:02:14 +08:00
|
|
|
includes(:repository_cell)
|
2019-12-16 22:21:50 +08:00
|
|
|
.where(repository_cells: { value_type: 'RepositoryDateTimeValueBase' })
|
2018-03-09 18:05:43 +08:00
|
|
|
end),
|
2020-02-17 07:26:20 +08:00
|
|
|
optional: true, foreign_key: :value_id, inverse_of: :repository_cell
|
2018-03-09 18:05:43 +08:00
|
|
|
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),
|
2020-02-17 07:26:20 +08:00
|
|
|
optional: true, foreign_key: :value_id, inverse_of: :repository_cell
|
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),
|
2020-02-17 07:26:20 +08:00
|
|
|
optional: true, foreign_key: :value_id, inverse_of: :repository_cell
|
2017-05-16 21:27:36 +08:00
|
|
|
|
2019-10-08 19:38:57 +08:00
|
|
|
belongs_to :repository_status_value,
|
|
|
|
(lambda do
|
|
|
|
includes(:repository_cell)
|
|
|
|
.where(repository_cells: { value_type: 'RepositoryStatusValue' })
|
|
|
|
end),
|
2020-02-17 07:26:20 +08:00
|
|
|
optional: true, foreign_key: :value_id, inverse_of: :repository_cell
|
2019-10-08 19:38:57 +08:00
|
|
|
|
2019-12-09 20:38:40 +08:00
|
|
|
belongs_to :repository_checklist_value,
|
2019-12-06 20:18:35 +08:00
|
|
|
(lambda do
|
|
|
|
includes(:repository_cell)
|
2019-12-09 20:38:40 +08:00
|
|
|
.where(repository_cells: { value_type: 'RepositoryChecklistValue' })
|
2019-12-06 20:18:35 +08:00
|
|
|
end),
|
2020-02-17 07:26:20 +08:00
|
|
|
optional: true, foreign_key: :value_id, inverse_of: :repository_cell
|
2019-12-06 20:18:35 +08:00
|
|
|
|
2020-01-09 23:18:28 +08:00
|
|
|
belongs_to :repository_date_time_value_base,
|
|
|
|
(lambda do
|
|
|
|
includes(:repository_cell)
|
|
|
|
.where(repository_cells: { value_type: 'RepositoryDateTimeValueBase' })
|
|
|
|
end),
|
2020-02-17 07:26:20 +08:00
|
|
|
optional: true, foreign_key: :value_id, inverse_of: :repository_cell
|
2020-01-09 23:18:28 +08:00
|
|
|
|
2019-11-18 22:21:57 +08:00
|
|
|
belongs_to :repository_date_time_value,
|
|
|
|
(lambda do
|
|
|
|
includes(:repository_cell)
|
2019-12-16 22:21:50 +08:00
|
|
|
.where(repository_cells: { value_type: 'RepositoryDateTimeValueBase' })
|
2019-11-18 22:21:57 +08:00
|
|
|
end),
|
2020-02-17 07:26:20 +08:00
|
|
|
optional: true, foreign_key: :value_id, inverse_of: :repository_cell
|
2019-11-18 22:21:57 +08:00
|
|
|
|
2019-12-08 23:53:59 +08:00
|
|
|
belongs_to :repository_time_value,
|
|
|
|
(lambda do
|
|
|
|
includes(:repository_cell)
|
2019-12-16 22:21:50 +08:00
|
|
|
.where(repository_cells: { value_type: 'RepositoryDateTimeValueBase' })
|
2019-12-08 23:53:59 +08:00
|
|
|
end),
|
2020-02-17 07:26:20 +08:00
|
|
|
optional: true, foreign_key: :value_id, inverse_of: :repository_cell
|
2020-01-09 23:18:28 +08:00
|
|
|
|
|
|
|
belongs_to :repository_date_time_range_value_base,
|
|
|
|
(lambda do
|
|
|
|
includes(:repository_cell)
|
|
|
|
.where(repository_cells: { value_type: 'RepositoryDateTimeRangeValueBase' })
|
|
|
|
end),
|
2020-02-17 07:26:20 +08:00
|
|
|
optional: true, foreign_key: :value_id, inverse_of: :repository_cell
|
2019-12-08 23:53:59 +08:00
|
|
|
|
2019-11-21 23:22:15 +08:00
|
|
|
belongs_to :repository_date_time_range_value,
|
|
|
|
(lambda do
|
|
|
|
includes(:repository_cell)
|
2019-12-17 18:27:07 +08:00
|
|
|
.where(repository_cells: { value_type: 'RepositoryDateTimeRangeValueBase' })
|
2019-11-21 23:22:15 +08:00
|
|
|
end),
|
2020-02-17 07:26:20 +08:00
|
|
|
optional: true, foreign_key: :value_id, inverse_of: :repository_cell
|
2019-11-21 23:22:15 +08:00
|
|
|
|
2019-12-08 23:53:59 +08:00
|
|
|
belongs_to :repository_date_range_value,
|
|
|
|
(lambda do
|
|
|
|
includes(:repository_cell)
|
2019-12-17 18:27:07 +08:00
|
|
|
.where(repository_cells: { value_type: 'RepositoryDateTimeRangeValueBase' })
|
2019-12-08 23:53:59 +08:00
|
|
|
end),
|
2020-02-17 07:26:20 +08:00
|
|
|
optional: true, foreign_key: :value_id, inverse_of: :repository_cell
|
2019-12-08 23:53:59 +08:00
|
|
|
|
|
|
|
belongs_to :repository_time_range_value,
|
|
|
|
(lambda do
|
|
|
|
includes(:repository_cell)
|
2019-12-17 18:27:07 +08:00
|
|
|
.where(repository_cells: { value_type: 'RepositoryDateTimeRangeValueBase' })
|
2019-12-08 23:53:59 +08:00
|
|
|
end),
|
2020-02-17 07:26:20 +08:00
|
|
|
optional: true, foreign_key: :value_id, inverse_of: :repository_cell
|
2019-12-08 23:53:59 +08:00
|
|
|
|
2019-11-27 20:06:46 +08:00
|
|
|
validates :repository_column,
|
|
|
|
inclusion: { in: (lambda do |cell|
|
|
|
|
cell.repository_row&.repository&.repository_columns || []
|
2020-02-11 20:51:34 +08:00
|
|
|
end) },
|
|
|
|
unless: :importing
|
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
|
2018-10-05 06:18:42 +08:00
|
|
|
cell
|
2018-08-23 20:52:00 +08:00
|
|
|
end
|
|
|
|
|
2020-04-09 18:33:04 +08:00
|
|
|
def snapshot!(row_snapshot)
|
|
|
|
cell_snapshot = dup
|
|
|
|
column_snapshot = row_snapshot.repository
|
|
|
|
.repository_columns
|
|
|
|
.find { |c| c.parent_id == repository_column.id }
|
|
|
|
cell_snapshot.assign_attributes(
|
|
|
|
repository_row: row_snapshot,
|
|
|
|
repository_column: column_snapshot,
|
|
|
|
created_at: created_at,
|
|
|
|
updated_at: updated_at
|
|
|
|
)
|
|
|
|
value.snapshot!(cell_snapshot)
|
|
|
|
end
|
|
|
|
|
2017-05-16 21:27:36 +08:00
|
|
|
private
|
|
|
|
|
|
|
|
def repository_column_data_type
|
2019-12-06 21:20:37 +08:00
|
|
|
if !repository_column || value.class.name != repository_column.data_type
|
2017-05-16 21:27:36 +08:00
|
|
|
errors.add(:value_type, 'must match column data type')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|