Permalink
Browse files

specs for version; with_versioning helper for rspec

Conflicts:
	paper_trail.gemspec
  • Loading branch information...
1 parent 5f53b1e commit 4eaaac5dd8ec78c516b3fe3912098a405c26ab70 Paul Belt committed with batter Apr 10, 2013
View
3 .rspec
@@ -0,0 +1,3 @@
+--format progress
+--color
+
View
@@ -6,6 +6,8 @@
Dir[File.join(File.dirname(__FILE__), 'paper_trail', 'frameworks', '*.rb')].each { |file| require file }
Dir[File.join(File.dirname(__FILE__), 'paper_trail', 'serializers', '*.rb')].each { |file| require file }
+require 'paper_trail/rspec' if defined?(RSpec)
+
# PaperTrail's module methods can be called in both models and controllers.
module PaperTrail
extend PaperTrail::Cleaner
View
@@ -0,0 +1,29 @@
+require 'paper_trail/rspec/paper_trail_extensions'
+
+module RSpec
+ module Rails
+ class Railtie < ::Rails::Railtie
+ initializer 'paper_trail.rspec_extensions' do
+ RSpec.configure do |config|
+ config.include RSpec::PaperTrailExtensions
+
+ config.before(:each) do
+ ::PaperTrail.enabled = false
+ ::PaperTrail.controller_info = {}
+ ::PaperTrail.whodunnit = nil
+ end
+
+ config.before(:each, versioning: true) do
+ ::PaperTrail.enabled = true
+ end
+ end
+
+ RSpec::Matchers.define :be_versioned do
+ match do |actual|
+ actual.respond_to?(:versions)
+ end
+ end
+ end
+ end
+ end
+end
@@ -0,0 +1,21 @@
+require 'rspec/core'
+require 'rspec/expectations'
+
+module RSpec
+ module PaperTrailExtensions
+ # :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
View
@@ -23,6 +23,7 @@ Gem::Specification.new do |s|
s.add_development_dependency 'railties', ['>= 3.0', '< 5.0']
s.add_development_dependency 'sinatra', '~> 1.0'
s.add_development_dependency 'rack-test', '>= 0.6'
+ s.add_development_dependency 'rspec-rails', '~> 2.14'
# JRuby support for the test ENV
unless defined?(JRUBY_VERSION)
@@ -0,0 +1,37 @@
+require 'spec_helper'
+
+describe Version do
+ context 'default' do
+ it 'should have versioning off by default' do
+ ::PaperTrail.enabled?.should_not be_true
+ end
+ it 'should turn versioning on in a with_versioning block' do
+ ::PaperTrail.enabled?.should_not be_true
+ with_versioning do
+ ::PaperTrail.enabled?.should be_true
+ end
+ ::PaperTrail.enabled?.should_not be_true
+ end
+ end
+ context 'versioning: true', versioning: true do
+ it 'should have versioning on by default' do
+ ::PaperTrail.enabled?.should be_true
+ end
+ it 'should keep versioning on after a with_versioning block' do
+ ::PaperTrail.enabled?.should be_true
+ with_versioning do
+ ::PaperTrail.enabled?.should be_true
+ end
+ ::PaperTrail.enabled?.should be_true
+ end
+ end
+end
+
+describe Model do
+ context 'be_versioned matcher', versioning: true do
+ it 'should respond to be_versioned' do
+ model = Model.create name: 'Bob', color: 'blue'
+ model.should be_versioned
+ end
+ end
+end
View
@@ -0,0 +1,5 @@
++--colour
++--format progress
++--loadby mtime
++--reverse
+
View
@@ -0,0 +1,38 @@
+require 'rubygems'
+require 'bundler/setup'
+
+ENV['RAILS_ENV'] ||= 'test'
+require File.expand_path('../dummy/config/environment', __FILE__)
+require 'rspec/rails'
+require 'rspec/autorun'
+
+require 'rails'
+require 'active_record'
+
+Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
+
+RSpec.configure do |config|
+ config.order = 'random'
+end
+
+ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
+
+ActiveRecord::Schema.define(:version => 1) do
+ create_table :models do |t|
+ t.string :name
+ t.string :color
+ end
+ create_table :versions do |t|
+ t.string :item_type, null: false
+ t.string :item_id, null: false
+ t.string :event, null: false
+ t.string :whodunnit
+ t.text :object
+ t.datetime :created_at
+ end
+end
+
+class Model < ActiveRecord::Base
+ has_paper_trail
+ attr_accessible :name, :color
+end
View
No changes.

0 comments on commit 4eaaac5

Please sign in to comment.