Created
June 8, 2017 19:09
-
-
Save duffyjp/4db4c912111350cf894946d1d9ab679a to your computer and use it in GitHub Desktop.
Trying to reproduce an issue with default scopes and paper_trail starting with Rails 5.1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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.1.1" | |
gem 'rspec', '3.6.0' | |
gem "paper_trail", "7.0.3", require: false | |
gem "sqlite3" | |
end | |
require "active_record" | |
require "logger" | |
# Please use sqlite for your bug reports, if possible. | |
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:") | |
ActiveRecord::Base.logger = nil | |
ActiveRecord::Schema.define do | |
# STEP TWO: Define your tables here. | |
create_table :mailing_lists, force: true do |t| | |
t.string :name | |
t.boolean :active, default: true | |
t.timestamps null: false | |
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, %i[item_type item_id] | |
add_index :versions, [:transaction_id] | |
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 = false | |
require "paper_trail" | |
# STEP FOUR: Define your AR models here. | |
class MailingList < ActiveRecord::Base | |
has_paper_trail ignore: :updated_at | |
validates :name, presence: true, uniqueness: true | |
default_scope { where(active: true).order(:name) } | |
end | |
require 'rspec' | |
require 'rspec/autorun' | |
#PaperTrail::Rails::Engine.eager_load! # https://github.com/airblade/paper_trail/issues/392 | |
# STEP FIVE: Please write a test that demonstrates your issue. | |
RSpec.describe "with default_scope" do | |
it "validations are working" do | |
MailingList.create(name: "1") | |
expect{MailingList.create!(name: "1")}.to raise_error(ActiveRecord::RecordInvalid).with_message(/Name has already been taken/) | |
end | |
it "default_scope is working" do | |
expect(MailingList.all.to_sql).to eq "SELECT \"mailing_lists\".* FROM \"mailing_lists\" WHERE \"mailing_lists\".\"active\" = 't' ORDER BY \"mailing_lists\".\"name\" ASC" | |
end | |
it "paper_trail works on attributes not in the default_scope" do | |
mailing_list = MailingList.create(name: "Foo") | |
expect(mailing_list.reload.versions.count).to eq 1 | |
mailing_list.update(name: "Bar") | |
expect(mailing_list.reload.versions.count).to eq 2 | |
end | |
it "issue changing attribute within the default_scope" do | |
mailing_list = MailingList.create(name: "Nope") | |
expect(mailing_list.reload.versions.count).to eq 1 | |
mailing_list.update(active: false) | |
expect(mailing_list.reload.versions.count).to eq 2 | |
end | |
end | |
# STEP SIX: Run this script using `ruby my_bug_report.rb` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment