From 52823d2fb2af035c03d789a4e8a2419312174bb4 Mon Sep 17 00:00:00 2001 From: Ben Atkins Date: Tue, 11 Mar 2014 10:48:06 -0400 Subject: [PATCH] close #312; Fix RSpec 'with_versioning' class helper method --- CHANGELOG.md | 1 + lib/paper_trail/frameworks/rspec.rb | 5 ++-- .../frameworks/rspec/extensions.rb | 20 -------------- lib/paper_trail/frameworks/rspec/helpers.rb | 27 +++++++++++++++++++ spec/paper_trail_spec.rb | 15 ++++++++++- 5 files changed, 45 insertions(+), 23 deletions(-) delete mode 100644 lib/paper_trail/frameworks/rspec/extensions.rb create mode 100644 lib/paper_trail/frameworks/rspec/helpers.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index b9f2be50..4cdee792 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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`. diff --git a/lib/paper_trail/frameworks/rspec.rb b/lib/paper_trail/frameworks/rspec.rb index 174a9992..564208c6 100644 --- a/lib/paper_trail/frameworks/rspec.rb +++ b/lib/paper_trail/frameworks/rspec.rb @@ -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 diff --git a/lib/paper_trail/frameworks/rspec/extensions.rb b/lib/paper_trail/frameworks/rspec/extensions.rb deleted file mode 100644 index c2149918..00000000 --- a/lib/paper_trail/frameworks/rspec/extensions.rb +++ /dev/null @@ -1,20 +0,0 @@ -module PaperTrail - module RSpec - module Extensions - # :call-seq: - # with_versioning - # - # enable versioning for specific blocks - - def with_versioning - was_enabled = ::PaperTrail.enabled? - ::PaperTrail.enabled = true - begin - yield - ensure - ::PaperTrail.enabled = was_enabled - end - end - end - end -end diff --git a/lib/paper_trail/frameworks/rspec/helpers.rb b/lib/paper_trail/frameworks/rspec/helpers.rb new file mode 100644 index 00000000..eabfba99 --- /dev/null +++ b/lib/paper_trail/frameworks/rspec/helpers.rb @@ -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 diff --git a/spec/paper_trail_spec.rb b/spec/paper_trail_spec.rb index e238fddb..d9dd4146 100644 --- a/spec/paper_trail_spec.rb +++ b/spec/paper_trail_spec.rb @@ -5,7 +5,7 @@ describe "PaperTrail RSpec Helper" do 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 @@ -27,6 +27,19 @@ describe "PaperTrail RSpec Helper" do 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 + describe :whodunnit do before(:all) { ::PaperTrail.whodunnit = 'foobar' }