Module: PartyCart

Extended by:
ActiveSupport::Concern
Included in:
Party
Defined in:
app/models/concerns/party_cart.rb

Overview

Shopping cart access and quote-builder project creation for parties with orders.
Heavy-lift logic lives in service objects; this concern stays as the public API.

See Also:

Instance Method Summary collapse

Instance Method Details

#all_uploadsObject



56
57
58
# File 'app/models/concerns/party_cart.rb', line 56

def all_uploads
  uploads.order(Upload[:created_at].desc)
end

#assign_chained_activities?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'app/models/concerns/party_cart.rb', line 52

def assign_chained_activities?
  true
end

#cartObject



36
37
38
# File 'app/models/concerns/party_cart.rb', line 36

def cart
  Cart::FindOrCreateForParty.call(self)
end

#cart=(new_cart) ⇒ Object



40
41
42
43
44
45
46
# File 'app/models/concerns/party_cart.rb', line 40

def cart=(new_cart)
  orders.carts.where.not(id: new_cart.id.to_i).destroy_all
  new_cart.state = 'cart' # to be sure
  new_cart.contact_id = id if is_a?(Contact)
  new_cart.currency = store.currency
  orders << new_cart
end

#cart?Boolean Also known as: has_cart?

Returns:

  • (Boolean)


31
32
33
# File 'app/models/concerns/party_cart.rb', line 31

def cart?
  load_cart.present?
end

#cart_idObject



27
28
29
# File 'app/models/concerns/party_cart.rb', line 27

def cart_id
  load_cart&.id
end

#cart_infoObject



10
11
12
13
14
15
16
17
18
19
# File 'app/models/concerns/party_cart.rb', line 10

def cart_info
  return unless cart?

  c = load_cart
  {
    id: c.id,
    quantities: c.line_items.parents_only.non_shipping.sum(:quantity),
    total: c.total
  }
end

#create_activity(task_type, **options) ⇒ Object



66
67
68
# File 'app/models/concerns/party_cart.rb', line 66

def create_activity(task_type, **options)
  PartyActivityCreator.call(self, task_type, **options)
end

#create_quote_builder_projectObject



48
49
50
# File 'app/models/concerns/party_cart.rb', line 48

def create_quote_builder_project
  QuoteBuilder::ProjectCreator.call(self)
end

#load_cartObject



21
22
23
24
25
# File 'app/models/concerns/party_cart.rb', line 21

def load_cart
  return unless respond_to?(:orders)

  orders.carts.order(created_at: :desc).first
end

#store_original_sourceObject



60
61
62
63
64
# File 'app/models/concerns/party_cart.rb', line 60

def store_original_source
  return unless original_source.nil? || original_source.unknown_source?

  self.original_source = source
end