Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save santir489/f5cb14970c68602eb621593946064f1d to your computer and use it in GitHub Desktop.
Save santir489/f5cb14970c68602eb621593946064f1d to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
gem "rails"
# If you want to test against edge Rails replace the previous line with this:
# gem "rails", github: "rails/rails", branch: "main"
gem "paper_trail"
gem "paper_trail-association_tracking"
gem "sqlite3"
end
require "active_record"
require "minitest/autorun"
require "logger"
# 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, force: true do |t|
end
create_table :comments, force: true do |t|
t.integer :post_id
t.string :post_type
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
t.text :object_changes
t.integer :transaction_id
t.datetime :created_at, limit: 6
# Metadata columns.
t.integer :answer
t.string :action
t.string :question
t.integer :article_id
t.string :title
# Controller info columns.
t.string :ip
t.string :user_agent
end
add_index :versions, %i[item_type item_id]
create_table :version_associations do |t|
t.integer :version_id
t.string :foreign_key_name, null: false
t.integer :foreign_key_id
t.string :foreign_type
end
add_index :version_associations, [:version_id]
add_index :version_associations,
%i[foreign_key_name foreign_key_id foreign_type],
name: "index_version_associations_on_foreign_key"
end
PaperTrail.config.track_associations = true
class Post < ActiveRecord::Base
has_many :comments
end
class Comment < ActiveRecord::Base
belongs_to :post, polymorphic: true, optional: true
has_paper_trail
end
class BugTest < Minitest::Test
def test_association_stuff
comment_empty_string = Comment.new(post_type: '')
assert_raises(NameError){ comment_empty_string.save! }
comment_with_nil = Comment.new(post_type: nil)
assert_equal true, comment_with_nil.save!
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment