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?
|
|
|
|
|
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
|
|
|
|
index = state['columns'].count
|
|
|
|
|
|
|
|
# Add new columns, ColReorder, length entries
|
|
|
|
state['columns'][index.to_s] =
|
|
|
|
HashUtil.deep_stringify_keys_and_values(
|
|
|
|
Constants::REPOSITORY_TABLE_STATE_CUSTOM_COLUMN_TEMPLATE
|
|
|
|
)
|
|
|
|
state['ColReorder'] << index.to_s
|
|
|
|
state['length'] = (index + 1).to_s
|
|
|
|
state['time'] = Time.new.to_i.to_s
|
|
|
|
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?
|
|
|
|
|
2018-04-18 21:53:48 +08:00
|
|
|
RepositoryTableState.where(
|
|
|
|
repository: repository
|
|
|
|
).find_each do |table_state|
|
|
|
|
state = table_state.state
|
|
|
|
|
|
|
|
# old_column_index is a String!
|
|
|
|
|
|
|
|
# Remove column from ColReorder, columns, length entries
|
|
|
|
state['columns'].delete(old_column_index)
|
|
|
|
state['columns'].keys.each do |index|
|
|
|
|
if index.to_i > old_column_index.to_i
|
|
|
|
state['columns'][(index.to_i - 1).to_s] =
|
|
|
|
state['columns'].delete(index.to_s)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
state['ColReorder'].delete(old_column_index)
|
|
|
|
state['ColReorder'].map! do |index|
|
|
|
|
if index.to_i > old_column_index.to_i
|
|
|
|
(index.to_i - 1).to_s
|
|
|
|
else
|
|
|
|
index
|
|
|
|
end
|
|
|
|
end
|
2018-04-20 17:44:43 +08:00
|
|
|
|
2018-05-15 05:48:03 +08:00
|
|
|
state['order'].reject! { |_, v| v[0] == old_column_index }
|
|
|
|
state['order'].each do |k, v|
|
|
|
|
if v[0].to_i > old_column_index.to_i
|
|
|
|
state['order'][k] = [(v[0].to_i - 1).to_s, v[1]]
|
|
|
|
end
|
|
|
|
end
|
2018-04-20 17:44:43 +08:00
|
|
|
if state['order'].empty?
|
|
|
|
# Fallback to default order if user had table ordered by
|
|
|
|
# the deleted column
|
|
|
|
state['order'] = HashUtil.deep_stringify_keys_and_values(
|
|
|
|
Constants::REPOSITORY_TABLE_DEFAULT_STATE[:order]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2018-04-18 21:53:48 +08:00
|
|
|
state['length'] = (state['length'].to_i - 1).to_s
|
|
|
|
state['time'] = Time.new.to_i.to_s
|
|
|
|
table_state.save
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|