2019-07-29 16:18:34 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-04-18 21:53:48 +08:00
|
|
|
class RepositoryTableStateColumnUpdateService
|
|
|
|
# We're using Constants::REPOSITORY_TABLE_DEFAULT_STATE as a reference for
|
|
|
|
# default table state; this Ruby Hash makes heavy use of Ruby symbols
|
|
|
|
# notation; HOWEVER, the state that is saved on the RepositoryTableState
|
|
|
|
# record, has EVERYTHING (booleans, symbols, keys, ...) saved as Strings.
|
|
|
|
|
2018-04-20 17:44:43 +08:00
|
|
|
def update_states_with_new_column(repository)
|
2018-04-20 17:45:58 +08:00
|
|
|
raise ArgumentError, 'repository is empty' if repository.blank?
|
|
|
|
|
2021-08-13 20:07:01 +08:00
|
|
|
columns_num = repository.default_columns_count + repository.repository_columns.count
|
2018-04-18 21:53:48 +08:00
|
|
|
RepositoryTableState.where(
|
2018-04-20 17:44:43 +08:00
|
|
|
repository: repository
|
2018-04-18 21:53:48 +08:00
|
|
|
).find_each do |table_state|
|
|
|
|
state = table_state.state
|
2020-11-11 00:24:04 +08:00
|
|
|
next if state['columns'].length == columns_num
|
|
|
|
|
|
|
|
index = state['columns'].length
|
2018-04-18 21:53:48 +08:00
|
|
|
|
|
|
|
# Add new columns, ColReorder, length entries
|
2019-11-19 17:33:51 +08:00
|
|
|
state['columns'][index] = Constants::REPOSITORY_TABLE_STATE_CUSTOM_COLUMN_TEMPLATE
|
|
|
|
state['ColReorder'] << index
|
2020-01-08 23:16:54 +08:00
|
|
|
state['time'] = (Time.now.to_f * 1_000).to_i
|
2018-04-18 21:53:48 +08:00
|
|
|
table_state.save
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_states_with_removed_column(repository, old_column_index)
|
2018-04-20 17:45:58 +08:00
|
|
|
raise ArgumentError, 'repository is empty' if repository.blank?
|
2018-04-20 17:44:43 +08:00
|
|
|
raise ArgumentError, 'old_column_index is empty' if old_column_index.blank?
|
|
|
|
|
2020-01-15 06:49:02 +08:00
|
|
|
RepositoryTableState.where(repository: repository).find_each do |table_state|
|
2018-04-18 21:53:48 +08:00
|
|
|
state = table_state.state
|
2020-01-15 06:49:02 +08:00
|
|
|
user = table_state.user
|
2018-04-18 21:53:48 +08:00
|
|
|
|
2020-01-15 06:49:02 +08:00
|
|
|
begin
|
|
|
|
# Remove column from ColReorder, columns, length entries
|
|
|
|
state['columns'].delete_at(old_column_index)
|
|
|
|
state['ColReorder'].delete(old_column_index)
|
|
|
|
state['ColReorder'].map! do |index|
|
|
|
|
if index > old_column_index
|
|
|
|
index - 1
|
|
|
|
else
|
|
|
|
index
|
|
|
|
end
|
2018-04-18 21:53:48 +08:00
|
|
|
end
|
2018-04-20 17:44:43 +08:00
|
|
|
|
2020-08-05 19:15:38 +08:00
|
|
|
if state.dig('order', 0, 0).to_i == old_column_index
|
2020-01-15 06:49:02 +08:00
|
|
|
# Fallback to default order if user had table ordered by
|
|
|
|
# the deleted column
|
2021-08-13 20:07:01 +08:00
|
|
|
state['order'] = repository.default_table_state['order']
|
2020-08-05 19:15:38 +08:00
|
|
|
elsif state.dig('order', 0, 0).to_i > old_column_index
|
2020-01-15 06:49:02 +08:00
|
|
|
state['order'][0][0] -= 1
|
|
|
|
end
|
2018-04-20 17:44:43 +08:00
|
|
|
|
2020-01-15 06:49:02 +08:00
|
|
|
state['time'] = (Time.now.to_f * 1_000).to_i
|
|
|
|
table_state.save
|
2020-08-05 19:15:38 +08:00
|
|
|
rescue StandardError => e
|
2020-01-15 06:49:02 +08:00
|
|
|
Rails.logger.error e.message
|
|
|
|
RepositoryTableStateService.new(user, repository).create_default_state
|
|
|
|
end
|
2018-04-18 21:53:48 +08:00
|
|
|
end
|
|
|
|
end
|
2019-07-29 16:18:34 +08:00
|
|
|
end
|