Manually require framework helpers individually and refactor the RSpec and Cucumber helpers to be conditionally required. close #272, close #278

This commit is contained in:
Ben Atkins 2013-10-02 13:48:20 -04:00
parent 379506ada3
commit 39decb3de4
5 changed files with 88 additions and 48 deletions

View File

@ -1,5 +1,7 @@
## 3.0.0 (Unreleased)
- [#278](https://github.com/airblade/paper_trail/issues/278)/[#272](https://github.com/airblade/paper_trail/issues/272) -
Make RSpec and Cucumber helpers usable with [Spork](https://github.com/sporkrb/spork) and [Zeus](https://github.com/burke/zeus).
- [#273](https://github.com/airblade/paper_trail/pull/273) - Make the `only` and `ignore` options accept `Hash` arguments;
allows for conditional tracking.
- [#264](https://github.com/airblade/paper_trail/pull/264) - Allow unwrapped symbol to be passed in to the `on` option.

View File

@ -891,9 +891,9 @@ You may want to turn PaperTrail off to speed up your tests. See the [Turning Pa
### RSpec
PaperTrail provides a helper that works with RSpec to make it easier to control when `PaperTrail` during testing. By default, PaperTrail will be
turned off for all tests. When you wish to enable PaperTrail for a test you can either wrap the test in a `with_versioning` block, or pass
in `:versioning => true` option to a spec block, like so:
PaperTrail provides a helper that works with [RSpec](https://github.com/rspec/rspec) to make it easier to control when `PaperTrail` is enabled
during testing. By default, PaperTrail will be turned off for all tests.
When you wish to enable PaperTrail for a test you can either wrap the test in a `with_versioning` block, or pass in `:versioning => true` option to a spec block, like so:
```ruby
describe "RSpec test group" do
@ -939,7 +939,7 @@ end
### Cucumber
PaperTrail provides a helper that works similar to the RSpec helper.
PaperTrail provides a helper for [Cucumber](http://cukes.info) that works similar to the RSpec helper.
By default, PaperTrail will be turned off for all scenarios by a `before` hook added by the helper.
When you wish to enable PaperTrail for a scenario, you can wrap code in a `with_versioning` block in a step, like so:
@ -954,6 +954,43 @@ end
The helper will also reset the `PaperTrail.whodunnit` value to `nil` before each test to help prevent data spillover between tests.
If you are using PaperTrail with Rails, the helper will automatically set the `PaperTrail.controller_info` value to `{}` as well, again, to help prevent data spillover between tests.
### Spork
If you wish to use the `RSpec` or `Cucumber` helpers with [Spork](https://github.com/sporkrb/spork), you will need to
manually require the helper(s) in your `prefork` block on your test helper, like so:
```ruby
# spec/spec_helper.rb
require 'spork'
Spork.prefork do
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'paper_trail/frameworks/rspec'
require 'paper_trail/frameworks/cucumber'
...
end
```
### Zeus
If you wish to use the `RSpec` or `Cucumber` heleprs with [Zeus](https://github.com/burke/zeus), you will need to
manually require the helper(s) in your test helper, like so:
```ruby
# spec/spec_helper.rb
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'paper_trail/frameworks/rspec'
```
## Articles
[Keep a Paper Trail with PaperTrail](http://www.linux-mag.com/id/7528), Linux Magazine, 16th September 2009.

View File

@ -2,8 +2,7 @@ require 'paper_trail/config'
require 'paper_trail/has_paper_trail'
require 'paper_trail/cleaner'
# Require all frameworks and serializers
Dir[File.join(File.dirname(__FILE__), 'paper_trail', 'frameworks', '*.rb')].each { |file| require file }
# Require serializers
Dir[File.join(File.dirname(__FILE__), 'paper_trail', 'serializers', '*.rb')].each { |file| require file }
# PaperTrail's module methods can be called in both models and controllers.
@ -104,6 +103,12 @@ end
require 'paper_trail/version'
# Require all frameworks
require 'paper_trail/frameworks/rails'
require 'paper_trail/frameworks/sinatra'
require 'paper_trail/frameworks/rspec' if defined? RSpec
require 'paper_trail/frameworks/cucumber' if defined? World
ActiveSupport.on_load(:active_record) do
include PaperTrail::Model
end

View File

@ -1,31 +1,29 @@
if defined? World
# before hook for Cucumber
before do
::PaperTrail.enabled = false
::PaperTrail.whodunnit = nil
::PaperTrail.controller_info = {} if defined? ::Rails
end
# before hook for Cucumber
before do
::PaperTrail.enabled = false
::PaperTrail.whodunnit = nil
::PaperTrail.controller_info = {} if defined? ::Rails
end
module PaperTrail
module Cucumber
module Extensions
# :call-seq:
# with_versioning
#
# enable versioning for specific blocks
module PaperTrail
module Cucumber
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
def with_versioning
was_enabled = ::PaperTrail.enabled?
::PaperTrail.enabled = true
begin
yield
ensure
::PaperTrail.enabled = was_enabled
end
end
end
end
World PaperTrail::Cucumber::Extensions
end
World PaperTrail::Cucumber::Extensions

View File

@ -1,24 +1,22 @@
if defined? RSpec
require 'rspec/core'
require 'rspec/matchers'
require File.expand_path('../rspec/extensions', __FILE__)
require 'rspec/core'
require 'rspec/matchers'
require File.expand_path('../rspec/extensions', __FILE__)
RSpec.configure do |config|
config.include ::PaperTrail::RSpec::Extensions
RSpec.configure do |config|
config.include ::PaperTrail::RSpec::Extensions
config.before(:each) do
::PaperTrail.enabled = false
::PaperTrail.whodunnit = nil
::PaperTrail.controller_info = {} if defined?(::Rails) && defined?(::RSpec::Rails)
end
config.before(:each, :versioning => true) do
::PaperTrail.enabled = true
end
config.before(:each) do
::PaperTrail.enabled = false
::PaperTrail.whodunnit = nil
::PaperTrail.controller_info = {} if defined?(::Rails) && defined?(::RSpec::Rails)
end
RSpec::Matchers.define :be_versioned do
# check to see if the model has `has_paper_trail` declared on it
match { |actual| actual.kind_of?(::PaperTrail::Model::InstanceMethods) }
config.before(:each, :versioning => true) do
::PaperTrail.enabled = true
end
end
RSpec::Matchers.define :be_versioned do
# check to see if the model has `has_paper_trail` declared on it
match { |actual| actual.kind_of?(::PaperTrail::Model::InstanceMethods) }
end