From 09e7167794fb504bc10be9d9a33e22cc4dc18b33 Mon Sep 17 00:00:00 2001 From: "A.A.Abroskin" Date: Fri, 25 Jan 2019 15:56:42 +0300 Subject: [PATCH] Add API to nullify log_data column This commit add class method .reset_log_data for nullify association and instance method #reset_log_data for nullify single record. --- lib/logidze/model.rb | 10 ++++++++++ spec/logidze/model_spec.rb | 26 ++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/lib/logidze/model.rb b/lib/logidze/model.rb index 85041a3e..7d64f93f 100644 --- a/lib/logidze/model.rb +++ b/lib/logidze/model.rb @@ -53,6 +53,11 @@ def has_logidze? true end # rubocop: enable Naming/PredicateName + + # Nullify log_data column for a association + def reset_log_data + without_logging { update_all(log_data: nil) } + end end # Use this to convert Ruby time to milliseconds @@ -204,6 +209,11 @@ def reload_log_data self.log_data = self.class.where(self.class.primary_key => id).pluck(:log_data).first end + # Nullify log_data column for a single record + def reset_log_data + self.class.without_logging { update_column(:log_data, nil) } + end + protected def apply_diff(version, diff) diff --git a/spec/logidze/model_spec.rb b/spec/logidze/model_spec.rb index 56eb6c1c..e0a2ad18 100644 --- a/spec/logidze/model_spec.rb +++ b/spec/logidze/model_spec.rb @@ -578,4 +578,30 @@ it { is_expected.to be_zero } end end + + describe '.reset_log_data' do + let!(:user2) { user.dup.tap(&:save!) } + let!(:user3) { user.dup.tap(&:save!) } + + before { User.limit(2).reset_log_data } + + it 'nullify log_data column for a association' do + expect(user.reload.log_size).to be_zero + expect(user2.reload.log_size).to be_zero + end + + it 'not affect other model records' do + expect(user3.reload.log_size).to eq 5 + end + end + + describe '#reset_log_data' do + subject { user.log_size } + + before { user.reset_log_data } + + it 'nullify log_data colulmn for a single record' do + is_expected.to be_zero + end + end end