0

given the below;

aasm do
  state :available, :intitial => true
  state :presented
  state :invited

  event :present do
    transitions :from => :available, :to => :presented
  end

  event :invite do
    transitions :from => :presented, :to => :invited
  end

  event :provide do
    transitions :from => [:presented, :invited], :to => :available
  end
end

what is an optimal pattern for setting the time period that an object 'lives under' a given state ?

ie, once the 'present' event occurs, I'd like the object to maintain the 'presented' state for exactly two hours, i'm feeling like I will have to mangle the way aasm works to achieve this, any thoughts ?

extra: this aasm code is being inserted into an active record class in a rails app, postgres is the db. Thx!

John
  • 1,246
  • 15
  • 34
  • 1
    I don't think AASM supports scheduling. You have to set up a background job that changes the object's state after a given time. – Stefan Apr 23 '14 at 16:21
  • yeah I was thinking that, and then schedule precise, granular actions that occur in the future? this seems messy, what if for some reason the state manually changes before the scheduled event and I have actions in the queue ? there is no linkage between events in the queue and the objects themselves ? – John Apr 23 '14 at 16:24
  • Instead of having a background job for each record, you could save a timestamp when setting the 'present' state (e.g. `presented_at`) and run a cron job every minute that updates the appropriate records. Something like `Record.presented.where("presented_at < ?", 2.hours.ago).each { |r| r.provide }` – Stefan Apr 23 '14 at 16:38

0 Answers0