From e48fcc1161ae61db4c541d6152772f0edd9a22ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorsten=20B=C3=B6ttger?= Date: Sat, 6 Dec 2014 11:15:12 +0100 Subject: [PATCH] bugfix: initialize the aasm state column after initialization of the ActiveRecord instance only if the attribute has been loaded #193 --- CHANGELOG.md | 4 ++++ aasm.gemspec | 2 +- .../persistence/active_record_persistence.rb | 4 +++- .../active_record_persistence_spec.rb | 17 ++++++++++++++--- 4 files changed, 22 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8d6f2c3..e7674e8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,10 @@ * `aasm_column` has been removed. Use `aasm.attribute_name` instead * `aasm_human_event_name` has been removed. Use `aasm.human_event_name` instead +## 4.0.5 + + * bugfix: initialize the aasm state column after initialization of the _ActiveRecord_ instance only if the attribute has been loaded (see [issue #193](https://github.com/aasm/aasm/issues/193) for details) + ## 4.0.4 * corrected callback order in README diff --git a/aasm.gemspec b/aasm.gemspec index 31bacf9..ddef72a 100644 --- a/aasm.gemspec +++ b/aasm.gemspec @@ -19,7 +19,7 @@ Gem::Specification.new do |s| # debugging # s.add_development_dependency 'debugger' - # s.add_development_dependency 'pry' + s.add_development_dependency 'pry' # test coverage # s.add_development_dependency 'mime-types', '~> 1.25' # needed by coveralls (>= 2.0 needs Ruby >=1.9.2) diff --git a/lib/aasm/persistence/active_record_persistence.rb b/lib/aasm/persistence/active_record_persistence.rb index 7bae809..7e3abed 100644 --- a/lib/aasm/persistence/active_record_persistence.rb +++ b/lib/aasm/persistence/active_record_persistence.rb @@ -164,7 +164,9 @@ module AASM # foo.aasm_state # => nil # def aasm_ensure_initial_state - aasm.enter_initial_state if send(self.class.aasm.attribute_name).blank? + if respond_to?(self.class.aasm.attribute_name) && send(self.class.aasm.attribute_name).blank? + aasm.enter_initial_state + end end def aasm_fire_event(name, options, *args, &block) diff --git a/spec/unit/persistence/active_record_persistence_spec.rb b/spec/unit/persistence/active_record_persistence_spec.rb index 1edee03..85746ec 100644 --- a/spec/unit/persistence/active_record_persistence_spec.rb +++ b/spec/unit/persistence/active_record_persistence_spec.rb @@ -252,9 +252,20 @@ describe "instance methods" do expect(gate.aasm.current_state).to be_nil end - it "should initialize the aasm state on instantiation" do - expect(Gate.new.aasm_state).to eql 'opened' - expect(Gate.new.aasm.current_state).to eql :opened + context 'on initialization' do + it "should initialize the aasm state" do + expect(Gate.new.aasm_state).to eql 'opened' + expect(Gate.new.aasm.current_state).to eql :opened + end + + it "should not initialize the aasm state if it has not been loaded" do + # we have to create a gate in the database, for which we only want to + # load the id, and not the state + gate = Gate.create! + + # then we just load the gate ids + Gate.select(:id).where(id: gate.id).first + end end end