From 13c8e96fe7cc3ecfc0ba471abe60bc13495e56b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorsten=20B=C3=B6ttger?= Date: Fri, 5 Dec 2014 00:07:38 +0100 Subject: [PATCH] bugfix: initialize the aasm state column after initialization of the _ActiveRecord_ instance #191 --- CHANGELOG.md | 3 ++- .../persistence/active_record_persistence.rb | 13 ++++-------- lib/aasm/persistence/mongoid_persistence.rb | 4 ++-- spec/database.rb | 6 +++++- spec/models/persistence.rb | 14 ++++++++++++- .../active_record_persistence_spec.rb | 21 +++---------------- 6 files changed, 29 insertions(+), 32 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 589dfea0..75f9b2a0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,8 @@ ## 4.0.4 (not yet released) - * bugfix: avoid Rails autoloading conflicts (see [issue #137](https://github.com/aasm/aasm/issues/137) and see [issue #139](https://github.com/aasm/aasm/issues/139) for details) + * bugfix: initialize the aasm state column after initialization of the _ActiveRecord_ instance (see [issue #191](https://github.com/aasm/aasm/issues/191) for details) + * bugfix: avoid Rails autoloading conflicts (see [issue #137](https://github.com/aasm/aasm/issues/137) and [issue #139](https://github.com/aasm/aasm/issues/139) for details) ## 4.0.3 diff --git a/lib/aasm/persistence/active_record_persistence.rb b/lib/aasm/persistence/active_record_persistence.rb index b0721ad8..7bae8090 100644 --- a/lib/aasm/persistence/active_record_persistence.rb +++ b/lib/aasm/persistence/active_record_persistence.rb @@ -33,15 +33,10 @@ def self.included(base) base.extend AASM::Persistence::ActiveRecordPersistence::ClassMethods base.send(:include, AASM::Persistence::ActiveRecordPersistence::InstanceMethods) - if ActiveRecord::VERSION::MAJOR >= 3 - base.before_validation(:aasm_ensure_initial_state, :on => :create) - else - base.before_validation_on_create(:aasm_ensure_initial_state) + base.after_initialize do + aasm_ensure_initial_state end - # ensure initial aasm state even when validations are skipped - base.before_create(:aasm_ensure_initial_state) - # ensure state is in the list of states base.validate :aasm_validate_states end @@ -197,5 +192,5 @@ def aasm_validate_states end # InstanceMethods end - end -end + end # Persistence +end # AASM diff --git a/lib/aasm/persistence/mongoid_persistence.rb b/lib/aasm/persistence/mongoid_persistence.rb index 23430b6b..bc20c68c 100644 --- a/lib/aasm/persistence/mongoid_persistence.rb +++ b/lib/aasm/persistence/mongoid_persistence.rb @@ -133,5 +133,5 @@ def aasm_state_with_named_scope name, options = {} end end end - end -end + end # Persistence +end # AASM diff --git a/spec/database.rb b/spec/database.rb index 02fccb9b..06660c00 100644 --- a/spec/database.rb +++ b/spec/database.rb @@ -1,10 +1,14 @@ ActiveRecord::Migration.suppress_messages do - %w{gates readers writers transients simples simple_new_dsls no_scopes no_direct_assignments thieves localizer_test_models persisted_states provided_and_persisted_states with_enums with_true_enums with_false_enums}.each do |table_name| + %w{gates readers writers transients simples no_scopes no_direct_assignments thieves localizer_test_models persisted_states provided_and_persisted_states with_enums with_true_enums with_false_enums}.each do |table_name| ActiveRecord::Migration.create_table table_name, :force => true do |t| t.string "aasm_state" end end + ActiveRecord::Migration.create_table "simple_new_dsls", :force => true do |t| + t.string "status" + end + ActiveRecord::Migration.create_table "validators", :force => true do |t| t.string "name" t.string "status" diff --git a/spec/models/persistence.rb b/spec/models/persistence.rb index a0450e69..d1c6db62 100644 --- a/spec/models/persistence.rb +++ b/spec/models/persistence.rb @@ -2,7 +2,11 @@ class Gate < ActiveRecord::Base include AASM # Fake this column for testing purposes - attr_accessor :aasm_state + # attr_accessor :aasm_state + + def value + 'value' + end aasm do state :opened @@ -20,6 +24,10 @@ class WithEnum < ActiveRecord::Base # Fake this column for testing purposes attr_accessor :aasm_state + def self.test + {} + end + aasm :enum => :test do state :opened state :closed @@ -36,6 +44,10 @@ class WithTrueEnum < ActiveRecord::Base # Fake this column for testing purposes attr_accessor :aasm_state + def value + 'value' + end + aasm :enum => true do state :opened state :closed diff --git a/spec/unit/persistence/active_record_persistence_spec.rb b/spec/unit/persistence/active_record_persistence_spec.rb index 791de8d6..1edee032 100644 --- a/spec/unit/persistence/active_record_persistence_spec.rb +++ b/spec/unit/persistence/active_record_persistence_spec.rb @@ -76,12 +76,10 @@ let(:with_true_enum) { WithTrueEnum.new } before :each do WithTrueEnum.aasm.stub(:attribute_name).and_return(:value) - with_true_enum.stub(:aasm_guess_enum_method).and_return(:values) end it "infers enum method name from pluralized column name" do expect(with_true_enum.send(:aasm_enum)).to eq :values - expect(with_true_enum).to have_received :aasm_guess_enum_method end end @@ -101,12 +99,10 @@ context "when AASM column looks like enum" do before :each do gate.stub(:aasm_column_looks_like_enum).and_return(true) - gate.stub(:aasm_guess_enum_method).and_return(:values) end it "infers enum method name from pluralized column name" do expect(gate.send(:aasm_enum)).to eq :values - expect(gate).to have_received :aasm_guess_enum_method end end @@ -256,20 +252,9 @@ expect(gate.aasm.current_state).to be_nil end - it "should call aasm_ensure_initial_state on validation before create" do - expect(gate).to receive(:aasm_ensure_initial_state).and_return(true) - gate.valid? - end - - it "should call aasm_ensure_initial_state before create, even if skipping validations" do - expect(gate).to receive(:aasm_ensure_initial_state).and_return(true) - gate.save(:validate => false) - end - - it "should not call aasm_ensure_initial_state on validation before update" do - allow(gate).to receive(:new_record?).and_return(false) - expect(gate).not_to receive(:aasm_ensure_initial_state) - gate.valid? + 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 end end