module WorkflowActiverecord::Scopes

This module will automatically generate ActiveRecord scopes based on workflow states.
The name of each generated scope will be something like `with_<state_name>_state`

Examples:

Article.with_pending_state # => ActiveRecord::Relation
Payment.without_refunded_state # => ActiveRecord::Relation

`

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

Public Class Methods

extended(object) click to toggle source
# File lib/workflow-activerecord.rb, line 49
def self.extended(object)
  class << object
    alias_method :workflow_without_scopes, :workflow unless method_defined?(:workflow_without_scopes)
    alias_method :workflow, :workflow_with_scopes
  end
end

Public Instance Methods

workflow_with_scopes(&specification) click to toggle source
# File lib/workflow-activerecord.rb, line 56
def workflow_with_scopes(&specification)
  workflow_without_scopes(&specification)
  states = workflow_spec.states.values

  states.each do |state|
    define_singleton_method("with_#{state}_state") do
      where("#{table_name}.#{self.workflow_column.to_sym} = ?", state.to_s)
    end

    define_singleton_method("without_#{state}_state") do
      where.not("#{table_name}.#{self.workflow_column.to_sym} = ?", state.to_s)
    end
  end
end