From 8f4b09698541a26ef9af89c09df69163dc1811a4 Mon Sep 17 00:00:00 2001 From: lingceng Date: Thu, 18 Feb 2016 15:01:52 +0800 Subject: [PATCH] Fix #273 work right after dup See: http://www.jonathanleighton.com/articles/2011/initialize_clone-initialize_dup-and-initialize_copy-in-ruby/ --- lib/aasm/aasm.rb | 5 +++++ spec/spec_helper.rb | 1 + spec/unit/api_spec.rb | 20 ++++++++++++++++++++ 3 files changed, 26 insertions(+) diff --git a/lib/aasm/aasm.rb b/lib/aasm/aasm.rb index a1b2521..2d19ece 100644 --- a/lib/aasm/aasm.rb +++ b/lib/aasm/aasm.rb @@ -67,6 +67,11 @@ module AASM @aasm[name.to_sym] ||= AASM::InstanceBase.new(self, name.to_sym) end + def initialize_dup(other) + @aasm = {} + super + end + private # Takes args and a from state and removes the first diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 5c3c3e2..a9aadf7 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -3,6 +3,7 @@ $LOAD_PATH.unshift(File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib require 'aasm' require 'rspec' require 'aasm/rspec' +require 'pry' # require 'ruby-debug'; Debugger.settings[:autoeval] = true; debugger; rubys_debugger = 'annoying' # require 'ruby-debug/completion' diff --git a/spec/unit/api_spec.rb b/spec/unit/api_spec.rb index 56cadd6..e16850d 100644 --- a/spec/unit/api_spec.rb +++ b/spec/unit/api_spec.rb @@ -22,6 +22,26 @@ describe "reading the current state" do it "uses the provided method even if persisted" do expect(ProvidedAndPersistedState.new.aasm.current_state).to eql :gamma end + + context "after dup" do + it "uses the persistence storage" do + source = PersistedState.create! + copy = source.dup + copy.save! + + copy.release! + + expect(source.aasm_state).to eql 'alpha' + expect(source.aasm.current_state).to eql :alpha + + source2 = PersistedState.find(source.id) + expect(source2.reload.aasm_state).to eql 'alpha' + expect(source2.aasm.current_state).to eql :alpha + + expect(copy.aasm_state).to eql 'beta' + expect(copy.aasm.current_state).to eql :beta + end + end end describe "writing and persisting the current state" do