2017-06-23 21:19:08 +08:00
|
|
|
class RepositoryColumn < ApplicationRecord
|
2017-06-28 21:21:32 +08:00
|
|
|
belongs_to :repository, optional: true
|
|
|
|
belongs_to :created_by,
|
|
|
|
foreign_key: :created_by_id,
|
|
|
|
class_name: 'User',
|
|
|
|
optional: true
|
2017-05-16 21:27:36 +08:00
|
|
|
has_many :repository_cells, dependent: :destroy
|
|
|
|
has_many :repository_rows, through: :repository_cells
|
2018-02-12 16:05:41 +08:00
|
|
|
has_many :repository_list_items, dependent: :destroy
|
2017-05-16 21:27:36 +08:00
|
|
|
|
|
|
|
enum data_type: Extends::REPOSITORY_DATA_TYPES
|
|
|
|
|
|
|
|
auto_strip_attributes :name, nullify: false
|
|
|
|
validates :name,
|
|
|
|
presence: true,
|
|
|
|
length: { maximum: Constants::NAME_MAX_LENGTH },
|
2017-05-17 16:04:07 +08:00
|
|
|
uniqueness: { scope: :repository, case_sensitive: true }
|
2017-05-16 21:27:36 +08:00
|
|
|
validates :created_by, presence: true
|
|
|
|
validates :repository, presence: true
|
2017-05-17 16:04:07 +08:00
|
|
|
validates :data_type, presence: true
|
2017-06-06 23:35:29 +08:00
|
|
|
|
2018-04-20 17:44:43 +08:00
|
|
|
after_create :update_repository_table_states_with_new_column
|
|
|
|
around_destroy :update_repository_table_states_with_removed_column
|
2017-06-06 23:35:29 +08:00
|
|
|
|
2018-03-15 22:43:16 +08:00
|
|
|
scope :list_type, -> { where(data_type: 'RepositoryListValue') }
|
2018-05-16 15:31:19 +08:00
|
|
|
scope :asset_type, -> { where(data_type: 'RepositoryAssetValue') }
|
|
|
|
|
|
|
|
def self.name_like(query)
|
|
|
|
where('repository_columns.name ILIKE ?', "%#{query}%")
|
|
|
|
end
|
2018-03-15 22:43:16 +08:00
|
|
|
|
2018-04-20 17:44:43 +08:00
|
|
|
def update_repository_table_states_with_new_column
|
2018-04-18 21:53:48 +08:00
|
|
|
service = RepositoryTableStateColumnUpdateService.new
|
2018-04-20 17:44:43 +08:00
|
|
|
service.update_states_with_new_column(repository)
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_repository_table_states_with_removed_column
|
|
|
|
# Calculate old_column_index - this can only be done before
|
|
|
|
# record is deleted when we still have its index
|
|
|
|
old_column_index = (
|
|
|
|
Constants::REPOSITORY_TABLE_DEFAULT_STATE[:length] +
|
|
|
|
repository.repository_columns
|
|
|
|
.order(id: :asc)
|
|
|
|
.pluck(:id)
|
|
|
|
.index(id)
|
|
|
|
).to_s
|
|
|
|
|
|
|
|
# Perform the destroy itself
|
|
|
|
yield
|
|
|
|
|
|
|
|
# Update repository table states
|
|
|
|
service = RepositoryTableStateColumnUpdateService.new
|
|
|
|
service.update_states_with_removed_column(
|
|
|
|
repository, old_column_index
|
|
|
|
)
|
2017-06-06 23:35:29 +08:00
|
|
|
end
|
2018-03-13 18:36:05 +08:00
|
|
|
|
|
|
|
def importable?
|
|
|
|
Extends::REPOSITORY_IMPORTABLE_TYPES.include?(data_type.to_sym)
|
|
|
|
end
|
2017-05-16 21:27:36 +08:00
|
|
|
end
|