Created
June 22, 2023 10:25
-
-
Save joelmoss/5522b429f53db7fd3d3e416c399c24d8 to your computer and use it in GitHub Desktop.
Ruby inline standalone script
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
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment