Skip to content

Commit

Permalink
Do not update unloaded state for Sequel:
Browse files Browse the repository at this point in the history
The problem is that the record might already have a state, which was
just not loaded due to explicit `select'. In this case, AASM should not
try to write the initial state into database.
  • Loading branch information
godfat committed Mar 3, 2015
1 parent ec000cf commit 109aa02
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/aasm/persistence/sequel_persistence.rb
Expand Up @@ -72,6 +72,7 @@ def aasm_read_state
#
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

Expand Down
13 changes: 13 additions & 0 deletions spec/unit/persistence/sequel_persistence_spec.rb
Expand Up @@ -44,6 +44,11 @@
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)
Expand All @@ -61,6 +66,14 @@
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?
Expand Down

0 comments on commit 109aa02

Please sign in to comment.