Skip to content

Instantly share code, notes, and snippets.

@cannikin
Last active October 6, 2016 19:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cannikin/1b951a6689d21cfe7c335e4d08adee8f to your computer and use it in GitHub Desktop.
Save cannikin/1b951a6689d21cfe7c335e4d08adee8f to your computer and use it in GitHub Desktop.
PaperTrail accepts_nested_attributes_for bug
# Use this template to report PaperTrail bugs.
# Please include only the minimum code necessary to reproduce your issue.
require "bundler/inline"
# STEP ONE: What versions are you using?
gemfile(true) do
ruby "2.3.1"
source "https://rubygems.org"
gem "activerecord", "5.0.0"
gem "minitest", "5.9.0"
gem "paper_trail", "5.2.0", require: false
gem "sqlite3"
end
require "active_record"
require "minitest/autorun"
require "logger"
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
ActiveRecord::Base.logger = nil
ActiveRecord::Schema.define do
# STEP TWO: Define your tables here.
create_table :users, force: true do |t|
t.timestamps null: false
end
create_table :posts do |t|
t.references :user
t.string :title
t.timestamps
end
create_table :versions 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, limit: 1_073_741_823
t.text :object_changes, limit: 1_073_741_823
t.integer :transaction_id
t.datetime :created_at
end
add_index :versions, [:item_type, :item_id]
add_index :versions, [:transaction_id]
create_table :version_associations do |t|
t.integer :version_id
t.string :foreign_key_name, null: false
t.integer :foreign_key_id
end
add_index :version_associations, [:version_id]
add_index :version_associations, [:foreign_key_name, :foreign_key_id],
name: "index_version_associations_on_foreign_key"
end
ActiveRecord::Base.logger = Logger.new(STDOUT)
require "paper_trail/config"
# STEP THREE: Configure PaperTrail as you would in your initializer
PaperTrail::Config.instance.track_associations = true
require "paper_trail"
# STEP FOUR: Define your AR models here.
class User < ActiveRecord::Base
has_many :posts
accepts_nested_attributes_for :posts
# has_paper_trail
end
class Post < ActiveRecord::Base
belongs_to :user, :touch => true
end
# STEP FIVE: Please write a test that demonstrates your issue.
class BugTest < ActiveSupport::TestCase
# Whether you enable PaperTrail or not doesn't fix the issue, you need to
# completely comment out `has_paper_trail` in the User model in order to get
# these to pass (line 67)!
def test_disabling_paper_trail_causes_parent_to_be_touched
PaperTrail.enabled = false
update_user
end
def test_enabling_paper_trail_causes_parent_not_to_be_touched
PaperTrail.enabled = true
update_user
end
def update_user
# reset users
User.destroy_all
# set updated_at to an hour ago
travel_to 1.hour.ago do
User.create
end
original_updated_at = User.first.updated_at
User.first.update! :posts_attributes => { '0' => { :title => 'Post title' } }
# User.first should be touched when the post is updated
assert_not_equal original_updated_at.to_i, User.first.updated_at.to_i
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment