2019-12-06 20:18:35 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-12-09 20:38:40 +08:00
|
|
|
class RepositoryChecklistItem < ApplicationRecord
|
2020-11-27 21:59:17 +08:00
|
|
|
belongs_to :repository_column, inverse_of: :repository_checklist_items
|
2019-12-06 21:16:20 +08:00
|
|
|
belongs_to :created_by, foreign_key: 'created_by_id', class_name: 'User',
|
2019-12-09 20:38:40 +08:00
|
|
|
inverse_of: :created_repository_checklist_types
|
2019-12-06 21:16:20 +08:00
|
|
|
belongs_to :last_modified_by, foreign_key: 'last_modified_by_id', class_name: 'User',
|
2019-12-09 20:38:40 +08:00
|
|
|
inverse_of: :modified_repository_checklist_types
|
2020-01-14 23:55:10 +08:00
|
|
|
has_many :repository_checklist_items_values, dependent: :destroy
|
2020-01-28 20:38:37 +08:00
|
|
|
has_many :repository_checklist_values, through: :repository_checklist_items_values, dependent: :destroy
|
2020-02-03 22:20:01 +08:00
|
|
|
|
|
|
|
validate :validate_per_column_limit
|
|
|
|
validates :data, presence: true,
|
2021-12-17 19:26:58 +08:00
|
|
|
uniqueness: { scope: :repository_column_id },
|
2020-02-03 22:20:01 +08:00
|
|
|
length: { maximum: Constants::NAME_MAX_LENGTH }
|
|
|
|
|
2022-01-26 17:12:46 +08:00
|
|
|
before_destroy :update_table_fiter_elements
|
|
|
|
|
2020-02-03 22:20:01 +08:00
|
|
|
private
|
|
|
|
|
|
|
|
def validate_per_column_limit
|
2020-02-03 22:48:49 +08:00
|
|
|
if repository_column &&
|
2020-11-23 19:54:29 +08:00
|
|
|
repository_column.repository_checklist_items.size > Constants::REPOSITORY_CHECKLIST_ITEMS_PER_COLUMN
|
2020-02-03 22:20:01 +08:00
|
|
|
errors.add(:base, :per_column_limit)
|
|
|
|
end
|
|
|
|
end
|
2022-01-26 17:12:46 +08:00
|
|
|
|
|
|
|
def update_table_fiter_elements
|
|
|
|
repository_column.repository_table_filter_elements.find_each do |filter_element|
|
2022-02-10 16:50:34 +08:00
|
|
|
filter_element.parameters['item_ids']&.delete(id.to_s)
|
2022-01-26 17:12:46 +08:00
|
|
|
filter_element.parameters['item_ids'].blank? ? filter_element.destroy! : filter_element.save!
|
|
|
|
end
|
|
|
|
end
|
2019-12-06 20:18:35 +08:00
|
|
|
end
|