Module: FlowMachine::Workflow

Extended by:
FactoryMethods
Defined in:
lib/flow_machine/workflow.rb

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from FactoryMethods

class_for, for, for_collection

Instance Attribute Details

#changesObject

Returns the value of attribute changes.



113
114
115
# File 'lib/flow_machine/workflow.rb', line 113

def changes
  @changes
end

#optionsObject

Returns the value of attribute options.



112
113
114
# File 'lib/flow_machine/workflow.rb', line 112

def options
  @options
end

#previous_stateObject

Returns the value of attribute previous_state.



112
113
114
# File 'lib/flow_machine/workflow.rb', line 112

def previous_state
  @previous_state
end

#previous_state_persistence_callbacksObject

Returns the value of attribute previous_state_persistence_callbacks.



112
113
114
# File 'lib/flow_machine/workflow.rb', line 112

def previous_state_persistence_callbacks
  @previous_state_persistence_callbacks
end

Class Method Details

.included(base) ⇒ Object



9
10
11
12
# File 'lib/flow_machine/workflow.rb', line 9

def self.included(base)
  base.extend(ClassMethods)
  base.send(:attr_reader, :object)
end

Instance Method Details

#create?Boolean

Useful for using in if/unless on state and after_save callbacks so you can run the callback only on the initial persistence

Returns:

  • (Boolean)


172
173
174
# File 'lib/flow_machine/workflow.rb', line 172

def create?
  self.changes[state_method.to_s].try(:first).blank?
end

#current_stateObject



134
135
136
# File 'lib/flow_machine/workflow.rb', line 134

def current_state
  @current_state ||= self.class.states[current_state_name.to_sym].new(self)
end

#current_state_nameObject Also known as: state



125
126
127
# File 'lib/flow_machine/workflow.rb', line 125

def current_state_name
  object.send(self.class.state_method)
end

#current_state_name=(new_state) ⇒ Object

Raises:

  • (ArgumentError)


180
181
182
183
# File 'lib/flow_machine/workflow.rb', line 180

def current_state_name=(new_state)
  raise ArgumentError.new("invalid state: #{new_state}") unless self.class.state_names.include?(new_state.to_s)
  object.send("#{self.class.state_method}=", new_state)
end

#current_userObject



185
186
187
# File 'lib/flow_machine/workflow.rb', line 185

def current_user
  options[:current_user]
end

#initialize(object, options = {}) ⇒ Object



119
120
121
122
123
# File 'lib/flow_machine/workflow.rb', line 119

def initialize(object, options = {})
  @object = object
  @options = options
  @previous_state_persistence_callbacks = []
end

#persistObject



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/flow_machine/workflow.rb', line 151

def persist
  self.changes = object.changes
  # If the model has a default state from the database, then it doesn't get
  # included in `changes` when you're first saving it.
  self.changes[state_method.to_s] ||= [nil, current_state_name] if object.new_record?

  fire_callbacks(:before_save)
  current_state.fire_callbacks(:before_change, changes)

  if persist_object
    fire_state_callbacks
    fire_callbacks(:after_save)
    true
  else
    self.current_state_name = @previous_state.name.to_s if @previous_state
    false
  end
end

#persist_objectObject



176
177
178
# File 'lib/flow_machine/workflow.rb', line 176

def persist_object
  object.save
end

#previous_state_nameObject



130
131
132
# File 'lib/flow_machine/workflow.rb', line 130

def previous_state_name
  @previous_state.try(:name)
end

#saveObject



147
148
149
# File 'lib/flow_machine/workflow.rb', line 147

def save
  persist
end

#transition(options = {}) ⇒ Object



138
139
140
141
142
143
144
145
# File 'lib/flow_machine/workflow.rb', line 138

def transition(options = {})
  @previous_state = current_state
  @current_state = nil
  self.current_state_name = options[:to].to_s if options[:to]
  @previous_state_persistence_callbacks << FlowMachine::StateCallback.new(options[:after]) if options[:after]
  fire_callbacks(:after_transition) unless previous_state == current_state
  current_state
end