Module: Workflow::Adapter::ActiveRecord::Scopes
- Defined in:
- lib/workflow/adapters/active_record.rb
Overview
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
Example above just adds ‘where(:state_column_name => ’pending’)‘ to AR query and returns ActiveRecord::Relation.
Class Method Summary collapse
Instance Method Summary collapse
Class Method Details
.extended(object) ⇒ Object
43 44 45 46 47 48 |
# File 'lib/workflow/adapters/active_record.rb', line 43 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 |
Instance Method Details
#workflow_with_scopes(&specification) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/workflow/adapters/active_record.rb', line 50 def workflow_with_scopes(&specification) workflow_without_scopes(&specification) states = workflow_spec.states.values eigenclass = class << self; self; end states.each do |state| # Use eigenclass instead of `define_singleton_method` # to be compatible with Ruby 1.8+ eigenclass.send(:define_method, "with_#{state}_state") do where("#{table_name}.#{self.workflow_column.to_sym} = ?", state.to_s) end end end |