Class: Spree::MergeableOrdersFinder

Inherits:
Object
  • Object
show all
Defined in:
app/models/spree/mergeable_orders_finder.rb

Overview

Finds orders to merge when a user logs in.

Configurable via Spree::Config#mergeable_orders_finder_class. Default behavior finds all incomplete orders from the same store.

Examples:

Custom finder for recent orders only

class RecentOrdersFinder
  def initialize(context:)
    @user = context.spree_current_user
    @store = context.current_store
    @current_order = context.current_order
  end

  def call
    @user.orders.by_store(@store).incomplete
         .where.not(id: @current_order.id)
         .where('created_at > ?', 7.days.ago)
  end
end

Spree::Config.mergeable_orders_finder_class = RecentOrdersFinder

Instance Method Summary collapse

Constructor Details

#initialize(context:) ⇒ MergeableOrdersFinder

Returns a new instance of MergeableOrdersFinder.

Parameters:

  • context (Object)

    an object that responds to spree_current_user, current_store, and current_order (typically a controller)



28
29
30
31
32
# File 'app/models/spree/mergeable_orders_finder.rb', line 28

def initialize(context:)
  @user = context.spree_current_user
  @store = context.current_store
  @current_order = context.current_order
end

Instance Method Details

#callActiveRecord::Relation<Spree::Order>

Returns orders that should be merged into the current order

same store

Returns:

  • (ActiveRecord::Relation<Spree::Order>)

    incomplete orders from the



38
39
40
41
42
# File 'app/models/spree/mergeable_orders_finder.rb', line 38

def call
  return Spree::Order.none unless @user && @current_order

  @user.orders.by_store(@store).incomplete.where(frontend_viewable: true).where.not(id: @current_order.id)
end