Skip to content

Instantly share code, notes, and snippets.

@joelmoss
Created June 22, 2023 10:25
Show Gist options
  • Save joelmoss/5522b429f53db7fd3d3e416c399c24d8 to your computer and use it in GitHub Desktop.
Save joelmoss/5522b429f53db7fd3d3e416c399c24d8 to your computer and use it in GitHub Desktop.

Revisions

  1. joelmoss created this gist Jun 22, 2023.
    61 changes: 61 additions & 0 deletions bug.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,61 @@
    # 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', '~> 7.0.5'
    gem 'debug'
    gem 'sqlite3'
    end

    require 'active_record'
    require 'active_support/testing/assertions'
    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 :appointments, force: true do |t|
    t.boolean :paid, default: false
    end

    create_table :payments, force: true do |t|
    t.references :appointment
    end
    end

    class Appointment < ActiveRecord::Base
    has_one :payment, dependent: :destroy

    after_create do
    logger.debug :ensure_payment
    create_payment
    end
    end

    class Payment < ActiveRecord::Base
    belongs_to :appointment

    before_create do
    logger.debug :create_intent
    appointment.assign_attributes paid: true
    appointment.save validate: false
    end
    end

    class BugTest < Minitest::Test
    include ActiveSupport::Testing::Assertions

    def test_bug
    assert_difference 'Payment.count', 1 do
    Appointment.create!
    end
    end
    end