From 5b710241e1a965d980ff89abaa33164768bf850a Mon Sep 17 00:00:00 2001 From: Urban Rotnik Date: Mon, 9 Dec 2019 08:34:27 +0100 Subject: [PATCH] Add delete condition to update_data method on CellValues --- app/models/repository_asset_value.rb | 2 ++ .../repository_date_time_range_value_base.rb | 2 ++ app/models/repository_date_time_value_base.rb | 2 ++ app/models/repository_list_value.rb | 2 ++ app/models/repository_status_value.rb | 2 ++ app/models/repository_text_value.rb | 2 ++ spec/models/repository_asset_value_spec.rb | 32 ++++++++++++------- ...ository_date_time_range_value_base_spec.rb | 22 ++++++++++--- .../repository_date_time_value_base_spec.rb | 20 +++++++++--- spec/models/repository_list_value_spec.rb | 19 ++++++++--- spec/models/repository_status_value_spec.rb | 20 +++++++++--- spec/models/repository_text_value_spec.rb | 20 +++++++++--- 12 files changed, 109 insertions(+), 36 deletions(-) diff --git a/app/models/repository_asset_value.rb b/app/models/repository_asset_value.rb index f9ffb94ca..24ca0400b 100644 --- a/app/models/repository_asset_value.rb +++ b/app/models/repository_asset_value.rb @@ -33,6 +33,8 @@ class RepositoryAssetValue < ApplicationRecord end def update_data!(new_data, user) + destroy! && return if new_data == '-1' + if new_data[:signed_url] asset.update(file: new_data[:sigend_url]) else diff --git a/app/models/repository_date_time_range_value_base.rb b/app/models/repository_date_time_range_value_base.rb index 0e0c5348b..0678ad845 100644 --- a/app/models/repository_date_time_range_value_base.rb +++ b/app/models/repository_date_time_range_value_base.rb @@ -16,6 +16,8 @@ class RepositoryDateTimeRangeValueBase < ApplicationRecord SORTABLE_VALUE_INCLUDE = :repository_date_time_range_value def update_data!(new_data, user) + destroy! && return if new_data == '' + self.start_time = Time.zone.parse(new_data[:start_time]) self.end_time = Time.zone.parse(new_data[:end_time]) self.last_modified_by = user diff --git a/app/models/repository_date_time_value_base.rb b/app/models/repository_date_time_value_base.rb index ae42ca34f..d93ac0159 100644 --- a/app/models/repository_date_time_value_base.rb +++ b/app/models/repository_date_time_value_base.rb @@ -16,6 +16,8 @@ class RepositoryDateTimeValueBase < ApplicationRecord SORTABLE_VALUE_INCLUDE = :repository_date_time_value def update_data!(new_data, user) + destroy! && return if new_data == '' + self.data = Time.zone.parse(new_data) self.last_modified_by = user save! diff --git a/app/models/repository_list_value.rb b/app/models/repository_list_value.rb index 1ef154bef..0ad905372 100644 --- a/app/models/repository_list_value.rb +++ b/app/models/repository_list_value.rb @@ -34,6 +34,8 @@ class RepositoryListValue < ApplicationRecord end def update_data!(new_data, user) + destroy! && return if new_data == '-1' + self.repository_list_item_id = new_data.to_i self.last_modified_by = user save! diff --git a/app/models/repository_status_value.rb b/app/models/repository_status_value.rb index 7bd6bed65..a63115d9f 100644 --- a/app/models/repository_status_value.rb +++ b/app/models/repository_status_value.rb @@ -23,6 +23,8 @@ class RepositoryStatusValue < ApplicationRecord end def update_data!(new_data, user) + destroy! && return if new_data == '-1' + self.repository_status_item_id = new_data.to_i self.last_modified_by = user save! diff --git a/app/models/repository_text_value.rb b/app/models/repository_text_value.rb index 61eb31857..5c058fe26 100644 --- a/app/models/repository_text_value.rb +++ b/app/models/repository_text_value.rb @@ -23,6 +23,8 @@ class RepositoryTextValue < ApplicationRecord end def update_data!(new_data, user) + destroy! && return if new_data == '' + self.data = new_data self.last_modified_by = user save! diff --git a/spec/models/repository_asset_value_spec.rb b/spec/models/repository_asset_value_spec.rb index e96eb8abb..84ed87c6c 100644 --- a/spec/models/repository_asset_value_spec.rb +++ b/spec/models/repository_asset_value_spec.rb @@ -64,23 +64,33 @@ describe RepositoryAssetValue, type: :model do } end - context 'when has signed_url' do - it 'should change last_modified_by and data' do - repository_asset_value.save + context 'when update data' do + context 'when has signed_url' do + it 'should change last_modified_by and data' do + repository_asset_value.save - expect { repository_asset_value.update_data!(new_file_with_signed_url, user) } - .to(change { repository_asset_value.reload.last_modified_by.id } - .and(change { repository_asset_value.reload.data })) + expect { repository_asset_value.update_data!(new_file_with_signed_url, user) } + .to(change { repository_asset_value.reload.last_modified_by.id } + .and(change { repository_asset_value.reload.data })) + end + end + + context 'when has base464 file' do + it 'should change last_modified_by and data' do + repository_asset_value.save + + expect { repository_asset_value.update_data!(new_file_base64, user) } + .to(change { repository_asset_value.reload.last_modified_by.id } + .and(change { repository_asset_value.reload.data })) + end end end - context 'when has base464 file' do - it 'should change last_modified_by and data' do + context 'when delete cell value' do + it do repository_asset_value.save - expect { repository_asset_value.update_data!(new_file_base64, user) } - .to(change { repository_asset_value.reload.last_modified_by.id } - .and(change { repository_asset_value.reload.data })) + expect { repository_asset_value.update_data!('-1', user) }.to change(RepositoryAssetValue, :count).by(-1) end end end diff --git a/spec/models/repository_date_time_range_value_base_spec.rb b/spec/models/repository_date_time_range_value_base_spec.rb index 2147cf356..f1bfe0310 100644 --- a/spec/models/repository_date_time_range_value_base_spec.rb +++ b/spec/models/repository_date_time_range_value_base_spec.rb @@ -37,12 +37,24 @@ describe RepositoryDateTimeRangeValueBase, type: :model do end_time: (Time.zone.now + 2.days).utc.to_s } end - it 'should change last_modified_by and dates' do - repository_date_time_range_value.save - expect { repository_date_time_range_value.update_data!(new_times, user) } - .to(change { repository_date_time_range_value.reload.last_modified_by.id } - .and(change { repository_date_time_range_value.reload.data })) + context 'when update data' do + it 'should change last_modified_by and dates' do + repository_date_time_range_value.save + + expect { repository_date_time_range_value.update_data!(new_times, user) } + .to(change { repository_date_time_range_value.reload.last_modified_by.id } + .and(change { repository_date_time_range_value.reload.data })) + end + end + + context 'when delete cell value' do + it do + repository_date_time_range_value.save + + expect { repository_date_time_range_value.update_data!('', user) } + .to(change(RepositoryDateTimeRangeValue, :count).by(-1)) + end end end diff --git a/spec/models/repository_date_time_value_base_spec.rb b/spec/models/repository_date_time_value_base_spec.rb index 42d471e90..642356c98 100644 --- a/spec/models/repository_date_time_value_base_spec.rb +++ b/spec/models/repository_date_time_value_base_spec.rb @@ -29,12 +29,22 @@ describe RepositoryDateTimeValueBase, type: :model do let(:user) { create :user } let(:new_data) { Time.now.utc.to_s } - it 'should change last_modified_by and dates' do - repository_date_time_value.save + context 'when update data' do + it 'should change last_modified_by and dates' do + repository_date_time_value.save - expect { repository_date_time_value.update_data!(new_data, user) } - .to(change { repository_date_time_value.reload.last_modified_by.id } - .and(change { repository_date_time_value.reload.data })) + expect { repository_date_time_value.update_data!(new_data, user) } + .to(change { repository_date_time_value.reload.last_modified_by.id } + .and(change { repository_date_time_value.reload.data })) + end + end + + context 'when delete cell value' do + it do + repository_date_time_value.save + + expect { repository_date_time_value.update_data!('', user) }.to change(RepositoryDateTimeValue, :count).by(-1) + end end end end diff --git a/spec/models/repository_list_value_spec.rb b/spec/models/repository_list_value_spec.rb index f91488b91..9dc896858 100644 --- a/spec/models/repository_list_value_spec.rb +++ b/spec/models/repository_list_value_spec.rb @@ -75,13 +75,22 @@ RSpec.describe RepositoryListValue, type: :model do let(:new_list_item) do create :repository_list_item, repository_column: repository_list_value.repository_list_item.repository_column end + context 'when update data' do + it 'should change last_modified_by and data' do + repository_list_value.save - it 'should change last_modified_by and data' do - repository_list_value.save + expect { repository_list_value.update_data!(new_list_item.id, user) } + .to(change { repository_list_value.reload.last_modified_by.id } + .and(change { repository_list_value.reload.data })) + end + end - expect { repository_list_value.update_data!(new_list_item.id, user) } - .to(change { repository_list_value.reload.last_modified_by.id } - .and(change { repository_list_value.reload.data })) + context 'when delete cell value' do + it do + repository_list_value.save + + expect { repository_list_value.update_data!('-1', user) }.to change(RepositoryListValue, :count).by(-1) + end end end end diff --git a/spec/models/repository_status_value_spec.rb b/spec/models/repository_status_value_spec.rb index 7ec58520e..0e35da931 100644 --- a/spec/models/repository_status_value_spec.rb +++ b/spec/models/repository_status_value_spec.rb @@ -45,12 +45,22 @@ describe RepositoryStatusValue do repository_column: repository_status_value.repository_status_item.repository_column end - it 'should change last_modified_by and data' do - repository_status_value.save + context 'when update data' do + it 'should change last_modified_by and data' do + repository_status_value.save - expect { repository_status_value.update_data!(new_status_item.id, user) } - .to(change { repository_status_value.reload.last_modified_by.id } - .and(change { repository_status_value.reload.data })) + expect { repository_status_value.update_data!(new_status_item.id, user) } + .to(change { repository_status_value.reload.last_modified_by.id } + .and(change { repository_status_value.reload.data })) + end + end + + context 'when delete cell value' do + it do + repository_status_value.save + + expect { repository_status_value.update_data!('-1', user) }.to change(RepositoryStatusValue, :count).by(-1) + end end end end diff --git a/spec/models/repository_text_value_spec.rb b/spec/models/repository_text_value_spec.rb index f4e8c41e7..6f6bd48ee 100644 --- a/spec/models/repository_text_value_spec.rb +++ b/spec/models/repository_text_value_spec.rb @@ -53,12 +53,22 @@ describe RepositoryTextValue, type: :model do describe 'update_data!' do let(:user) { create :user } - it 'should change last_modified_by and data' do - repository_text_value.save + context 'when update data' do + it 'should change last_modified_by and data' do + repository_text_value.save - expect { repository_text_value.update_data!('newData', user) } - .to(change { repository_text_value.reload.last_modified_by.id } - .and(change { repository_text_value.reload.data })) + expect { repository_text_value.update_data!('newData', user) } + .to(change { repository_text_value.reload.last_modified_by.id } + .and(change { repository_text_value.reload.data })) + end + end + + context 'when delete cell value' do + it do + repository_text_value.save + + expect { repository_text_value.update_data!('', user) }.to change(RepositoryTextValue, :count).by(-1) + end end end end