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.
Instance Method Summary collapse
-
#all_uploads ⇒ ActiveRecord::Relation<Upload>
All uploads belonging to the party, newest first.
-
#assign_chained_activities? ⇒ Boolean
Whether chained activities should be assigned; always true for parties.
-
#cart ⇒ Order
Finds or creates the party's cart order.
-
#cart=(new_cart) ⇒ Order
Replaces the party's carts with the given one, destroying any others.
-
#cart? ⇒ Boolean
(also: #has_cart?)
Whether the party has a cart order.
-
#cart_id ⇒ Integer?
Id of the party's most recent cart, if any.
-
#cart_info ⇒ Hash{Symbol => Object}?
Summary info for the party's most recent cart, if one exists.
-
#create_activity(task_type) ⇒ PartyActivity
Creates an activity for the party.
-
#create_quote_builder_project ⇒ QuoteBuilder::Project
Creates a quote-builder project for the party.
-
#load_cart ⇒ Order?
The party's most recently created cart order.
-
#store_original_source ⇒ Object?
Backfills original_source from source when it is missing or unknown.
Instance Method Details
#all_uploads ⇒ ActiveRecord::Relation<Upload>
All uploads belonging to the party, newest first.
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.
66 67 68 |
# File 'app/concerns/models/party_cart.rb', line 66 def assign_chained_activities? true end |
#cart ⇒ Order
Finds or creates the party's cart order.
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.
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.
37 38 39 |
# File 'app/concerns/models/party_cart.rb', line 37 def cart? load_cart.present? end |
#cart_id ⇒ Integer?
Returns 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_info ⇒ Hash{Symbol => Object}?
Summary info for the party's most recent cart, if one exists.
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.
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_project ⇒ QuoteBuilder::Project
Creates a quote-builder project for the party.
61 62 63 |
# File 'app/concerns/models/party_cart.rb', line 61 def create_quote_builder_project QuoteBuilder::ProjectCreator.call(self) end |
#load_cart ⇒ Order?
The party's most recently created cart order.
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_source ⇒ Object?
Backfills original_source from source when it is missing or unknown.
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 |