Skip to content

Instantly share code, notes, and snippets.

@batter
Created March 12, 2014 18:40
Show Gist options
  • Save batter/9513527 to your computer and use it in GitHub Desktop.
Save batter/9513527 to your computer and use it in GitHub Desktop.
PaperTrail #341
# Activate the gem you are reporting the issue against.
gem 'rails', '3.2.17'
gem 'sqlite3', '1.3.9'
gem 'paper_trail', '3.0.0'
require 'active_record/railtie'
require 'action_controller/railtie'
require 'rails/test_help'
require 'logger'
require 'paper_trail'
# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table :posts do |t|
t.string :name
t.timestamps
end
create_table :version_posts do |t|
t.string :item_type, :null => false
t.integer :item_id, :null => false
t.string :event, :null => false
t.string :whodunnit
t.text :object
t.datetime :created_at
end
add_index :version_posts, [:item_type, :item_id]
end
class TestApp < Rails::Application
routes.draw do
resources :posts do
get :versions, on: :member
end
end
end
PaperTrail::Version.module_eval { self.abstract_class = true }
PaperTrail.enabled = false
def with_versioning
was_enabled = PaperTrail.enabled?
PaperTrail.enabled = true
yield
ensure
PaperTrail.enabled = was_enabled
end
class Post < ActiveRecord::Base
has_paper_trail :on => [:update, :destroy],
:class_name => 'PostVersion',
if: Proc.new {|t| t.changed? && t.created_at < (Time.now - 1.day)}
end
class PostVersion < PaperTrail::Version #paper_trail 3
self.table_name = :version_posts
end
class PostsController < ActionController::Base
include Rails.application.routes.url_helpers
def versions
@versions = Post.find(params[:id]).versions
render nothing: true
end
def update
@post = Post.find(params[:id])
@post.update_attributes params[:post]
head :ok
end
end
# unit test ok
class BugTest < ActiveSupport::TestCase
def test_versioning
post = Post.create! name: 'post name', created_at: 2.days.ago
with_versioning do
post.update_attribute(:name, 'name changed')
assert_equal 1, post.versions.count
end
end
end
require 'minitest/autorun'
require 'rack/test'
# functional test
class PostsControllerTest < ActionController::TestCase
tests PostsController
setup do
@post = Post.create! name: 'setup post', created_at: 2.days.ago
end
test 'versions' do
get :versions, id: @post.to_param
assert_empty assigns(:versions)
end
test 'versions 2' do
with_versioning do
assert PaperTrail.enabled?
put :update, id: @post.to_param, post: {name: 'name changed'}
assert_equal 1, @post.versions.size
get :versions, id: @post.to_param
assert_response :success
assert_equal 1, assigns(:versions).size
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment