From 6c7987681c54e1e32bcf6b5d441a79c951194581 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Zrim=C5=A1ek?= Date: Tue, 20 Jun 2017 14:52:41 +0200 Subject: [PATCH] Updating repository table state of every user belonging to the team of the repository, when custom column is removed or added. [SCI-1384] --- app/models/repository_table_state.rb | 56 ++++++++++++++-------------- 1 file changed, 29 insertions(+), 27 deletions(-) diff --git a/app/models/repository_table_state.rb b/app/models/repository_table_state.rb index 27af3e2d1..6e40e2ea7 100644 --- a/app/models/repository_table_state.rb +++ b/app/models/repository_table_state.rb @@ -14,39 +14,41 @@ class RepositoryTableState < ActiveRecord::Base end def self.update_state(custom_column, column_index, user) - table_state = RepositoryTableState.where( - user: user, + # table state of every user having access to this repository needs udpating + table_states = RepositoryTableState.where( repository: custom_column.repository ) - return if table_state.empty? - repository_state = table_state.first['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 + 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 - 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 + + 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] = RepositoryDatatable:: + REPOSITORY_TABLE_DEFAULT_STATE['columns'].first + repository_state['ColReorder'].insert(2, index) end - else - # add column - index = repository_state['columns'].count - repository_state['columns'][index] = RepositoryDatatable:: - REPOSITORY_TABLE_DEFAULT_STATE['columns'].first - repository_state['ColReorder'].insert(2, index) + table_state.update(state: repository_state) end - table_state.first.update(state: repository_state) end def self.create_state(user, repository)