Class: FlowCore::Workflow
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- FlowCore::Workflow
- Defined in:
- app/models/flow_core/workflow.rb
Constant Summary collapse
- FORBIDDEN_ATTRIBUTES =
%i[verified verified_at created_at updated_at].freeze
Instance Method Summary collapse
- #build_instance(attributes = {}) ⇒ Object
- #create_instance(attributes = {}) ⇒ Object
- #create_instance!(attributes = {}) ⇒ Object
- #fork ⇒ Object
- #invalid? ⇒ Boolean
- #reset_workflow_verification! ⇒ Object
- #verify! ⇒ Object
- #verify? ⇒ Boolean
- #verify_status ⇒ Object
- #violations ⇒ Object
Instance Method Details
#build_instance(attributes = {}) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 |
# File 'app/models/flow_core/workflow.rb', line 24 def build_instance(attributes = {}) if force_workflow_verified_on_create_instance && invalid? raise FlowCore::UnverifiedWorkflow, "Workflow##{id} didn't do soundness check yet." end instance = instances.new type: instance_class on_build_instance(instance) instance.assign_attributes attributes.except(FlowCore::Instance::FORBIDDEN_ATTRIBUTES) instance end |
#create_instance(attributes = {}) ⇒ Object
36 37 38 39 40 41 42 |
# File 'app/models/flow_core/workflow.rb', line 36 def create_instance(attributes = {}) instance = build_instance(attributes) return unless instance instance.save instance end |
#create_instance!(attributes = {}) ⇒ Object
44 45 46 47 48 49 50 51 52 |
# File 'app/models/flow_core/workflow.rb', line 44 def create_instance!(attributes = {}) instance = build_instance(attributes) unless instance raise RecordNotSaved.new("Failed to create workflow instance", instance) end instance.save! instance end |
#fork ⇒ Object
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'app/models/flow_core/workflow.rb', line 137 def fork new_workflow = dup transaction do yield new_workflow if block_given? new_workflow.save! new_transitions = transitions.includes(:trigger, :callbacks).map do |t| new_transition = t.dup new_transition.workflow = new_workflow new_transition.save! if t.trigger new_trigger = t.trigger.dup new_trigger.workflow = new_workflow new_trigger.transition = new_transition new_trigger.save! end t.callbacks.find_each do |cb| new_cb = cb.dup new_cb.workflow = new_workflow new_cb.transition = new_transition new_cb.save! end [t.id, new_transition.id] end.to_h new_places = places.map do |p| new_place = p.dup new_place.workflow = new_workflow new_place.save! [p.id, new_place.id] end.to_h arcs.includes(:guards).find_each do |a| new_arc = a.dup new_arc.workflow = new_workflow new_arc.place_id = new_places[a.place_id] new_arc.transition_id = new_transitions[a.transition_id] new_arc.save! a.guards.find_each do |g| new_guard = g.dup new_guard.workflow = new_workflow new_guard.arc = new_arc new_guard.save! end end new_workflow.verify! end new_workflow end |
#invalid? ⇒ Boolean
54 55 56 |
# File 'app/models/flow_core/workflow.rb', line 54 def invalid? !verified? end |
#reset_workflow_verification! ⇒ Object
131 132 133 134 135 |
# File 'app/models/flow_core/workflow.rb', line 131 def reset_workflow_verification! return unless verified? update! verified: false, verified_at: nil end |
#verify! ⇒ Object
126 127 128 129 |
# File 'app/models/flow_core/workflow.rb', line 126 def verify! update! verified: verify?, verified_at: Time.zone.now verified end |
#verify? ⇒ Boolean
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'app/models/flow_core/workflow.rb', line 58 def verify? violations.clear unless start_place violations.add(self, :no_start_place) end unless end_place violations.add(self, :no_end_place) end return false unless start_place && end_place # TODO: Naive implementation for now, Help wanted! rgl = to_rgl start_place_code = "P_#{start_place.id}" end_place_code = "P_#{end_place.id}" unless rgl.path?(start_place_code, end_place_code) violations.add end_place, :unreachable end places.find_each do |p| next if p == start_place next if p == end_place place_code = "P_#{p.id}" unless rgl.path?(start_place_code, place_code) violations.add p, :unreachable end unless rgl.path?(place_code, end_place_code) violations.add p, :impassable end end transitions.includes(:trigger).find_each do |t| transition_code = "T_#{t.id}" unless rgl.path?(start_place_code, transition_code) violations.add t, :unreachable end unless rgl.path?(transition_code, end_place_code) violations.add t, :impassable end t.verify violations: violations end violations.empty? end |
#verify_status ⇒ Object
116 117 118 119 120 121 122 123 124 |
# File 'app/models/flow_core/workflow.rb', line 116 def verify_status if verified_at.blank? :unverified elsif verified? :verified else :invalid end end |
#violations ⇒ Object
112 113 114 |
# File 'app/models/flow_core/workflow.rb', line 112 def violations @violations ||= FlowCore::Violations.new end |