Sha256: 7eaca44ddb476d9aed30fe2798b798e068ebc5d6119ebeeb1eef01e8e5b97557

Contents?: true

Size: 929 Bytes

Versions: 21

Compression:

Stored size: 929 Bytes

Contents

class MessageStateMachine
  include Statesman::Machine

  state :pending, initial: true
  state :unread
  state :read

  transition from: :pending, to: :unread
  transition from: :pending, to: :read
  transition from: :unread, to: :read
  transition from: :read, to: :unread

  before_transition(from: :pending, to: :read) do |message|
    message.read_at = Time.zone.now unless message.read_at
  end

  before_transition(from: :unread, to: :read) do |message|
    message.read_at = Time.zone.now unless message.read_at
  end

  before_transition(from: :read, to: :unread) do |message|
    message.read_at = nil
  end

  after_transition(from: :pending, to: :read) do |message|
    message.index
    Sunspot.commit
  end

  after_transition(from: :unread, to: :read) do |message|
    message.index
    Sunspot.commit
  end

  after_transition(from: :read, to: :unread) do |message|
    message.index
    Sunspot.commit
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
enju_message-0.1.16 app/models/message_state_machine.rb