Module: FlowMachine::Workflow
- Extended by:
- FactoryMethods
- Defined in:
- lib/flow_machine/workflow.rb,
lib/flow_machine/workflow/model_extension.rb
Defined Under Namespace
Modules: ClassMethods, ModelExtension
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
class_for, for, for_collection
Instance Attribute Details
#changes ⇒ Object
Returns the value of attribute changes.
100
101
102
|
# File 'lib/flow_machine/workflow.rb', line 100
def changes
@changes
end
|
#options ⇒ Object
Returns the value of attribute options.
99
100
101
|
# File 'lib/flow_machine/workflow.rb', line 99
def options
@options
end
|
#previous_state ⇒ Object
Returns the value of attribute previous_state.
99
100
101
|
# File 'lib/flow_machine/workflow.rb', line 99
def previous_state
@previous_state
end
|
#previous_state_persistence_callbacks ⇒ Object
Returns the value of attribute previous_state_persistence_callbacks.
99
100
101
|
# File 'lib/flow_machine/workflow.rb', line 99
def previous_state_persistence_callbacks
@previous_state_persistence_callbacks
end
|
Class Method Details
.included(base) ⇒ Object
10
11
12
13
14
|
# File 'lib/flow_machine/workflow.rb', line 10
def self.included(base)
base.extend(ClassMethods)
base.extend(ModelExtension)
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
159
160
161
|
# File 'lib/flow_machine/workflow.rb', line 159
def create?
self.changes[state_method.to_s].try(:first).blank?
end
|
#current_state ⇒ Object
121
122
123
|
# File 'lib/flow_machine/workflow.rb', line 121
def current_state
@current_state ||= self.class.states[current_state_name.to_sym].new(self)
end
|
#current_state_name ⇒ Object
Also known as:
state
112
113
114
|
# File 'lib/flow_machine/workflow.rb', line 112
def current_state_name
object.send(self.class.state_method)
end
|
#current_state_name=(new_state) ⇒ Object
167
168
169
170
|
# File 'lib/flow_machine/workflow.rb', line 167
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_user ⇒ Object
172
173
174
|
# File 'lib/flow_machine/workflow.rb', line 172
def current_user
options[:current_user]
end
|
#initialize(object, options = {}) ⇒ Object
106
107
108
109
110
|
# File 'lib/flow_machine/workflow.rb', line 106
def initialize(object, options = {})
@object = object
@options = options
@previous_state_persistence_callbacks = []
end
|
#persist ⇒ Object
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
# File 'lib/flow_machine/workflow.rb', line 138
def persist
self.changes = object.changes
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_object ⇒ Object
163
164
165
|
# File 'lib/flow_machine/workflow.rb', line 163
def persist_object
object.save
end
|
#previous_state_name ⇒ Object
117
118
119
|
# File 'lib/flow_machine/workflow.rb', line 117
def previous_state_name
@previous_state.try(:name)
end
|
#save ⇒ Object
134
135
136
|
# File 'lib/flow_machine/workflow.rb', line 134
def save
persist
end
|
#transition(options = {}) ⇒ Object
125
126
127
128
129
130
131
132
|
# File 'lib/flow_machine/workflow.rb', line 125
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
|