Module: ScottBarron::Acts::StateMachine::ClassMethods
- Defined in:
- lib/acts_as_state_machine.rb
Instance Method Summary collapse
-
#calculate_in_state(state, *args) ⇒ Object
Wraps ActiveRecord::Base.calculate to conveniently calculate all records in a given state.
-
#count_in_state(state, *args) ⇒ Object
Wraps ActiveRecord::Base.count to conveniently count all records in a given state.
-
#event(event, opts = {}, &block) ⇒ Object
Define an event.
-
#find_in_state(number, state, *args) ⇒ Object
Wraps ActiveRecord::Base.find to conveniently find all records in a given state.
-
#state(name, opts = {}) ⇒ Object
Define a state of the system.
-
#states ⇒ Object
Returns an array of all known states.
Instance Method Details
#calculate_in_state(state, *args) ⇒ Object
Wraps ActiveRecord::Base.calculate to conveniently calculate all records in a given state. Options:
-
state
- The state to find -
args
- The rest of the args are passed down to ActiveRecordcalculate
256 257 258 259 260 |
# File 'lib/acts_as_state_machine.rb', line 256 def calculate_in_state(state, *args) with_state_scope state do calculate(*args) end end |
#count_in_state(state, *args) ⇒ Object
Wraps ActiveRecord::Base.count to conveniently count all records in a given state. Options:
-
state
- The state to find -
args
- The rest of the args are passed down to ActiveRecordfind
245 246 247 248 249 |
# File 'lib/acts_as_state_machine.rb', line 245 def count_in_state(state, *args) with_state_scope state do count(*args) end end |
#event(event, opts = {}, &block) ⇒ Object
Define an event. This takes a block which describes all valid transitions for this event.
Example:
class Order < ActiveRecord::Base
acts_as_state_machine :initial => :open
state :open
state :closed
event :close_order do
transitions :to => :closed, :from => :open
end
end
transitions
takes a hash where :to
is the state to transition to and :from
is a state (or Array of states) from which this event can be fired.
This creates an instance method used for firing the event. The method created is the name of the event followed by an exclamation point (!). Example: order.close_order!
.
201 202 203 204 205 206 207 |
# File 'lib/acts_as_state_machine.rb', line 201 def event(event, opts={}, &block) tt = read_inheritable_attribute(:transition_table) e = SupportingClasses::Event.new(event, opts, tt, &block) write_inheritable_hash(:event_table, event.to_sym => e) define_method("#{event.to_s}!") { e.fire(self) } end |
#find_in_state(number, state, *args) ⇒ Object
Wraps ActiveRecord::Base.find to conveniently find all records in a given state. Options:
-
number
- This is just :first or :all from ActiveRecordfind
-
state
- The state to find -
args
- The rest of the args are passed down to ActiveRecordfind
234 235 236 237 238 |
# File 'lib/acts_as_state_machine.rb', line 234 def find_in_state(number, state, *args) with_state_scope state do find(number, *args) end end |
#state(name, opts = {}) ⇒ Object
Define a state of the system. state
can take an optional Proc object which will be executed every time the system transitions into that state. The proc will be passed the current object.
Example:
class Order < ActiveRecord::Base
acts_as_state_machine :initial => :open
state :open
state :closed, Proc.new { |o| Mailer.send_notice(o) }
end
221 222 223 224 225 226 |
# File 'lib/acts_as_state_machine.rb', line 221 def state(name, opts={}) state = SupportingClasses::State.new(name, opts) write_inheritable_hash(:states, state.value => state) define_method("#{state.name}?") { current_state.to_s == state.value } end |
#states ⇒ Object
Returns an array of all known states.
174 175 176 |
# File 'lib/acts_as_state_machine.rb', line 174 def states read_inheritable_attribute(:states).keys.collect { |state| state.to_sym } end |