Module: TaliaCore::Workflow::ClassMethods

Defined in:
lib/talia_core/workflow.rb

Instance Method Summary collapse

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 ActiveRecord calculate



284
285
286
287
288
# File 'lib/talia_core/workflow.rb', line 284

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 ActiveRecord find



273
274
275
276
277
# File 'lib/talia_core/workflow.rb', line 273

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!.



229
230
231
232
233
234
235
# File 'lib/talia_core/workflow.rb', line 229

def event(event, opts={}, &block)
  tt = read_inheritable_attribute(:transition_table)
    
  et = read_inheritable_attribute(:event_table)
  e = et[event.to_sym] = SupportingClasses::Event.new(event, opts, tt, &block)
  define_method("#{event.to_s}!") { |user, *args| e.fire(self, user, args) }
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 ActiveRecord find

  • state - The state to find

  • args - The rest of the args are passed down to ActiveRecord find



262
263
264
265
266
# File 'lib/talia_core/workflow.rb', line 262

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



249
250
251
252
253
254
# File 'lib/talia_core/workflow.rb', line 249

def state(name, opts={})
  state = SupportingClasses::State.new(name.to_sym, opts)
  read_inheritable_attribute(:states)[name.to_sym] = state
  
  define_method("#{state.name}?") { current_state == state.name }
end

#statesObject

Returns an array of all known states.



202
203
204
# File 'lib/talia_core/workflow.rb', line 202

def states
  read_inheritable_attribute(:states).keys
end