Skip to content

Commit

Permalink
close paper-trail-gem#329; Add :touch_with_version method to PaperTra…
Browse files Browse the repository at this point in the history
…il::Model::InstanceMethods
  • Loading branch information
batter committed Feb 21, 2014
1 parent 0359704 commit 2a7225f
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 43 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
@@ -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.
Expand Down
7 changes: 7 additions & 0 deletions README.md
Expand Up @@ -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:
Expand Down
16 changes: 16 additions & 0 deletions lib/paper_trail/has_paper_trail.rb
Expand Up @@ -216,6 +216,22 @@ def without_versioning(method = nil)
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
Expand Down
102 changes: 61 additions & 41 deletions spec/models/widget_spec.rb
Expand Up @@ -5,9 +5,9 @@
it { should be_versioned }
end

describe "`versioning` option" do
let(:widget) { Widget.create :name => 'Bob', :an_integer => 1 }
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
Expand All @@ -21,60 +21,80 @@
end
end

describe "class methods" do
subject { Widget }

describe :paper_trail_off! do
it { should respond_to(:paper_trail_off!) }

it 'should set the `paper_trail_enabled_for_model?` to `false`' do
subject.paper_trail_enabled_for_model?.should be_true
subject.paper_trail_off!
subject.paper_trail_enabled_for_model?.should be_false
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 :paper_trail_off do
it { should respond_to(:paper_trail_off) }
describe "Class" do
subject { Widget }

it 'should set the invoke `paper_trail_off!`' do
subject.should_receive(:warn)
subject.should_receive(:paper_trail_off!)
subject.paper_trail_off
end
describe :paper_trail_off! do
it { should respond_to(:paper_trail_off!) }

it 'should display a deprecation warning' do
subject.should_receive(:warn).with("DEPRECATED: use `paper_trail_on!` instead of `paper_trail_on`. Support for `paper_trail_on` will be removed in PaperTrail 3.1")
subject.paper_trail_on
it 'should set the `paper_trail_enabled_for_model?` to `false`' do
subject.paper_trail_enabled_for_model?.should be_true
subject.paper_trail_off!
subject.paper_trail_enabled_for_model?.should be_false
end
end
end

describe :paper_trail_on! do
before { subject.paper_trail_off! }
describe :paper_trail_off do
it { should respond_to(:paper_trail_off) }

it { should respond_to(:paper_trail_on!) }
it 'should set the invoke `paper_trail_off!`' do
subject.should_receive(:warn)
subject.should_receive(:paper_trail_off!)
subject.paper_trail_off
end

it 'should set the `paper_trail_enabled_for_model?` to `true`' do
subject.paper_trail_enabled_for_model?.should be_false
subject.paper_trail_on!
subject.paper_trail_enabled_for_model?.should be_true
it 'should display a deprecation warning' do
subject.should_receive(:warn).with("DEPRECATED: use `paper_trail_on!` instead of `paper_trail_on`. Support for `paper_trail_on` will be removed in PaperTrail 3.1")
subject.paper_trail_on
end
end
end

describe :paper_trail_on do
before { subject.paper_trail_off! }
describe :paper_trail_on! do
before { subject.paper_trail_off! }

it { should respond_to(:paper_trail_on) }
it { should respond_to(:paper_trail_on!) }

it 'should set the invoke `paper_trail_on!`' do
subject.should_receive(:warn)
subject.should_receive(:paper_trail_on!)
subject.paper_trail_on
it 'should set the `paper_trail_enabled_for_model?` to `true`' do
subject.paper_trail_enabled_for_model?.should be_false
subject.paper_trail_on!
subject.paper_trail_enabled_for_model?.should be_true
end
end

it 'should display a deprecation warning' do
subject.should_receive(:warn).with("DEPRECATED: use `paper_trail_on!` instead of `paper_trail_on`. Support for `paper_trail_on` will be removed in PaperTrail 3.1")
subject.paper_trail_on
describe :paper_trail_on do
before { subject.paper_trail_off! }

it { should respond_to(:paper_trail_on) }

it 'should set the invoke `paper_trail_on!`' do
subject.should_receive(:warn)
subject.should_receive(:paper_trail_on!)
subject.paper_trail_on
end

it 'should display a deprecation warning' do
subject.should_receive(:warn).with("DEPRECATED: use `paper_trail_on!` instead of `paper_trail_on`. Support for `paper_trail_on` will be removed in PaperTrail 3.1")
subject.paper_trail_on
end
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions test/dummy/db/migrate/20110208155312_set_up_test_tables.rb
Expand Up @@ -10,9 +10,9 @@ def self.up
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|
Expand Down Expand Up @@ -54,7 +54,7 @@ def self.up
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|
Expand Down

0 comments on commit 2a7225f

Please sign in to comment.