mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2024-12-27 10:14:17 +08:00
Refactor repository_table_state and its class methods
This commit is contained in:
parent
5e2622ecfc
commit
d8ca42aa0c
4 changed files with 66 additions and 55 deletions
|
@ -124,10 +124,9 @@ class RepositoryColumnsController < ApplicationController
|
|||
respond_to do |format|
|
||||
format.json do
|
||||
if @repository_column.destroy
|
||||
RepositoryTableState.update_state(
|
||||
RepositoryTableState.update_states_with_removed_column(
|
||||
@del_repository_column,
|
||||
params[:repository_column][:column_index],
|
||||
current_user
|
||||
params[:repository_column][:column_index]
|
||||
)
|
||||
render json: {
|
||||
message: t('libraries.repository_columns.destroy.success_flash',
|
||||
|
|
|
@ -2,15 +2,7 @@ class UserRepositoriesController < ApplicationController
|
|||
before_action :load_vars
|
||||
|
||||
def save_table_state
|
||||
table_state = RepositoryTableState.where(user: current_user,
|
||||
repository: @repository).first
|
||||
if table_state
|
||||
table_state.update(state: params[:state])
|
||||
else
|
||||
RepositoryTableState.create(user: current_user,
|
||||
repository: @repository,
|
||||
state: params[:state])
|
||||
end
|
||||
RepositoryTableState.update_state(current_user, @repository, params[:state])
|
||||
respond_to do |format|
|
||||
format.json do
|
||||
render json: {
|
||||
|
|
|
@ -19,12 +19,12 @@ class RepositoryColumn < ApplicationRecord
|
|||
validates :repository, presence: true
|
||||
validates :data_type, presence: true
|
||||
|
||||
after_create :update_repository_table_state
|
||||
after_create :update_repository_table_states
|
||||
|
||||
scope :list_type, -> { where(data_type: 'RepositoryListValue') }
|
||||
|
||||
def update_repository_table_state
|
||||
RepositoryTableState.update_state(self, nil, created_by)
|
||||
def update_repository_table_states
|
||||
RepositoryTableState.update_states_with_new_column(self)
|
||||
end
|
||||
|
||||
def importable?
|
||||
|
|
|
@ -12,50 +12,20 @@ class RepositoryTableState < ApplicationRecord
|
|||
def self.load_state(user, repository)
|
||||
state = where(user: user, repository: repository).take
|
||||
if state.blank?
|
||||
state = RepositoryTableState.create_state(user, repository)
|
||||
state = RepositoryTableState.create_default_state(user, repository)
|
||||
end
|
||||
state
|
||||
end
|
||||
|
||||
def self.update_state(custom_column, column_index, user)
|
||||
# table state of every user having access to this repository needs udpating
|
||||
table_states = RepositoryTableState.where(
|
||||
repository: custom_column.repository
|
||||
)
|
||||
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
|
||||
end
|
||||
|
||||
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
|
||||
end
|
||||
else
|
||||
# add column
|
||||
index = repository_state['columns'].count
|
||||
repository_state['columns'][index] =
|
||||
Constants::REPOSITORY_TABLE_DEFAULT_STATE[:columns].first
|
||||
repository_state['ColReorder'].insert(2, index.to_s)
|
||||
end
|
||||
table_state.update(state: repository_state)
|
||||
end
|
||||
def self.update_state(user, repository, state)
|
||||
RepositoryTableState.load_state(user, repository)
|
||||
.update(state: state)
|
||||
end
|
||||
|
||||
def self.create_state(user, repository)
|
||||
def self.create_default_state(user, repository)
|
||||
# Destroy any state object before recreating a new one
|
||||
RepositoryTableState.where(user: user, repository: repository).destroy
|
||||
|
||||
default_columns_num =
|
||||
Constants::REPOSITORY_TABLE_DEFAULT_STATE[:length]
|
||||
|
||||
|
@ -74,7 +44,57 @@ class RepositoryTableState < ApplicationRecord
|
|||
state['length'] = state['columns'].length.to_s
|
||||
state['time'] = Time.new.to_i.to_s
|
||||
return RepositoryTableState.create(user: user,
|
||||
repository: repository,
|
||||
state: state)
|
||||
repository: repository,
|
||||
state: state)
|
||||
end
|
||||
|
||||
def self.update_states_with_new_column(new_column)
|
||||
RepositoryTableState.where(
|
||||
repository: new_column.repository
|
||||
).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 self.update_states_with_removed_column(repository, old_column_index)
|
||||
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
|
||||
state['length'] = (state['length'].to_i - 1).to_s
|
||||
state['time'] = Time.new.to_i.to_s
|
||||
table_state.save
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue