diff --git a/lib/aasm/persistence/sequel_persistence.rb b/lib/aasm/persistence/sequel_persistence.rb index 69c78e8..aa64752 100644 --- a/lib/aasm/persistence/sequel_persistence.rb +++ b/lib/aasm/persistence/sequel_persistence.rb @@ -72,6 +72,7 @@ module AASM # def aasm_ensure_initial_state aasm.enter_initial_state if + (new? || values.key?(self.class.aasm.attribute_name)) && send(self.class.aasm.attribute_name).to_s.strip.empty? end diff --git a/spec/unit/persistence/sequel_persistence_spec.rb b/spec/unit/persistence/sequel_persistence_spec.rb index 7b7285c..8c4f01b 100644 --- a/spec/unit/persistence/sequel_persistence_spec.rb +++ b/spec/unit/persistence/sequel_persistence_spec.rb @@ -44,6 +44,11 @@ describe 'sequel' do expect(model.aasm.current_state).to eq(:alpha) end + it "should save the initial state" do + model.save + expect(model.status).to eq("alpha") + end + it "should return the aasm column when new and the aasm field is not nil" do model.status = "beta" expect(model.aasm.current_state).to eq(:beta) @@ -61,6 +66,14 @@ describe 'sequel' do expect(model.aasm.current_state).to be_nil end + it "should not change the state if state is not loaded" do + model.release + model.save + model.class.select(:id).first.save + model.reload + expect(model.aasm.current_state).to eq(:beta) + end + it "should call aasm_ensure_initial_state on validation before create" do expect(model).to receive(:aasm_ensure_initial_state).and_return(true) model.valid?