Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add API to nullify log_data column #110

Merged
merged 3 commits into from
Feb 1, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,18 @@ Logidze.without_logging { Post.update_all(seen: true) }
Post.without_logging { Post.update_all(seen: true) }
```

## Reset log

Reset the history for a record (or records):

```ruby
# for single record
record.reset_log_data

# for relation
User.where(...).reset_log_data
```

## Log format

The `log_data` column has the following format:
Expand Down
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 column for a single record' do
is_expected.to be_zero
end
end
end