Skip to content

Commit

Permalink
Add API to nullify log_data column
Browse files Browse the repository at this point in the history
This commit add class method .reset_log_data for nullify
association and instance method #reset_log_data for nullify
single record.
  • Loading branch information
A.A.Abroskin committed Jan 25, 2019
1 parent fe650af commit 09e7167
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/logidze/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
26 changes: 26 additions & 0 deletions spec/logidze/model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 09e7167

Please sign in to comment.