Skip to content

Commit

Permalink
close #312; Fix RSpec 'with_versioning' class helper method
Browse files Browse the repository at this point in the history
  • Loading branch information
batter committed Mar 11, 2014
1 parent ed2e3d5 commit b75b009
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 23 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
instead of `current_user` (if `current_user` is defined).
- [#313](/~https://github.com/airblade/paper_trail/pull/313) - Make the `Rails::Controller` helper compatible with
`ActionController::API` for compatibility with the [`rails-api`](/~https://github.com/rails-api/rails-api) gem.
- [#312](/~https://github.com/airblade/paper_trail/issues/312) - Fix RSpec `with_versioning` class level helper method.
- Deprecated `Model.paper_trail_on` and `Model.paper_trail_off` in favor of bang versions of the methods. Deprecation warning
informs users that the non-bang versions of the methods will be removed in version `3.1.0`.

Expand Down
5 changes: 3 additions & 2 deletions lib/paper_trail/frameworks/rspec.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
require 'rspec/core'
require 'rspec/matchers'
require File.expand_path('../rspec/extensions', __FILE__)
require 'paper_trail/frameworks/rspec/helpers'

RSpec.configure do |config|
config.include ::PaperTrail::RSpec::Extensions
config.include ::PaperTrail::RSpec::Helpers::InstanceMethods
config.extend ::PaperTrail::RSpec::Helpers::ClassMethods

config.before(:each) do
::PaperTrail.enabled = false
Expand Down
20 changes: 0 additions & 20 deletions lib/paper_trail/frameworks/rspec/extensions.rb

This file was deleted.

27 changes: 27 additions & 0 deletions lib/paper_trail/frameworks/rspec/helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module PaperTrail
module RSpec
module Helpers
module InstanceMethods
# enable versioning for specific blocks (at instance-level)
def with_versioning
was_enabled = ::PaperTrail.enabled?
::PaperTrail.enabled = true
begin
yield
ensure
::PaperTrail.enabled = was_enabled
end
end
end

module ClassMethods
# enable versioning for specific blocks (at class-level)
def with_versioning(&block)
context 'with versioning', :versioning => true do
class_exec(&block)
end
end
end
end
end
end
19 changes: 18 additions & 1 deletion spec/paper_trail_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
it 'should have versioning off by default' do
::PaperTrail.should_not be_enabled
end
it 'should turn versioning on in a with_versioning block' do
it 'should turn versioning on in a `with_versioning` block' do
::PaperTrail.should_not be_enabled
with_versioning do
::PaperTrail.should be_enabled
Expand All @@ -27,6 +27,23 @@
end
end

context '`with_versioning` block at class level' do
it { ::PaperTrail.should_not be_enabled }

with_versioning do
it 'should have versioning on by default' do
::PaperTrail.should be_enabled
end
end
it 'should not leak the `enabled?` state into successive tests' do
::PaperTrail.should_not be_enabled
end
end

it 'should not leak the `enabled?` state into successive tests' do
::PaperTrail.should_not be_enabled
end

describe :whodunnit do
before(:all) { ::PaperTrail.whodunnit = 'foobar' }

Expand Down

0 comments on commit b75b009

Please sign in to comment.