Skip to content

Commit

Permalink
Manually require framework helpers individually and refactor the RSpe…
Browse files Browse the repository at this point in the history
…c and Cucumber helpers to be conditionally required. close #272, close #278
  • Loading branch information
Ben Atkins committed Oct 2, 2013
1 parent 379506a commit 39decb3
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 48 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
45 changes: 41 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:

Expand All @@ -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.
Expand Down
9 changes: 7 additions & 2 deletions lib/paper_trail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
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.
Expand Down Expand Up @@ -104,6 +103,12 @@ def self.configure

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
Expand Down
46 changes: 22 additions & 24 deletions lib/paper_trail/frameworks/cucumber.rb
Original file line number Diff line number Diff line change
@@ -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
34 changes: 16 additions & 18 deletions lib/paper_trail/frameworks/rspec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 39decb3

Please sign in to comment.