Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# Use this template to report PaperTrail bugs.
# It is based on the ActiveRecord template.
# https://github.com/rails/rails/blob/master/guides/bug_report_templates/active_record_gem.rb
begin
require "bundler/inline"
rescue LoadError => e
$stderr.puts "Bundler version 1.10 or later is required. Please update your Bundler"
raise e
end
gemfile(true) do
ruby '2.3.0'
source 'https://rubygems.org'
gem 'activerecord', '4.2.0'
gem 'minitest', '5.8.3'
gem 'paper_trail', '4.1.0', require: false
gem 'sqlite3'
gem 'pg'
end
require 'active_record'
require 'minitest/autorun'
require 'logger'
ActiveRecord::Base.establish_connection(
adapter: 'postgresql',
database: 'pt_issue_685'
)
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
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'
create_table :supply_chain_requirement_versions do |t|
t.string :item_type, null: false
t.integer :item_id, null: false
t.string :event, null: false
t.string :whodunnit
t.json :object
t.datetime :created_at
t.integer :transaction_id
end
create_table :supply_chain_versions, force: true do |t|
t.string :item_type, null: false
t.integer :item_id, null: false
t.string :event, null: false
t.string :whodunnit
t.json :object
t.datetime :created_at
t.integer :transaction_id
end
end
require 'paper_trail'
class SupplyChainVersion < PaperTrail::Version
self.table_name = :supply_chain_versions
end
class SupplyChainRequirementVersion < PaperTrail::Version
self.table_name = :supply_chain_requirement_versions
end
class SupplyChainRequirement < ActiveRecord::Base
has_paper_trail :class_name => 'SupplyChainRequirementVersion'
end
class SupplyChain < ActiveRecord::Base
has_paper_trail :class_name => 'SupplyChainVersion', :only => [:compliant_status,:compliant_description]
has_many :supply_chain_requirements, inverse_of: :supply_chain
end
class BugTest < ActiveSupport::TestCase
def test_2
s = SupplyChain.create(compliance_status: "warning")
s1 = SupplyChain.find(1)
s1.update_attribute(compliance_status:"non_compliant")
s << SupplyChainRequirement.create(buyer_demanding_document_id:1)
versions = SupplyChain.find(1).versions
assert_equal(1, versions.length) # it will pass the test
s << SupplyChainRequirement.create(buyer_demanding_document_id:2)
s1.update_attribute(compliance_status:"compliant")
requirements = SupplyChain.find(1).versions.find(2).reify(has_many: true).supply_chain_requirements
assert_equal(1, requirements.length) # but returning length 2
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment