module Workflow::WorkflowClassMethods

Attributes

workflow_spec[R]

Public Instance Methods

workflow(&specification) click to toggle source
# File lib/workflow.rb, line 141
def workflow(&specification)
  @workflow_spec = Specification.new(Hash.new, &specification)
  @workflow_spec.states.values.each do |state|
    state_name = state.name
    module_eval do
      define_method "#{state_name}?" do
        state_name.to_sym == current_state.name.to_sym
      end

      define_method "in_#{state_name}_exit?" do
        self.in_exit and self.in_exit.to_sym == state_name.to_sym
      end

      define_method "in_#{state_name}_entry?" do
        self.in_entry and self.in_entry.to_sym == state_name.to_sym
      end
    end

    state.events.values.each do |event|
      event_name = event.name
      module_eval do
        define_method "#{event_name}!".to_sym do |*args|
          process_event!(event_name, *args)
        end

        # this allows checks like can_approve? or can_reject_item?
        # note we don't have a more generic can?(:approve) method.
        # this is fully intentional, since this way it is far easier
        # to overwrite the can_...? mehtods in a model than it would be
        # with a generic can?(...) method.
        define_method "can_#{event_name}?" do
          return self.current_state.events.include? event_name
        end

        define_method "in_transition_#{event_name}?" do
          return false unless self.in_transition
          return self.in_transition.to_sym == event_name.to_sym
        end
      end
    end
  end
end
workflow_column(column_name=nil) click to toggle source
# File lib/workflow.rb, line 131
def workflow_column(column_name=nil)
  if column_name
    @workflow_state_column_name = column_name.to_sym
  end
  if !@workflow_state_column_name && superclass.respond_to?(:workflow_column)
    @workflow_state_column_name = superclass.workflow_column
  end
  @workflow_state_column_name ||= :workflow_state
end