2017-06-23 21:19:08 +08:00
|
|
|
class RepositoryTableState < ApplicationRecord
|
2017-06-28 21:21:32 +08:00
|
|
|
belongs_to :user, inverse_of: :repository_table_states, optional: true
|
|
|
|
belongs_to :repository, inverse_of: :repository_table_states, optional: true
|
2017-06-06 23:35:29 +08:00
|
|
|
|
|
|
|
validates :user, :repository, presence: true
|
|
|
|
|
2017-06-07 19:36:39 +08:00
|
|
|
def self.load_state(user, repository)
|
|
|
|
table_state = where(user: user, repository: repository).pluck(:state)
|
|
|
|
if table_state.blank?
|
|
|
|
RepositoryTableState.create_state(user, repository)
|
|
|
|
table_state = where(user: user, repository: repository).pluck(:state)
|
|
|
|
end
|
|
|
|
table_state
|
|
|
|
end
|
2017-06-06 23:35:29 +08:00
|
|
|
|
|
|
|
def self.update_state(custom_column, column_index, user)
|
2017-06-20 20:52:41 +08:00
|
|
|
# table state of every user having access to this repository needs udpating
|
|
|
|
table_states = RepositoryTableState.where(
|
2017-06-06 23:35:29 +08:00
|
|
|
repository: custom_column.repository
|
|
|
|
)
|
2017-06-20 20:52:41 +08:00
|
|
|
table_states.each do |table_state|
|
|
|
|
repository_state = table_state['state']
|
|
|
|
if column_index
|
|
|
|
# delete column
|
|
|
|
repository_state['columns'].delete(column_index)
|
|
|
|
repository_state['columns'].keys.each do |index|
|
|
|
|
if index.to_i > column_index.to_i
|
|
|
|
repository_state['columns'][(index.to_i - 1).to_s] =
|
|
|
|
repository_state['columns'].delete(index)
|
|
|
|
else
|
|
|
|
index
|
|
|
|
end
|
2017-06-06 23:35:29 +08:00
|
|
|
end
|
2017-06-20 20:52:41 +08:00
|
|
|
|
|
|
|
repository_state['ColReorder'].delete(column_index)
|
|
|
|
repository_state['ColReorder'].map! do |index|
|
|
|
|
if index.to_i > column_index.to_i
|
|
|
|
(index.to_i - 1).to_s
|
|
|
|
else
|
|
|
|
index
|
|
|
|
end
|
2017-06-06 23:35:29 +08:00
|
|
|
end
|
2017-06-20 20:52:41 +08:00
|
|
|
else
|
|
|
|
# add column
|
|
|
|
index = repository_state['columns'].count
|
|
|
|
repository_state['columns'][index] = RepositoryDatatable::
|
|
|
|
REPOSITORY_TABLE_DEFAULT_STATE['columns'].first
|
2018-01-29 23:01:33 +08:00
|
|
|
repository_state['ColReorder'].insert(2, index.to_s)
|
2017-06-06 23:35:29 +08:00
|
|
|
end
|
2017-06-20 20:52:41 +08:00
|
|
|
table_state.update(state: repository_state)
|
2017-06-06 23:35:29 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.create_state(user, repository)
|
|
|
|
default_columns_num = RepositoryDatatable::
|
|
|
|
REPOSITORY_TABLE_DEFAULT_STATE['columns'].count
|
|
|
|
repository_state =
|
|
|
|
RepositoryDatatable::REPOSITORY_TABLE_DEFAULT_STATE.deep_dup
|
|
|
|
repository.repository_columns.each_with_index do |_, index|
|
|
|
|
repository_state['columns'] << RepositoryDatatable::
|
|
|
|
REPOSITORY_TABLE_DEFAULT_STATE['columns'].first
|
|
|
|
repository_state['ColReorder'] << (default_columns_num + index)
|
|
|
|
end
|
2017-06-07 19:36:39 +08:00
|
|
|
RepositoryTableState.create(user: user,
|
|
|
|
repository: repository,
|
|
|
|
state: repository_state)
|
2017-06-06 23:35:29 +08:00
|
|
|
end
|
|
|
|
end
|