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.
99
100
101
|
# File 'lib/flow_machine/workflow.rb', line 99
def changes
@changes
end
|
#options ⇒ Object
Returns the value of attribute options.
98
99
100
|
# File 'lib/flow_machine/workflow.rb', line 98
def options
@options
end
|
#previous_state ⇒ Object
Returns the value of attribute previous_state.
98
99
100
|
# File 'lib/flow_machine/workflow.rb', line 98
def previous_state
@previous_state
end
|
#previous_state_persistence_callbacks ⇒ Object
Returns the value of attribute previous_state_persistence_callbacks.
98
99
100
|
# File 'lib/flow_machine/workflow.rb', line 98
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
158
159
160
|
# File 'lib/flow_machine/workflow.rb', line 158
def create?
changes[state_method.to_s].try(:first).blank?
end
|
#current_state ⇒ Object
120
121
122
|
# File 'lib/flow_machine/workflow.rb', line 120
def current_state
@current_state ||= self.class.states[current_state_name.to_sym].new(self)
end
|
#current_state_name ⇒ Object
Also known as:
state
111
112
113
|
# File 'lib/flow_machine/workflow.rb', line 111
def current_state_name
object.send(self.class.state_method)
end
|
#current_state_name=(new_state) ⇒ Object
166
167
168
169
170
|
# File 'lib/flow_machine/workflow.rb', line 166
def current_state_name=(new_state)
raise ArgumentError, "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
105
106
107
108
109
|
# File 'lib/flow_machine/workflow.rb', line 105
def initialize(object, options = {})
@object = object
@options = options
@previous_state_persistence_callbacks = []
end
|
#persist ⇒ Object
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
# File 'lib/flow_machine/workflow.rb', line 137
def persist
self.changes = object.changes
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
162
163
164
|
# File 'lib/flow_machine/workflow.rb', line 162
def persist_object
object.save
end
|
#previous_state_name ⇒ Object
116
117
118
|
# File 'lib/flow_machine/workflow.rb', line 116
def previous_state_name
@previous_state.try(:name)
end
|
#save ⇒ Object
133
134
135
|
# File 'lib/flow_machine/workflow.rb', line 133
def save
persist
end
|
#transition(options = {}) ⇒ Object
124
125
126
127
128
129
130
131
|
# File 'lib/flow_machine/workflow.rb', line 124
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
|