Module: Spree::Core::StateMachines::Order::ClassMethods
- Defined in:
- lib/spree/core/state_machines/order.rb
Instance Attribute Summary collapse
- #checkout_steps ⇒ Object
- #next_event_transitions ⇒ Object
-
#previous_states ⇒ Object
Returns the value of attribute previous_states.
- #removed_transitions ⇒ Object
Instance Method Summary collapse
- #add_transition(options) ⇒ Object
- #checkout_flow(&block) ⇒ Object
- #checkout_step_names ⇒ Object
- #define_state_machine! ⇒ Object
- #find_transition(options = {}) ⇒ Object
- #go_to_state(name, options = {}) ⇒ Object
- #insert_checkout_step(name, options = {}) ⇒ Object
- #remove_checkout_step(name) ⇒ Object
- #remove_transition(options = {}) ⇒ Object
Instance Attribute Details
#checkout_steps ⇒ Object
205 206 207 |
# File 'lib/spree/core/state_machines/order.rb', line 205 def checkout_steps @checkout_steps ||= {} end |
#next_event_transitions ⇒ Object
201 202 203 |
# File 'lib/spree/core/state_machines/order.rb', line 201 def next_event_transitions @next_event_transitions ||= [] end |
#previous_states ⇒ Object
Returns the value of attribute previous_states.
12 13 14 |
# File 'lib/spree/core/state_machines/order.rb', line 12 def previous_states @previous_states end |
#removed_transitions ⇒ Object
217 218 219 |
# File 'lib/spree/core/state_machines/order.rb', line 217 def removed_transitions @removed_transitions ||= [] end |
Instance Method Details
#add_transition(options) ⇒ Object
213 214 215 |
# File 'lib/spree/core/state_machines/order.rb', line 213 def add_transition() next_event_transitions << { .delete(:from) => .delete(:to) }.merge() end |
#checkout_flow(&block) ⇒ Object
17 18 19 20 21 22 23 24 |
# File 'lib/spree/core/state_machines/order.rb', line 17 def checkout_flow(&block) if block_given? @checkout_flow = block define_state_machine! else @checkout_flow end end |
#checkout_step_names ⇒ Object
209 210 211 |
# File 'lib/spree/core/state_machines/order.rb', line 209 def checkout_step_names checkout_steps.keys end |
#define_state_machine! ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/spree/core/state_machines/order.rb', line 26 def define_state_machine! self.checkout_steps = {} self.next_event_transitions = [] self.previous_states = [:cart] self.removed_transitions = [] # Build the checkout flow using the checkout_flow defined either # within the Order class, or a decorator for that class. # # This method may be called multiple times depending on if the # checkout_flow is re-defined in a decorator or not. instance_eval(&checkout_flow) klass = self # To avoid multiple occurrences of the same transition being defined # On first definition, state_machines will not be defined state_machines.clear if respond_to?(:state_machines) state_machine :state, initial: :cart, use_transactions: false do klass.next_event_transitions.each { |state| transition(state.merge(on: :next)) } # Persist the state on the order after_transition do |order, transition| # Hard to say if this is really necessary, it was introduced in this commit: # https://github.com/mamhoff/solidus/commit/fa1d66c42e4c04ee7cd1c20d87e4cdb74a226d3d # But it seems to be harmless, so we'll keep it for now. order.state = order.state # rubocop:disable Lint/SelfAssignment order.state_changes.create( previous_state: transition.from, next_state: transition.to, name: 'order', user_id: order.user_id ) order.save end event :cancel do transition to: :canceled, if: :allow_cancel?, from: :complete end event :return do transition to: :returned, from: [:complete, :awaiting_return], if: :all_inventory_units_returned? end event :resume do transition to: :resumed, from: :canceled, if: :canceled? end event :authorize_return do transition to: :awaiting_return, from: :complete end event :complete do transition to: :complete, from: klass.checkout_steps.keys.last end if states[:payment] event :payment_failed do transition to: :payment, from: :confirm end after_transition to: :complete, do: :add_payment_sources_to_wallet before_transition to: :payment, do: :add_default_payment_from_wallet before_transition to: :payment, do: :ensure_billing_address before_transition to: :confirm, do: :add_store_credit_payments # see also process_payments_before_complete below which needs to # be added in the correct sequence. end before_transition from: :cart, do: :ensure_line_items_present if states[:address] before_transition to: :address, do: :assign_default_user_addresses before_transition from: :address, do: :persist_user_address! end if states[:delivery] before_transition to: :delivery, do: :ensure_shipping_address before_transition to: :delivery, do: :create_proposed_shipments before_transition to: :delivery, do: :ensure_available_shipping_rates before_transition from: :delivery, do: :apply_shipping_promotions end before_transition to: :resumed, do: :ensure_line_item_variants_are_not_deleted before_transition to: :resumed, do: :validate_line_item_availability # Sequence of before_transition to: :complete # calls matter so that we do not process payments # until validations have passed before_transition to: :complete, do: :validate_line_item_availability before_transition to: :complete, do: :ensure_promotions_eligible before_transition to: :complete, do: :ensure_line_item_variants_are_not_deleted before_transition to: :complete, do: :ensure_inventory_units if states[:payment] before_transition to: :complete, do: :process_payments_before_complete end after_transition to: :complete, do: :finalize after_transition to: :resumed, do: :after_resume after_transition to: :canceled, do: :after_cancel after_transition from: any - :cart, to: any - [:confirm, :complete] do |order| order.recalculate end after_transition do |order, transition| order.logger.debug "Order #{order.number} transitioned from #{transition.from} to #{transition.to} via #{transition.event}" end after_failure do |order, transition| order.logger.debug "Order #{order.number} halted transition on event #{transition.event} state #{transition.from}: #{order.errors..join}" end end end |
#find_transition(options = {}) ⇒ Object
193 194 195 196 197 198 199 |
# File 'lib/spree/core/state_machines/order.rb', line 193 def find_transition( = {}) return nil if .nil? || !.include?(:from) || !.include?(:to) next_event_transitions.detect do |transition| transition[[:from].to_sym] == [:to].to_sym end end |
#go_to_state(name, options = {}) ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/spree/core/state_machines/order.rb', line 144 def go_to_state(name, = {}) checkout_steps[name] = previous_states.each do |state| add_transition({ from: state, to: name }.merge()) end if [:if] previous_states << name else self.previous_states = [name] end end |
#insert_checkout_step(name, options = {}) ⇒ Object
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/spree/core/state_machines/order.rb', line 156 def insert_checkout_step(name, = {}) before = .delete(:before) after = .delete(:after) unless before after = checkout_steps.keys.last unless before || after cloned_steps = checkout_steps.clone cloned_removed_transitions = removed_transitions.clone checkout_flow do cloned_steps.each_pair do |key, value| go_to_state(name, ) if key == before go_to_state(key, value) go_to_state(name, ) if key == after end cloned_removed_transitions.each do |transition| remove_transition(transition) end end end |
#remove_checkout_step(name) ⇒ Object
175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/spree/core/state_machines/order.rb', line 175 def remove_checkout_step(name) cloned_steps = checkout_steps.clone cloned_removed_transitions = removed_transitions.clone checkout_flow do cloned_steps.each_pair do |key, value| go_to_state(key, value) unless key == name end cloned_removed_transitions.each do |transition| remove_transition(transition) end end end |
#remove_transition(options = {}) ⇒ Object
188 189 190 191 |
# File 'lib/spree/core/state_machines/order.rb', line 188 def remove_transition( = {}) removed_transitions << next_event_transitions.delete(find_transition()) end |