mirror of
https://github.com/paper-trail-gem/paper_trail.git
synced 2022-11-09 11:33:19 -05:00
Add version limit config option. Close #213
This commit is contained in:
parent
9da4930ab5
commit
dbb4f28325
6 changed files with 59 additions and 1 deletions
|
@ -4,6 +4,8 @@
|
|||
so that `current_user` only gets invoked if it is defined.
|
||||
- [#219](https://github.com/airblade/paper_trail/pull/219) - Fixed issue where attributes stored with `nil` value might not get
|
||||
reified properly depending on the way the serializer worked.
|
||||
- [#213](https://github.com/airblade/paper_trail/issues/213) - Added a `version_limit` option to the `PaperTrail::Config` options
|
||||
that can be used to restrict the number of versions PaperTrail will store per object instance.
|
||||
- [#187](https://github.com/airblade/paper_trail/pull/187) - Confirmed JRuby support.
|
||||
- [#174](https://github.com/airblade/paper_trail/pull/174) - The `event` field on the versions table can now be customized.
|
||||
|
||||
|
|
11
README.md
11
README.md
|
@ -771,6 +771,17 @@ A valid serializer is a `module` (or `class`) that defines a `load` and `dump` m
|
|||
* [Yaml](https://github.com/airblade/paper_trail/blob/master/lib/paper_trail/serializers/yaml.rb) - Default
|
||||
* [Json](https://github.com/airblade/paper_trail/blob/master/lib/paper_trail/serializers/json.rb)
|
||||
|
||||
## Limiting the number of versions created per object instance
|
||||
|
||||
If you are weary of your `versions` table growing to an unwieldy size, or just don't care to track more than a certain number of versions about an object,
|
||||
there is a configuration option that can be set to cap the number of versions saved per object. Note that if this value is set to a numeric value, the number
|
||||
version marking the `create` event will always be preserved, so it actually has a version limit of 1 higher than what this value is set to.
|
||||
|
||||
```ruby
|
||||
>> PaperTrail.config.version_limit = 3 # will make it so that a maximum of 4 versions will be stored for each object (3 most recent ones plus a `create` event)
|
||||
>> PaperTrail.config.version_limit = nil # disables/removes the version limit
|
||||
```
|
||||
|
||||
## Deleting Old Versions
|
||||
|
||||
Over time your `versions` table will grow to an unwieldy size. Because each version is self-contained (see the Diffing section above for more) you can simply delete any records you don't want any more. For example:
|
||||
|
|
|
@ -3,7 +3,7 @@ require 'singleton'
|
|||
module PaperTrail
|
||||
class Config
|
||||
include Singleton
|
||||
attr_accessor :enabled, :timestamp_field, :serializer
|
||||
attr_accessor :enabled, :timestamp_field, :serializer, :version_limit
|
||||
|
||||
def initialize
|
||||
@enabled = true # Indicates whether PaperTrail is on or off.
|
||||
|
|
|
@ -3,6 +3,8 @@ class Version < ActiveRecord::Base
|
|||
validates_presence_of :event
|
||||
attr_accessible :item_type, :item_id, :event, :whodunnit, :object, :object_changes
|
||||
|
||||
after_create :enforce_version_limit!
|
||||
|
||||
def self.with_item_keys(item_type, item_id)
|
||||
where :item_type => item_type, :item_id => item_id
|
||||
end
|
||||
|
@ -19,6 +21,10 @@ class Version < ActiveRecord::Base
|
|||
where :event => 'destroy'
|
||||
end
|
||||
|
||||
def self.not_creates
|
||||
where 'event <> ?', 'create'
|
||||
end
|
||||
|
||||
scope :subsequent, lambda { |version|
|
||||
where("#{self.primary_key} > ?", version).order("#{self.primary_key} ASC")
|
||||
}
|
||||
|
@ -180,4 +186,13 @@ class Version < ActiveRecord::Base
|
|||
end
|
||||
end
|
||||
|
||||
# checks to see if a value has been set for the `version_limit` config option, and if so enforces it
|
||||
def enforce_version_limit!
|
||||
return unless PaperTrail.config.version_limit.is_a? Numeric
|
||||
previous_versions = sibling_versions.not_creates
|
||||
return unless previous_versions.size > PaperTrail.config.version_limit
|
||||
excess_previous_versions = previous_versions - previous_versions.last(PaperTrail.config.version_limit)
|
||||
excess_previous_versions.map(&:destroy)
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -1266,6 +1266,22 @@ class HasPaperTrailModelTest < ActiveSupport::TestCase
|
|||
end
|
||||
end
|
||||
|
||||
context '`PaperTrail::Config.version_limit` set' do
|
||||
setup do
|
||||
PaperTrail.config.version_limit = 2
|
||||
@widget = Widget.create! :name => 'Henry'
|
||||
6.times { @widget.update_attribute(:name, Faker::Lorem.word) }
|
||||
end
|
||||
|
||||
teardown { PaperTrail.config.version_limit = nil }
|
||||
|
||||
should "limit the number of versions to 3 (2 plus the created at event)" do
|
||||
assert_equal 'create', @widget.versions.first.event
|
||||
assert_equal 3, @widget.versions.size
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
private
|
||||
|
||||
# Updates `model`'s last version so it looks like the version was
|
||||
|
|
|
@ -40,4 +40,18 @@ class VersionTest < ActiveSupport::TestCase
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
context "Version.not_creates" do
|
||||
setup {
|
||||
@article.update_attributes(:name => 'Animal')
|
||||
@article.destroy
|
||||
assert Version.not_creates.present?
|
||||
}
|
||||
|
||||
should "return all items except create events" do
|
||||
Version.not_creates.each do |version|
|
||||
assert_not_equal "create", version.event
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue