geekq / workflow

Ruby finite-state-machine-inspired API for modeling workflow
MIT License
1.75k stars 207 forks source link

current_state in case statement #133

Closed toydestroyer closed 9 years ago

toydestroyer commented 9 years ago

I have a simple model:

class Article < ActiveRecord::Base

  include Workflow

  workflow do
    state :unreviewed do
      event :accept, transitions_to: :accepted
      event :reject, transitions_to: :rejected
    end
    state :accepted
    state :rejected
  end

  def table_class
    case self.current_state
    when :unreviewed
      'warning'
    when :accepted
      'success'
    when :rejected
      'danger'
    else
      'info'
    end
  end

end

I want to use table_class method to highlight rows in html table. But it always returns info.

Is there any way to use current_state in case statement or i should use workflow_state directly from database?

Thanks.

nyon commented 9 years ago

not tested, but i think

  def table_class
    case self.current_state.to_sym
    when :unreviewed
      'warning'
    when :accepted
      'success'
    when :rejected
      'danger'
    else
      'info'
    end
  end

should work

toydestroyer commented 9 years ago

It works! Thanks @nyon. And my apologies to everyone for such silly question.