Module: FlowMachine::Workflow::ClassMethods

Defined in:
lib/flow_machine/workflow.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#callbacksObject

Returns the value of attribute callbacks.



37
38
39
# File 'lib/flow_machine/workflow.rb', line 37

def callbacks
  @callbacks
end

Instance Method Details

#after_save(*args, &block) ⇒ Object



77
78
79
# File 'lib/flow_machine/workflow.rb', line 77

def after_save(*args, &block)
  add_callback(:after_save, FlowMachine::Callback.new(*args, &block))
end

#after_transition(*args, &block) ⇒ Object



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

def after_transition(*args, &block)
  add_callback(:after_transition, FlowMachine::Callback.new(*args, &block))
end

#before_save(*args, &block) ⇒ Object



73
74
75
# File 'lib/flow_machine/workflow.rb', line 73

def before_save(*args, &block)
  add_callback(:before_save, FlowMachine::Callback.new(*args, &block))
end

#create_scopes_on(content_class) ⇒ Object

resolves to class Model

def self.published
  where(status: 'published')
end

def published?
  self.status == 'published'
end

end



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/flow_machine/workflow.rb', line 25

def create_scopes_on(content_class)
  self.state_names.each do |status|
    content_class.singleton_class.send(:define_method, status) do
      where(status: status)
    end

    content_class.send(:define_method, "#{status}?") do
      self.status == status
    end
  end
end

#refresh_state_methods!Object

Mainly to be used in testing, call this method to ensure that any dynamically created state methods get exposed to the workflow



49
50
51
52
53
# File 'lib/flow_machine/workflow.rb', line 49

def refresh_state_methods!
  states.values.each do |state_class|
    add_state_methods_from(state_class)
  end
end

#state(state_class) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/flow_machine/workflow.rb', line 55

def state(state_class)
  name = get_state_name(state_class)
  states[name] = state_class
  add_state_methods_from(state_class)

  define_method "#{name}?" do
    current_state_name.to_s == name.to_s
  end
end

#state_attribute(method) ⇒ Object



65
66
67
# File 'lib/flow_machine/workflow.rb', line 65

def state_attribute(method)
  @state_attribute = method
end

#state_methodObject



69
70
71
# File 'lib/flow_machine/workflow.rb', line 69

def state_method
  @state_attribute || :state
end

#state_namesObject



39
40
41
# File 'lib/flow_machine/workflow.rb', line 39

def state_names
  states.keys.map &:to_s
end

#statesObject



43
44
45
# File 'lib/flow_machine/workflow.rb', line 43

def states
  @states ||= {}
end