Skip to content

Commit

Permalink
version bump to 3.0.0 (new DSL)
Browse files Browse the repository at this point in the history
  • Loading branch information
alto committed Nov 26, 2011
1 parent 1927590 commit 8f0ff1b
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 134 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# CHANGELOG

## 3.0.0

* switched documentation to the new DSL
* whiny transactions: by default, raise an exception if an event transition is not possible
* you may disable whiny transactions

## 2.4.0

* supporting new DSL (which is much shorter)
Expand Down
66 changes: 32 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,26 +59,24 @@ gem 'aasm'
Here's a quick example highlighting some of the features.

```ruby
class Conversation
include AASM

aasm_column :current_state # defaults to aasm_state

aasm_initial_state :unread
class Conversation
include AASM

aasm_state :unread
aasm_state :read
aasm_state :closed
aasm :column => :current_state do # defaults to aasm_state
state :unread, :initial => true
state :read
state :closed


aasm_event :view do
event :view do
transitions :to => :read, :from => [:unread]
end

aasm_event :close do
event :close do
transitions :to => :closed, :from => [:read, :unread]
end
end

end
```

## A Slightly More Complex Example ##
Expand All @@ -89,22 +87,21 @@ This example uses a few of the more complex features available.
class Relationship
include AASM

aasm_column :status

aasm_initial_state Proc.new { |relationship| relationship.strictly_for_fun? ? :intimate : :dating }

aasm_state :dating, :enter => :make_happy, :exit => :make_depressed
aasm_state :intimate, :enter => :make_very_happy, :exit => :never_speak_again
aasm_state :married, :enter => :give_up_intimacy, :exit => :buy_exotic_car_and_wear_a_combover

aasm_event :get_intimate do
transitions :to => :intimate, :from => [:dating], :guard => :drunk?
end

aasm_event :get_married do
transitions :to => :married, :from => [:dating, :intimate], :guard => :willing_to_give_up_manhood?
aasm :column => :status
state :dating, :enter => :make_happy, :exit => :make_depressed
state :intimate, :enter => :make_very_happy, :exit => :never_speak_again
state :married, :enter => :give_up_intimacy, :exit => :buy_exotic_car_and_wear_a_combover

event :get_intimate do
transitions :to => :intimate, :from => [:dating], :guard => :drunk?
end

event :get_married do
transitions :to => :married, :from => [:dating, :intimate], :guard => :willing_to_give_up_manhood?
end
end

aasm_initial_state Proc.new { |relationship| relationship.strictly_for_fun? ? :intimate : :dating }

def strictly_for_fun?; end
def drunk?; end
def willing_to_give_up_manhood?; end
Expand All @@ -122,14 +119,15 @@ This example uses a few of the more complex features available.
class Relationship
include AASM

aasm_state :dating
aasm_state :married
aasm do
state :dating
state :married

aasm_event :get_married,
:before => :make_vows,
:after => :eat_wedding_cake do
transitions :to => :married, :from => [:dating]
end
event :get_married,
:before => :make_vows,
:after => :eat_wedding_cake do
transitions :to => :married, :from => [:dating]
end
end
```

Expand Down
4 changes: 2 additions & 2 deletions lib/aasm/deprecated/aasm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ module AASM

module ClassMethods
def human_event_name(*args)
warn "AASM.human_event_name is deprecated and will be removed in version 3.0.0; please use AASM.aasm_human_event_name instead!"
warn "AASM.human_event_name is deprecated and will be removed in version 3.1.0; please use AASM.aasm_human_event_name instead!"
aasm_human_event_name(*args)
end
end

def human_state
warn "AASM#human_state is deprecated and will be removed in version 3.0.0; please use AASM#aasm_human_state instead!"
warn "AASM#human_state is deprecated and will be removed in version 3.1.0; please use AASM#aasm_human_state instead!"
aasm_human_state
end

Expand Down
2 changes: 1 addition & 1 deletion lib/aasm/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module AASM
VERSION = "2.4.0"
VERSION = "3.0.0"
end
42 changes: 21 additions & 21 deletions spec/models/conversation.rb
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
class Conversation
include AASM

aasm_initial_state :needs_attention
aasm do
state :needs_attention, :initial => true
state :read
state :closed
state :awaiting_response
state :junk

aasm_state :needs_attention
aasm_state :read
aasm_state :closed
aasm_state :awaiting_response
aasm_state :junk
event :new_message do
end

aasm_event :new_message do
end

aasm_event :view do
transitions :to => :read, :from => [:needs_attention]
end
event :view do
transitions :to => :read, :from => [:needs_attention]
end

aasm_event :reply do
end
event :reply do
end

aasm_event :close do
transitions :to => :closed, :from => [:read, :awaiting_response]
end
event :close do
transitions :to => :closed, :from => [:read, :awaiting_response]
end

aasm_event :junk do
transitions :to => :junk, :from => [:read]
end
event :junk do
transitions :to => :junk, :from => [:read]
end

aasm_event :unjunk do
event :unjunk do
end
end

def initialize(persister)
Expand Down
155 changes: 83 additions & 72 deletions spec/spec_helpers/models_spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@

class Foo
include AASM
aasm_initial_state :open
aasm_state :open, :exit => :exit
aasm_state :closed, :enter => :enter
aasm do
state :open, :initial => true, :exit => :exit
state :closed, :enter => :enter

aasm_event :close, :success => :success_callback do
transitions :to => :closed, :from => [:open]
end
event :close, :success => :success_callback do
transitions :to => :closed, :from => [:open]
end

aasm_event :null do
transitions :to => :closed, :from => [:open], :guard => :always_false
event :null do
transitions :to => :closed, :from => [:open], :guard => :always_false
end
end

def always_false
Expand All @@ -29,17 +30,21 @@ def exit

class FooTwo < Foo
include AASM
aasm_state :foo
aasm do
state :foo
end
end

class Bar
include AASM

aasm_state :read
aasm_state :ended
aasm do
state :read
state :ended

aasm_event :foo do
transitions :to => :ended, :from => [:read]
event :foo do
transitions :to => :ended, :from => [:read]
end
end
end

Expand All @@ -48,9 +53,11 @@ class Baz < Bar

class Banker
include AASM
aasm do
state :retired
state :selling_bad_mortgages
end
aasm_initial_state Proc.new { |banker| banker.rich? ? :retired : :selling_bad_mortgages }
aasm_state :retired
aasm_state :selling_bad_mortgages
RICH = 1_000_000
attr_accessor :balance
def initialize(balance = 0); self.balance = balance; end
Expand All @@ -59,12 +66,13 @@ def rich?; self.balance >= RICH; end

class Argument
include AASM
aasm_initial_state :invalid
aasm_state :invalid
aasm_state :valid
aasm do
state :invalid, :initial => true
state :valid

aasm_event :valid do
transitions :to => :valid, :from => [:invalid]
event :valid do
transitions :to => :valid, :from => [:invalid]
end
end
end

Expand All @@ -73,39 +81,39 @@ class AuthMachine

attr_accessor :activation_code, :activated_at, :deleted_at

aasm_initial_state :pending

aasm_state :passive
aasm_state :pending, :enter => :make_activation_code
aasm_state :active, :enter => :do_activate
aasm_state :suspended
aasm_state :deleted, :enter => :do_delete, :exit => :do_undelete

aasm_event :register do
transitions :from => :passive, :to => :pending, :guard => Proc.new {|u| u.can_register? }
end

aasm_event :activate do
transitions :from => :pending, :to => :active
end

aasm_event :suspend do
transitions :from => [:passive, :pending, :active], :to => :suspended
end

aasm_event :delete do
transitions :from => [:passive, :pending, :active, :suspended], :to => :deleted
end

# a dummy event that can never happen
aasm_event :unpassify do
transitions :from => :passive, :to => :active, :guard => Proc.new {|u| false }
end
aasm do
state :passive
state :pending, :initial => true, :enter => :make_activation_code
state :active, :enter => :do_activate
state :suspended
state :deleted, :enter => :do_delete, :exit => :do_undelete

event :register do
transitions :from => :passive, :to => :pending, :guard => Proc.new {|u| u.can_register? }
end

event :activate do
transitions :from => :pending, :to => :active
end

event :suspend do
transitions :from => [:passive, :pending, :active], :to => :suspended
end

event :delete do
transitions :from => [:passive, :pending, :active, :suspended], :to => :deleted
end

# a dummy event that can never happen
event :unpassify do
transitions :from => :passive, :to => :active, :guard => Proc.new {|u| false }
end

aasm_event :unsuspend do
transitions :from => :suspended, :to => :active, :guard => Proc.new {|u| u.has_activated? }
transitions :from => :suspended, :to => :pending, :guard => Proc.new {|u| u.has_activation_code? }
transitions :from => :suspended, :to => :passive
event :unsuspend do
transitions :from => :suspended, :to => :active, :guard => Proc.new {|u| u.has_activated? }
transitions :from => :suspended, :to => :pending, :guard => Proc.new {|u| u.has_activation_code? }
transitions :from => :suspended, :to => :passive
end
end

def initialize
Expand Down Expand Up @@ -147,30 +155,33 @@ def has_activation_code?
class ThisNameBetterNotBeInUse
include AASM

aasm_state :initial
aasm_state :symbol
aasm_state :string
aasm_state :array
aasm_state :proc
aasm do
state :initial
state :symbol
state :string
state :array
state :proc
end
end

class ChetanPatil
include AASM
aasm_initial_state :sleeping
aasm_state :sleeping
aasm_state :showering
aasm_state :working
aasm_state :dating
aasm_state :prettying_up

aasm_event :wakeup do
transitions :from => :sleeping, :to => [:showering, :working]
end

aasm_event :dress do
transitions :from => :sleeping, :to => :working, :on_transition => :wear_clothes
transitions :from => :showering, :to => [:working, :dating], :on_transition => Proc.new { |obj, *args| obj.wear_clothes(*args) }
transitions :from => :showering, :to => :prettying_up, :on_transition => [:condition_hair, :fix_hair]
aasm do
state :sleeping, :initial => true
state :showering
state :working
state :dating
state :prettying_up

event :wakeup do
transitions :from => :sleeping, :to => [:showering, :working]
end

event :dress do
transitions :from => :sleeping, :to => :working, :on_transition => :wear_clothes
transitions :from => :showering, :to => [:working, :dating], :on_transition => Proc.new { |obj, *args| obj.wear_clothes(*args) }
transitions :from => :showering, :to => :prettying_up, :on_transition => [:condition_hair, :fix_hair]
end
end

def wear_clothes(shirt_color, trouser_type)
Expand Down
Loading

0 comments on commit 8f0ff1b

Please sign in to comment.