scinote-web/app/models/repository_cell.rb

151 lines
5.8 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
class RepositoryCell < ApplicationRecord
attr_accessor :importing
belongs_to :repository_row
belongs_to :repository_column
belongs_to :value, polymorphic: true,
inverse_of: :repository_cell,
dependent: :destroy
belongs_to :repository_text_value,
(lambda do
includes(:repository_cell)
.where(repository_cells: { value_type: 'RepositoryTextValue' })
end),
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),
optional: true, foreign_key: :value_id, inverse_of: :repository_cell
belongs_to :repository_date_value,
(lambda do
includes(:repository_cell)
.where(repository_cells: { value_type: 'RepositoryDateTimeValueBase' })
end),
optional: true, foreign_key: :value_id, inverse_of: :repository_cell
belongs_to :repository_list_value,
(lambda do
includes(:repository_cell)
.where(repository_cells: { value_type: 'RepositoryListValue' })
end),
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
includes(:repository_cell)
.where(repository_cells: { value_type: 'RepositoryAssetValue' })
2018-03-10 00:04:54 +08:00
end),
optional: true, foreign_key: :value_id, inverse_of: :repository_cell
belongs_to :repository_status_value,
(lambda do
includes(:repository_cell)
.where(repository_cells: { value_type: 'RepositoryStatusValue' })
end),
optional: true, foreign_key: :value_id, inverse_of: :repository_cell
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),
optional: true, foreign_key: :value_id, inverse_of: :repository_cell
2019-12-06 20:18:35 +08:00
belongs_to :repository_date_time_value_base,
(lambda do
includes(:repository_cell)
.where(repository_cells: { value_type: 'RepositoryDateTimeValueBase' })
end),
optional: true, foreign_key: :value_id, inverse_of: :repository_cell
2019-11-18 22:21:57 +08:00
belongs_to :repository_date_time_value,
(lambda do
includes(:repository_cell)
.where(repository_cells: { value_type: 'RepositoryDateTimeValueBase' })
2019-11-18 22:21:57 +08:00
end),
optional: true, foreign_key: :value_id, inverse_of: :repository_cell
2019-11-18 22:21:57 +08:00
belongs_to :repository_time_value,
(lambda do
includes(:repository_cell)
.where(repository_cells: { value_type: 'RepositoryDateTimeValueBase' })
end),
optional: true, foreign_key: :value_id, inverse_of: :repository_cell
belongs_to :repository_date_time_range_value_base,
(lambda do
includes(:repository_cell)
.where(repository_cells: { value_type: 'RepositoryDateTimeRangeValueBase' })
end),
optional: true, foreign_key: :value_id, inverse_of: :repository_cell
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' })
end),
optional: true, foreign_key: :value_id, inverse_of: :repository_cell
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' })
end),
optional: true, foreign_key: :value_id, inverse_of: :repository_cell
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' })
end),
optional: true, foreign_key: :value_id, inverse_of: :repository_cell
validates :repository_column,
inclusion: { in: (lambda do |cell|
cell.repository_row&.repository&.repository_columns || []
end) },
unless: :importing
2017-06-06 23:35:29 +08:00
validates :repository_column, presence: true
validate :repository_column_data_type
validates :repository_row,
uniqueness: { scope: :repository_column },
unless: :importing
def self.create_with_value!(row, column, data, user)
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
cell
end
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
private
def repository_column_data_type
2019-12-06 21:20:37 +08:00
if !repository_column || value.class.name != repository_column.data_type
errors.add(:value_type, 'must match column data type')
end
end
end