Skip to content

Commit

Permalink
bugfix: initialize the aasm state column after initialization of the …
Browse files Browse the repository at this point in the history
…ActiveRecord instance only if the attribute has been loaded #193
  • Loading branch information
alto committed Dec 6, 2014
1 parent adbe338 commit e48fcc1
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion aasm.gemspec
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion lib/aasm/persistence/active_record_persistence.rb
Expand Up @@ -164,7 +164,9 @@ def aasm_raw_attribute_value(state)
# 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)
Expand Down
17 changes: 14 additions & 3 deletions spec/unit/persistence/active_record_persistence_spec.rb
Expand Up @@ -252,9 +252,20 @@
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
Expand Down

0 comments on commit e48fcc1

Please sign in to comment.