Module: Workflow::Adapters::ActiveRecord::ClassMethods

Defined in:
lib/workflow/adapters/active_record.rb

Overview

Scopes for the ActiveRecord entity class.

Two scopes for each State specified plus some general ones:

Examples:

class Article < ApplicationRecord
  include Workflow
  state :pending, tags: :some_tag do
    on :submit, to: :submitted
  end
  state :submitted
end
Article.with_pending_state # => ActiveRecord::Relation
Article.without_pending_state # => ActiveRecord::Relation
Article.state_tagged_with(:some_tag, :another_tag)
Article.state_not_tagged_with(:some_tag)
Article.in_terminal_state

Example above just adds where(:state_column_name => 'pending') or where.not(:state_column_name => 'pending') to AR query and returns ActiveRecord::Relation.

Instance Method Summary collapse

Instance Method Details

#in_terminal_stateActiveRecord::Relation

Find objects that are in a terminal state - no available event transitions

Returns:

  • (ActiveRecord::Relation)

    ActiveRecord query object meeting the criteria



81
82
83
# File 'lib/workflow/adapters/active_record.rb', line 81

def in_terminal_state
  with_state workflow_spec.states.select(&:terminal?)
end

#not_in_terminal_stateActiveRecord::Relation

Find objects that are not in a terminal state

Returns:

  • (ActiveRecord::Relation)

    ActiveRecord query object meeting the criteria



87
88
89
# File 'lib/workflow/adapters/active_record.rb', line 87

def not_in_terminal_state
  with_state(workflow_spec.states.reject(&:terminal?))
end

#state_not_tagged_with(*tags) ⇒ ActiveRecord::Relation

Locate objects that are not in a state tagged with the given tag(s)

Parameters:

  • tags (Symbol)

    List of tags the objects (and their states) should not have

Returns:

  • (ActiveRecord::Relation)

    ActiveRecord query object meeting the criteria



103
104
105
# File 'lib/workflow/adapters/active_record.rb', line 103

def state_not_tagged_with(*tags)
  with_state workflow_spec.states.not_tagged_with(tags)
end

#state_tagged_with(*tags) ⇒ ActiveRecord::Relation

Locate objects that are in a state tagged with the given tag(s)

Parameters:

  • tags (Symbol)

    List of tags that apply

Returns:

  • (ActiveRecord::Relation)

    ActiveRecord query object meeting the criteria



95
96
97
# File 'lib/workflow/adapters/active_record.rb', line 95

def state_tagged_with(*tags)
  with_state workflow_spec.states.tagged_with(tags)
end

#with_state(*states) ⇒ ActiveRecord::Relation

Find objects in the given state(s)

Parameters:

  • states (Object)

    Symbol, String or State objects

Returns:

  • (ActiveRecord::Relation)

    ActiveRecord query object meeting the criteria



111
112
113
114
115
116
117
118
119
120
121
# File 'lib/workflow/adapters/active_record.rb', line 111

def with_state(*states)
  states = [states].flatten
  states.map! do |state|
    if state.is_a?(Workflow::State)
      state.name.to_s
    else
      state.to_s
    end
  end
  where(workflow_state: states)
end

#workflow(&specification) ⇒ Object



73
74
75
76
# File 'lib/workflow/adapters/active_record.rb', line 73

def workflow(&specification)
  super
  workflow_spec.states.each { |state| define_scopes(state) }
end

#workflow_column(column_name = nil) ⇒ Symbol

Instructs Workflow which column to use to persist workflow state.

Parameters:

  • column_name (Symbol) (defaults to: nil)

    If provided, will set a new workflow column name.

Returns:

  • (Symbol)

    The current (or new) name for the workflow column on this class.



68
69
70
71
# File 'lib/workflow/adapters/active_record.rb', line 68

def workflow_column(column_name = nil)
  @workflow_column = column_name.to_sym if column_name
  @workflow_column ||= superclass_workflow || :workflow_state
end