Last active
September 23, 2020 11:18
-
-
Save mtsmfm/3f4c6ca4e63d03f42460ceec41a14768 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
require 'bundler/inline' | |
gemfile do | |
source 'https://rubygems.org' | |
gem 'mongoid' | |
end | |
require 'yaml' | |
require 'tempfile' | |
require 'logger' | |
uri = 'mongodb://mongo/test' | |
Tempfile.open(%w(config .yml)) do |f| | |
f.write(<<~YAML) | |
development: | |
clients: | |
default: | |
uri: #{uri} | |
YAML | |
f.flush | |
Mongoid.load!(f.path, 'development') | |
Mongoid::Clients.clients.each do |_, client| | |
client.logger.level = Logger::ERROR | |
end | |
end | |
class Foo | |
include Mongoid::Document | |
embeds_one :bar, cascade_callbacks: true | |
end | |
class Bar | |
include Mongoid::Document | |
embedded_in :foo | |
after_save :test_after_save | |
def test_after_save | |
puts "after_save is called" if $output_after_save_log | |
end | |
end | |
Foo.delete_all | |
foo1 = Foo.new(bar: Bar.new) | |
foo1.save | |
foo2 = Foo.new(bar: Bar.new) | |
foo2.save | |
$output_after_save_log = true | |
foo1.bar = nil | |
foo1.save | |
# => <output nothing> | |
foo2.update_attributes!(bar: nil) | |
# => after_save is called |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment