From d35f7119b7b9910c8ef5a3123d0a927e2060115e Mon Sep 17 00:00:00 2001 From: Urban Rotnik Date: Tue, 10 Dec 2019 13:58:47 +0100 Subject: [PATCH] Moved delete action to service --- app/models/repository_asset_value.rb | 2 -- app/models/repository_date_time_range_value_base.rb | 2 -- app/models/repository_date_time_value_base.rb | 4 +--- app/models/repository_list_value.rb | 2 -- app/models/repository_status_value.rb | 2 -- app/models/repository_text_value.rb | 2 -- .../repository_rows/create_repository_row_service.rb | 3 +-- .../repository_rows/update_repository_row_service.rb | 7 +++++++ spec/models/repository_asset_value_spec.rb | 8 -------- .../models/repository_date_time_range_value_base_spec.rb | 9 --------- spec/models/repository_date_time_value_base_spec.rb | 8 -------- spec/models/repository_list_value_spec.rb | 8 -------- spec/models/repository_status_value_spec.rb | 8 -------- spec/models/repository_text_value_spec.rb | 8 -------- 14 files changed, 9 insertions(+), 64 deletions(-) diff --git a/app/models/repository_asset_value.rb b/app/models/repository_asset_value.rb index 812939b5b..49ee94d3e 100644 --- a/app/models/repository_asset_value.rb +++ b/app/models/repository_asset_value.rb @@ -33,8 +33,6 @@ class RepositoryAssetValue < ApplicationRecord end def update_data!(new_data, user) - destroy! && return if new_data == '-1' - if new_data.is_a?(String) # assume it's a signed_id_token asset.file.attach(new_data) elsif new_data[:file_data] diff --git a/app/models/repository_date_time_range_value_base.rb b/app/models/repository_date_time_range_value_base.rb index 3be90358f..08ddf5542 100644 --- a/app/models/repository_date_time_range_value_base.rb +++ b/app/models/repository_date_time_range_value_base.rb @@ -16,8 +16,6 @@ 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 305f1e219..e827c8467 100644 --- a/app/models/repository_date_time_value_base.rb +++ b/app/models/repository_date_time_value_base.rb @@ -16,8 +16,6 @@ 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! @@ -26,6 +24,6 @@ class RepositoryDateTimeValueBase < ApplicationRecord private def formatted(format) - I18n.l(d, format: format) + I18n.l(data, format: format) end end diff --git a/app/models/repository_list_value.rb b/app/models/repository_list_value.rb index 0ad905372..1ef154bef 100644 --- a/app/models/repository_list_value.rb +++ b/app/models/repository_list_value.rb @@ -34,8 +34,6 @@ 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 95262aecd..e26994707 100644 --- a/app/models/repository_status_value.rb +++ b/app/models/repository_status_value.rb @@ -23,8 +23,6 @@ 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 5c058fe26..61eb31857 100644 --- a/app/models/repository_text_value.rb +++ b/app/models/repository_text_value.rb @@ -23,8 +23,6 @@ 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/app/services/repository_rows/create_repository_row_service.rb b/app/services/repository_rows/create_repository_row_service.rb index 87c601b0c..c8bb62b62 100644 --- a/app/services/repository_rows/create_repository_row_service.rb +++ b/app/services/repository_rows/create_repository_row_service.rb @@ -18,8 +18,7 @@ module RepositoryRows return self unless valid? ActiveRecord::Base.transaction do - @repository_row = RepositoryRow.new(params[:repository_row]) - @repository_row.repository = @repository + @repository_row = @repository.repository_rows.new(params[:repository_row]) @repository_row.last_modified_by = @user @repository_row.created_by = @user @repository_row.save! diff --git a/app/services/repository_rows/update_repository_row_service.rb b/app/services/repository_rows/update_repository_row_service.rb index 507f31ca4..d358c85d3 100644 --- a/app/services/repository_rows/update_repository_row_service.rb +++ b/app/services/repository_rows/update_repository_row_service.rb @@ -24,6 +24,13 @@ module RepositoryRows next unless column cell = @repository_row.repository_cells.find_by(repository_column_id: column.id) + + if cell && value.empty? + cell.destroy! + @record_updated = true + next + end + unless cell RepositoryCell.create_with_value!(@repository_row, column, value, @user) @record_updated = true diff --git a/spec/models/repository_asset_value_spec.rb b/spec/models/repository_asset_value_spec.rb index 9c5511dc5..60c2345e2 100644 --- a/spec/models/repository_asset_value_spec.rb +++ b/spec/models/repository_asset_value_spec.rb @@ -81,14 +81,6 @@ describe RepositoryAssetValue, type: :model do end end end - - context 'when delete cell value' do - it do - repository_asset_value.save - - expect { repository_asset_value.update_data!('-1', user) }.to change(RepositoryAssetValue, :count).by(-1) - end - end end describe 'self.new_with_payload' do 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 f1bfe0310..e79886229 100644 --- a/spec/models/repository_date_time_range_value_base_spec.rb +++ b/spec/models/repository_date_time_range_value_base_spec.rb @@ -47,15 +47,6 @@ describe RepositoryDateTimeRangeValueBase, type: :model do .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 describe '.data' do diff --git a/spec/models/repository_date_time_value_base_spec.rb b/spec/models/repository_date_time_value_base_spec.rb index 642356c98..a178cfaf0 100644 --- a/spec/models/repository_date_time_value_base_spec.rb +++ b/spec/models/repository_date_time_value_base_spec.rb @@ -38,13 +38,5 @@ describe RepositoryDateTimeValueBase, type: :model do .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 d51a00aba..188e0499f 100644 --- a/spec/models/repository_list_value_spec.rb +++ b/spec/models/repository_list_value_spec.rb @@ -84,14 +84,6 @@ RSpec.describe RepositoryListValue, type: :model do .and(change { repository_list_value.reload.data })) end end - - 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 describe 'self.new_with_payload' do diff --git a/spec/models/repository_status_value_spec.rb b/spec/models/repository_status_value_spec.rb index 1d1c3eb54..8e2f799c1 100644 --- a/spec/models/repository_status_value_spec.rb +++ b/spec/models/repository_status_value_spec.rb @@ -54,14 +54,6 @@ describe RepositoryStatusValue do .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 describe 'self.new_with_payload' do diff --git a/spec/models/repository_text_value_spec.rb b/spec/models/repository_text_value_spec.rb index e91db616f..33dadbbdb 100644 --- a/spec/models/repository_text_value_spec.rb +++ b/spec/models/repository_text_value_spec.rb @@ -62,14 +62,6 @@ describe RepositoryTextValue, type: :model do .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 describe 'self.new_with_payload' do