Module: Models::PartyCart

Extended by:
ActiveSupport::Concern
Included in:
Party
Defined in:
app/concerns/models/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_uploadsActiveRecord::Relation<Upload>

All uploads belonging to the party, newest first.

Returns:

  • (ActiveRecord::Relation<Upload>)

    uploads ordered by creation date descending



72
73
74
# File 'app/concerns/models/party_cart.rb', line 72

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

#assign_chained_activities?Boolean

Returns whether chained activities should be assigned; always true for parties.

Returns:

  • (Boolean)

    whether chained activities should be assigned; always true for parties



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

def assign_chained_activities?
  true
end

#cartOrder

Finds or creates the party's cart order.

Returns:

  • (Order)

    the party's cart



44
45
46
# File 'app/concerns/models/party_cart.rb', line 44

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

#cart=(new_cart) ⇒ Order

Replaces the party's carts with the given one, destroying any others.

Parameters:

  • new_cart (Order)

    cart order to attach to the party

Returns:

  • (Order)

    the attached cart order



51
52
53
54
55
56
57
# File 'app/concerns/models/party_cart.rb', line 51

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 whether the party has a cart order.

Returns:

  • (Boolean)

    whether the party has a cart order



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

def cart?
  load_cart.present?
end

#cart_idInteger?

Returns id of the party's most recent cart, if any.

Returns:

  • (Integer, nil)

    id of the party's most recent cart, if any



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

def cart_id
  load_cart&.id
end

#cart_infoHash{Symbol => Object}?

Summary info for the party's most recent cart, if one exists.

Returns:

  • (Hash{Symbol => Object}, nil)

    cart id, total quantity, and total; nil without a cart



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

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) ⇒ PartyActivity

Creates an activity for the party.
Keyword options (:skip_if_exists, :description, :notes, :resource,
:assigned_resource) are forwarded to PartyActivityCreator.

Parameters:

  • task_type (String)

    type of task to create

Returns:

  • (PartyActivity)

    the created activity



89
90
91
# File 'app/concerns/models/party_cart.rb', line 89

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

#create_quote_builder_projectQuoteBuilder::Project

Creates a quote-builder project for the party.

Returns:

  • (QuoteBuilder::Project)

    the created project



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

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

#load_cartOrder?

The party's most recently created cart order.

Returns:

  • (Order, nil)

    latest cart order; nil when the party has no orders



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

def load_cart
  return unless respond_to?(:orders)

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

#store_original_sourceObject?

Backfills original_source from source when it is missing or unknown.

Returns:

  • (Object, nil)

    the assigned source, or nil when no assignment was needed



78
79
80
81
82
# File 'app/concerns/models/party_cart.rb', line 78

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

  self.original_source = source
end