Module: Workflow::Callbacks::ClassMethods

Defined in:
lib/workflow/callbacks.rb

Instance Method Summary collapse

Instance Method Details

#after_transitionObject

:call-seq: after_transition(*instance_method_names, options={}) after_transition(options={}, &block)

Append a callback after transition. Instance methods used for before and after transitions receive no parameters. Instance methods for around transitions will be given a block, which must be yielded/called in order for the sequence to continue.

Using a block notation, the first parameter will be an instance of the object under transition, while the second parameter (around transition only) will be the block which should be called for the sequence to continue.

== Transition Metadata

Within the callback you can access the transition_context instance variable, which will give you metadata and arguments passed to the transition. See Workflow::TransitionContext

== Options

=== If / Unless

The callback will run if or unless the named method returns a truthy value.

# Assuming some_instance_method returns a boolean, after_transition :do_something, if: :some_instance_method after_transition :do_something_else, unless: :some_instance_method

=== Only / Except

The callback will run if or unless the event being processed is in the list given

#  Run this callback only on the `accept` and `publish` events.
after_transition :do_something, only: [:accept, :publish]
#  Run this callback on events other than the `accept` and `publish` events.
after_transition :do_something_else, except: [:accept, :publish]


# File 'lib/workflow/callbacks.rb', line 86

#around_transitionObject

:call-seq: around_transition(*instance_method_names, options={}) around_transition(options={}, &block)

Append a callback around transition. Instance methods used for before and after transitions receive no parameters. Instance methods for around transitions will be given a block, which must be yielded/called in order for the sequence to continue.

Using a block notation, the first parameter will be an instance of the object under transition, while the second parameter (around transition only) will be the block which should be called for the sequence to continue.

== Transition Metadata

Within the callback you can access the transition_context instance variable, which will give you metadata and arguments passed to the transition. See Workflow::TransitionContext

== Options

=== If / Unless

The callback will run if or unless the named method returns a truthy value.

# Assuming some_instance_method returns a boolean, around_transition :do_something, if: :some_instance_method around_transition :do_something_else, unless: :some_instance_method

=== Only / Except

The callback will run if or unless the event being processed is in the list given

#  Run this callback only on the `accept` and `publish` events.
around_transition :do_something, only: [:accept, :publish]
#  Run this callback on events other than the `accept` and `publish` events.
around_transition :do_something_else, except: [:accept, :publish]


# File 'lib/workflow/callbacks.rb', line 146

#before_transitionObject

:call-seq: before_transition(*instance_method_names, options={}) before_transition(options={}, &block)

Append a callback before transition. Instance methods used for before and after transitions receive no parameters. Instance methods for around transitions will be given a block, which must be yielded/called in order for the sequence to continue.

Using a block notation, the first parameter will be an instance of the object under transition, while the second parameter (around transition only) will be the block which should be called for the sequence to continue.

== Transition Metadata

Within the callback you can access the transition_context instance variable, which will give you metadata and arguments passed to the transition. See Workflow::TransitionContext

== Options

=== If / Unless

The callback will run if or unless the named method returns a truthy value.

# Assuming some_instance_method returns a boolean, before_transition :do_something, if: :some_instance_method before_transition :do_something_else, unless: :some_instance_method

=== Only / Except

The callback will run if or unless the event being processed is in the list given

#  Run this callback only on the `accept` and `publish` events.
before_transition :do_something, only: [:accept, :publish]
#  Run this callback on events other than the `accept` and `publish` events.
before_transition :do_something_else, except: [:accept, :publish]


# File 'lib/workflow/callbacks.rb', line 26

#prepend_after_transition(options = {}, &block) ⇒ Object

Something Interesting

Prepend a callback after transition, making it the first after transition called. Options are the same as for the standard #after_transition method.



# File 'lib/workflow/callbacks.rb', line 128

#prepend_around_transition(options = {}, &block) ⇒ Object

Something Interesting

Prepend a callback around transition, making it the first around transition called. Options are the same as for the standard #around_transition method.



# File 'lib/workflow/callbacks.rb', line 188

#prepend_before_transition(options = {}, &block) ⇒ Object

Something Interesting

Prepend a callback before transition, making it the first before transition called. Options are the same as for the standard #before_transition method.



# File 'lib/workflow/callbacks.rb', line 68

#skip_after_transitionObject

:call-seq: skip_after_transition(names)

Skip a callback after transition. Options are the same as for the standard #after_transition method.



# File 'lib/workflow/callbacks.rb', line 138

#skip_around_transitionObject

:call-seq: skip_around_transition(names)

Skip a callback around transition. Options are the same as for the standard #around_transition method.



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/workflow/callbacks.rb', line 206

[:before, :after, :around].each do |callback|
  CALLBACK_MAP.each do |type, context_attribute|
    define_method "#{callback}_#{type}" do |*names, &blk|
      _insert_callbacks(names, context_attribute, blk) do |name, options|
        set_callback(type, callback, name, options)
      end
    end

    define_method "prepend_#{callback}_#{type}" do |*names, &blk|
      _insert_callbacks(names, context_attribute, blk) do |name, options|
        set_callback(type, callback, name, options.merge(prepend: true))
      end
    end

    # Skip a before, after or around callback. See _insert_callbacks
    # for details on the allowed parameters.
    define_method "skip_#{callback}_#{type}" do |*names|
      _insert_callbacks(names, context_attribute) do |name, options|
        skip_callback(type, callback, name, options)
      end
    end

    # *_action is the same as append_*_action
    alias_method :"append_#{callback}_#{type}", :"#{callback}_#{type}"
  end
end

#skip_before_transitionObject

:call-seq: skip_before_transition(names)

Skip a callback before transition. Options are the same as for the standard #before_transition method.



# File 'lib/workflow/callbacks.rb', line 78