close #329; Add :touch_with_version method to PaperTrail::Model::InstanceMethods
This commit is contained in:
parent
0359704751
commit
2a7225f7ca
|
@ -1,5 +1,7 @@
|
|||
## 3.0.1 (Unreleased)
|
||||
|
||||
- [#329](https://github.com/airblade/paper_trail/issues/329) - Add `touch_with_version` method to `PaperTrail::Model::InstanceMethods`,
|
||||
to allow for generating a version `touch`ing a model.
|
||||
- [#328](https://github.com/airblade/paper_trail/pull/328) / [#326](https://github.com/airblade/paper_trail/issues/326)/
|
||||
[#307](https://github.com/airblade/paper_trail/issues/307) - `Model.paper_trail_enabled_for_model?` and
|
||||
`model_instance.without_versioning` is now thread-safe.
|
||||
|
|
|
@ -126,11 +126,18 @@ widget.previous_version
|
|||
# Returns the widget (not a version) as it became next.
|
||||
widget.next_version
|
||||
|
||||
# Generates a version for a `touch` event (`widget.touch` does NOT generate a version)
|
||||
widget.touch_with_version
|
||||
|
||||
# Turn PaperTrail off for all widgets.
|
||||
Widget.paper_trail_off!
|
||||
|
||||
# Turn PaperTrail on for all widgets.
|
||||
Widget.paper_trail_on!
|
||||
|
||||
# Check wheter PaperTrail is enabled for all widgets
|
||||
Widget.paper_trail_enabled_for_model?
|
||||
widget.paper_trail_enabled_for_model?
|
||||
```
|
||||
|
||||
And a `PaperTrail::Version` instance has these methods:
|
||||
|
|
|
@ -216,6 +216,22 @@ module PaperTrail
|
|||
self.class.paper_trail_on! if paper_trail_was_enabled
|
||||
end
|
||||
|
||||
# Mimicks behavior of `touch` method from `ActiveRecord::Persistence`, but generates a version
|
||||
#
|
||||
# TODO: lookinto leveraging the `after_touch` callback from `ActiveRecord` to allow the
|
||||
# regular `touch` method go generate a version as normal. May make sense to switch the `record_update`
|
||||
# method to leverage an `after_update` callback anyways (likely for v3.1.0)
|
||||
def touch_with_version(name = nil)
|
||||
raise ActiveRecordError, "can not touch on a new record object" unless persisted?
|
||||
|
||||
attributes = timestamp_attributes_for_update_in_model
|
||||
attributes << name if name
|
||||
current_time = current_time_from_proper_timezone
|
||||
|
||||
attributes.each { |column| write_attribute(column, current_time) }
|
||||
save!
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def source_version
|
||||
|
|
|
@ -5,9 +5,9 @@ describe Widget do
|
|||
it { should be_versioned }
|
||||
end
|
||||
|
||||
describe "`versioning` option" do
|
||||
let(:widget) { Widget.create :name => 'Bob', :an_integer => 1 }
|
||||
|
||||
describe "`versioning` option" do
|
||||
context :enabled, :versioning => true do
|
||||
it 'should enable versioning for models wrapped within a block' do
|
||||
widget.versions.size.should == 1
|
||||
|
@ -21,7 +21,26 @@ describe Widget do
|
|||
end
|
||||
end
|
||||
|
||||
describe "class methods" do
|
||||
describe "Methods" do
|
||||
describe "Instance", :versioning => true do
|
||||
describe :touch_with_version do
|
||||
it { should respond_to(:touch_with_version) }
|
||||
|
||||
it "should generate a version" do
|
||||
count = widget.versions.size
|
||||
widget.touch_with_version
|
||||
widget.versions.size.should == count + 1
|
||||
end
|
||||
|
||||
it "should increment the `:updated_at` timestamp" do
|
||||
time_was = widget.updated_at
|
||||
widget.touch_with_version
|
||||
widget.updated_at.should > time_was
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "Class" do
|
||||
subject { Widget }
|
||||
|
||||
describe :paper_trail_off! do
|
||||
|
@ -78,4 +97,5 @@ describe Widget do
|
|||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -10,9 +10,9 @@ class SetUpTestTables < ActiveRecord::Migration
|
|||
t.time :a_time
|
||||
t.date :a_date
|
||||
t.boolean :a_boolean
|
||||
t.datetime :created_at, :updated_at
|
||||
t.string :sacrificial_column
|
||||
t.string :type
|
||||
t.timestamps
|
||||
end
|
||||
|
||||
create_table :versions, :force => true do |t|
|
||||
|
@ -54,7 +54,7 @@ class SetUpTestTables < ActiveRecord::Migration
|
|||
create_table :wotsits, :force => true do |t|
|
||||
t.integer :widget_id
|
||||
t.string :name
|
||||
t.datetime :created_at, :updated_at
|
||||
t.timestamps
|
||||
end
|
||||
|
||||
create_table :fluxors, :force => true do |t|
|
||||
|
|
Loading…
Reference in New Issue