From 09e5ae5937bbe8bb82e1f85e8551034848988d77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorsten=20B=C3=B6ttger?= Date: Mon, 16 Dec 2013 23:42:03 +0100 Subject: [PATCH] initialize the state even if validation is skipped (for Mongoid) --- lib/aasm/persistence/mongoid_persistence.rb | 2 ++ .../unit/persistence/mongoid_persistance_spec.rb | 16 ++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/lib/aasm/persistence/mongoid_persistence.rb b/lib/aasm/persistence/mongoid_persistence.rb index 44f9864..0cf4663 100644 --- a/lib/aasm/persistence/mongoid_persistence.rb +++ b/lib/aasm/persistence/mongoid_persistence.rb @@ -36,6 +36,8 @@ module AASM # Mongoid's Validatable gem dependency goes not have a before_validation_on_xxx hook yet. # base.before_validation_on_create :aasm_ensure_initial_state base.before_validation :aasm_ensure_initial_state + # ensure initial aasm state even when validations are skipped + base.before_create :aasm_ensure_initial_state end module ClassMethods diff --git a/spec/unit/persistence/mongoid_persistance_spec.rb b/spec/unit/persistence/mongoid_persistance_spec.rb index e91817d..f214667 100644 --- a/spec/unit/persistence/mongoid_persistance_spec.rb +++ b/spec/unit/persistence/mongoid_persistance_spec.rb @@ -130,6 +130,22 @@ describe 'mongoid', :if => Gem::Version.create(RUBY_VERSION.dup) >= Gem::Version end end + + + describe "instance methods" do + let(:simple) {SimpleNewDslMongoid.new} + + it "should call aasm_ensure_initial_state on validation before create" do + simple.should_receive(:aasm_ensure_initial_state).and_return(true) + simple.valid? + end + + it "should call aasm_ensure_initial_state before create, even if skipping validations" do + simple.should_receive(:aasm_ensure_initial_state).and_return(true) + simple.save(:validate => false) + end + end + rescue LoadError puts "Not running Mongoid specs because mongoid gem if not installed!!!" end