Created
April 23, 2023 09:06
-
-
Save briu/c224b8e094398262614c96e0347943e5 to your computer and use it in GitHub Desktop.
This file contains 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
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 | |
source "https://rubygems.org" | |
gem "rails", "7.0.4.2" | |
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 :users do |t| | |
t.integer :published_posts_count, default: 0 | |
t.timestamps | |
end | |
create_table :posts do |t| | |
t.integer :user_id | |
t.integer :poll_id | |
t.string :title | |
t.string :release_stage, default: 'draft' | |
t.datetime :release_at | |
t.timestamps | |
end | |
create_table :polls do |t| | |
t.boolean :multiple | |
t.timestamps | |
end | |
create_table :poll_options do |t| | |
t.integer :poll_id | |
t.string :title | |
t.timestamps | |
end | |
end | |
class User < ActiveRecord::Base | |
has_many :posts | |
end | |
class Post < ActiveRecord::Base | |
belongs_to :user | |
belongs_to :poll | |
accepts_nested_attributes_for :poll | |
after_commit :test_after_commit | |
# after_save :test_after_commit works as expected | |
def test_after_commit | |
puts "PREVIOUS_CHANGES: #{previous_changes}" | |
puts "SAVED CHANGEs #{saved_changes}" | |
if saved_change_to_release_stage?(to: 'published') | |
user.increment(:published_posts_count) | |
end | |
end | |
end | |
class Poll < ActiveRecord::Base | |
has_one :post | |
has_many :poll_options | |
accepts_nested_attributes_for :poll_options | |
end | |
class PollOption < ActiveRecord::Base | |
belongs_to :poll | |
end | |
class BugTest < Minitest::Test | |
def setup | |
Post.destroy_all | |
Poll.destroy_all | |
PollOption.destroy_all | |
end | |
def test_creating_post_with_poll_and_poll_options | |
user = User.create | |
post = Post.new( | |
user: user, | |
title: 'yoyo', | |
release_stage: 'published', | |
poll_attributes: { poll_options_attributes: [{ title: 'good' }, { title: 'not good' }] } | |
) | |
post.save | |
assert_equal user.published_posts_count, 1 | |
end | |
def test_creating_post_with_poll_only | |
user = User.create | |
post = Post.new(user: user, title: 'yoyo', release_stage: 'published') | |
post.save | |
assert_equal user.published_posts_count, 1 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment