Module: Workflow::WorkflowClassMethods

Defined in:
lib/workflow.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#workflow_specObject (readonly)

Returns the value of attribute workflow_spec.



129
130
131
# File 'lib/workflow.rb', line 129

def workflow_spec
  @workflow_spec
end

Instance Method Details

#workflow(&specification) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# 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) ⇒ Object



131
132
133
134
135
136
137
138
139
# 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