2019-05-07 19:47:50 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-11-27 20:06:46 +08:00
|
|
|
class RepositoryCell < ApplicationRecord
|
2022-03-09 21:13:48 +08:00
|
|
|
include ReminderRepositoryCellJoinable
|
|
|
|
|
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
|
2021-07-07 23:43:51 +08:00
|
|
|
belongs_to :value, polymorphic: true, inverse_of: :repository_cell, dependent: :destroy
|
|
|
|
|
|
|
|
{
|
|
|
|
repository_text: 'RepositoryTextValue',
|
|
|
|
repository_number: 'RepositoryNumberValue',
|
|
|
|
repository_list: 'RepositoryListValue',
|
|
|
|
repository_asset: 'RepositoryAssetValue',
|
|
|
|
repository_status: 'RepositoryStatusValue',
|
|
|
|
repository_checklist: 'RepositoryChecklistValue',
|
|
|
|
repository_date_time: 'RepositoryDateTimeValueBase',
|
|
|
|
repository_time: 'RepositoryDateTimeValueBase',
|
|
|
|
repository_date: 'RepositoryDateTimeValueBase',
|
|
|
|
repository_date_time_range: 'RepositoryDateTimeRangeValueBase',
|
|
|
|
repository_time_range: 'RepositoryDateTimeRangeValueBase',
|
2022-01-26 00:48:45 +08:00
|
|
|
repository_date_range: 'RepositoryDateTimeRangeValueBase',
|
|
|
|
repository_stock: 'RepositoryStockValue',
|
|
|
|
repository_stock_consumption_snapshot: 'RepositoryStockConsumptionValue'
|
2021-07-07 23:43:51 +08:00
|
|
|
}.each do |relation, class_name|
|
|
|
|
belongs_to "#{relation}_value".to_sym,
|
|
|
|
(lambda do |repository_cell|
|
|
|
|
repository_cell.value_type == class_name ? self : none
|
|
|
|
end),
|
|
|
|
optional: true, foreign_key: :value_id, inverse_of: :repository_cell
|
|
|
|
end
|
2019-12-08 23:53:59 +08:00
|
|
|
|
2022-03-11 19:38:45 +08:00
|
|
|
has_many :hidden_repository_cell_reminders, dependent: :destroy
|
|
|
|
|
2019-11-27 20:06:46 +08:00
|
|
|
validates :repository_column,
|
2021-07-07 23:43:51 +08:00
|
|
|
inclusion: { in: (lambda do |repository_cell|
|
|
|
|
repository_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
|
|
|
|
2022-03-11 19:38:45 +08:00
|
|
|
scope :with_active_reminder, lambda { |user|
|
2023-01-24 16:36:13 +08:00
|
|
|
reminder_repository_cells_scope(self, user)
|
2022-03-09 21:13:48 +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)
|
2022-01-26 00:48:45 +08:00
|
|
|
cell_snapshot
|
2020-04-09 18:33:04 +08:00
|
|
|
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
|