Skip to content

Commit

Permalink
Merge pull request #60 from jherdman/auto-define-state-constants
Browse files Browse the repository at this point in the history
AASM defines constants for each state name
  • Loading branch information
alto committed Mar 20, 2013
2 parents 696aaae + 1b422a5 commit 636a25f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,9 @@
# CHANGELOG

## Unreleased

* added autocreation of constants for each state ([@jherdman](https://github.com/jherdman))

## 3.0.16

* added autocreation of state scopes for Mongoid (thanks to [@jonnyshields](https://github.com/johnnyshields))
Expand Down
4 changes: 4 additions & 0 deletions lib/aasm/base.rb
Expand Up @@ -32,6 +32,10 @@ def state(name, options={})
@clazz.send(:define_method, "#{name.to_s}?") do
aasm_current_state == name
end

unless @clazz.const_defined?("STATE_#{name.to_s.upcase}")
@clazz.const_set("STATE_#{name.to_s.upcase}", name)
end
end

# define an event
Expand Down
6 changes: 6 additions & 0 deletions spec/unit/simple_example_spec.rb
Expand Up @@ -50,4 +50,10 @@ class Payment
lambda {payment.fill_out}.should raise_error(AASM::InvalidTransition)
lambda {payment.fill_out!}.should raise_error(AASM::InvalidTransition)
end

it 'defines constants for each state name' do
Payment::STATE_INITIALISED.should eq(:initialised)
Payment::STATE_FILLED_OUT.should eq(:filled_out)
Payment::STATE_AUTHORISED.should eq(:authorised)
end
end

0 comments on commit 636a25f

Please sign in to comment.