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
-
#in_terminal_state ⇒ ActiveRecord::Relation
Find objects that are in a terminal state - no available event transitions.
-
#not_in_terminal_state ⇒ ActiveRecord::Relation
Find objects that are not in a terminal state.
-
#state_not_tagged_with(*tags) ⇒ ActiveRecord::Relation
Locate objects that are not in a state tagged with the given tag(s).
-
#state_tagged_with(*tags) ⇒ ActiveRecord::Relation
Locate objects that are in a state tagged with the given tag(s).
-
#with_state(*states) ⇒ ActiveRecord::Relation
Find objects in the given state(s).
- #workflow(&specification) ⇒ Object
-
#workflow_column(column_name = nil) ⇒ Symbol
Instructs Workflow which column to use to persist workflow state.
Instance Method Details
#in_terminal_state ⇒ ActiveRecord::Relation
Find objects that are in a terminal state - no available event transitions
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_state ⇒ ActiveRecord::Relation
Find objects that are not in a terminal state
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)
103 104 105 |
# File 'lib/workflow/adapters/active_record.rb', line 103 def state_not_tagged_with(*) with_state workflow_spec.states.not_tagged_with() end |
#state_tagged_with(*tags) ⇒ ActiveRecord::Relation
Locate objects that are in a state tagged with the given tag(s)
95 96 97 |
# File 'lib/workflow/adapters/active_record.rb', line 95 def state_tagged_with(*) with_state workflow_spec.states.tagged_with() end |
#with_state(*states) ⇒ ActiveRecord::Relation
Find objects in the given state(s)
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.
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 |