Class: Spree::OrderMerger
- Inherits:
-
Object
- Object
- Spree::OrderMerger
- Defined in:
- app/models/spree/order_merger.rb
Instance Attribute Summary collapse
-
#order ⇒ Object
Returns the value of attribute order.
Instance Method Summary collapse
-
#find_matching_line_item(other_order_line_item) ⇒ Object
Compare the line item of the other order with mine.
-
#handle_error(line_item) ⇒ Object
Change the error messages as you choose.
-
#handle_merge(current_line_item, other_order_line_item) ⇒ Object
The idea is the end developer can choose to override the merge to their own choosing.
-
#initialize(order) ⇒ OrderMerger
constructor
A new instance of OrderMerger.
- #merge!(other_order, user = nil) ⇒ Object
- #persist_merge ⇒ Object
- #set_user(user = nil) ⇒ Object
Constructor Details
#initialize(order) ⇒ OrderMerger
Returns a new instance of OrderMerger.
6 7 8 |
# File 'app/models/spree/order_merger.rb', line 6 def initialize(order) @order = order end |
Instance Attribute Details
#order ⇒ Object
Returns the value of attribute order.
3 4 5 |
# File 'app/models/spree/order_merger.rb', line 3 def order @order end |
Instance Method Details
#find_matching_line_item(other_order_line_item) ⇒ Object
Compare the line item of the other order with mine. Make sure you allow any extensions to chime in on whether or not the extension-specific parts of the line item match
29 30 31 32 33 34 35 36 |
# File 'app/models/spree/order_merger.rb', line 29 def find_matching_line_item(other_order_line_item) order.line_items.detect do |my_li| my_li.variant == other_order_line_item.variant && order.line_item_comparison_hooks.all? do |hook| order.send(hook, my_li, other_order_line_item.serializable_hash) end end end |
#handle_error(line_item) ⇒ Object
Change the error messages as you choose.
55 56 57 |
# File 'app/models/spree/order_merger.rb', line 55 def handle_error(line_item) order.errors[:base] << line_item.errors. end |
#handle_merge(current_line_item, other_order_line_item) ⇒ Object
The idea is the end developer can choose to override the merge to their own choosing. Default is merge with errors.
44 45 46 47 48 49 50 51 52 |
# File 'app/models/spree/order_merger.rb', line 44 def handle_merge(current_line_item, other_order_line_item) if current_line_item current_line_item.quantity += other_order_line_item.quantity handle_error(current_line_item) unless current_line_item.save else other_order_line_item.order_id = order.id handle_error(other_order_line_item) unless other_order_line_item.save end end |
#merge!(other_order, user = nil) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'app/models/spree/order_merger.rb', line 10 def merge!(other_order, user = nil) other_order.line_items.each do |other_order_line_item| next unless other_order_line_item.currency == order.currency current_line_item = find_matching_line_item(other_order_line_item) handle_merge(current_line_item, other_order_line_item) end set_user(user) persist_merge # So that the destroy doesn't take out line items which may have been re-assigned other_order.line_items.reload other_order.destroy end |
#persist_merge ⇒ Object
59 60 61 62 63 |
# File 'app/models/spree/order_merger.rb', line 59 def persist_merge updater.update_item_count updater.update_item_total updater.persist_totals end |
#set_user(user = nil) ⇒ Object
38 39 40 |
# File 'app/models/spree/order_merger.rb', line 38 def set_user(user = nil) order.associate_user!(user) if !order.user && !user.blank? end |