Class: HhAddOn::ReservationAddOns::SelectedAddOnsBuilder

Inherits:
Object
  • Object
show all
Defined in:
app/my_lib/hh_add_on/reservation_add_ons/selected_add_ons_builder.rb

Overview

HhAddOn::ReservationAddOns::SelectedAddOnsBuilder is responsible for building a list of selected add_ons based on the provided add_ons and pricing rules.

This builder class calculates base prices, applies mix-and-match quantity rules, handles pricing for adults and kids, and optionally applies promotional pricing

Examples:

Initialize and build selected add_ons

builder = HhAddOn::ReservationAddOns::SelectedAddOnsBuilder.new(
  add_ons: add_ons,
  restaurant_add_on_id: 1,
  adult: 2,
  kids: 1,
  price_cents: 1000,
  date: Date.today
  use_dynamic_pricing: true,
  custom_price_cents: 1500
)
builder.build

Returns:

  • (Array<Hash>)

    A list of selected add_ons with calculated prices

Instance Method Summary collapse

Constructor Details

#initialize(add_ons:, restaurant_add_on_id:, adult:, kids:, price_cents:) ⇒ SelectedAddOnsBuilder

Returns a new instance of SelectedAddOnsBuilder.



32
33
34
35
36
37
38
39
40
# File 'app/my_lib/hh_add_on/reservation_add_ons/selected_add_ons_builder.rb', line 32

def initialize(add_ons:, restaurant_add_on_id:, adult:, kids:, price_cents:)
  @add_ons = add_ons
  @restaurant_add_on_id = restaurant_add_on_id
  @adult = to_integer(adult)
  @kids = to_integer(kids)
  @price_cents = price_cents
  @price_finder = HhPackage::ReservationPackages::PriceFinder.new
  @selected_kids_price_add_on_id = nil
end

Instance Method Details

#buildArray<Hash>

Build the selected add_ons and calculate prices for adults and kids.

Returns:

  • (Array<Hash>)

    A list of selected add_ons with calculated prices



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/my_lib/hh_add_on/reservation_add_ons/selected_add_ons_builder.rb', line 45

def build
  selected_add_on = find_selected_add_on
  return [] if selected_add_on.blank?

  add_on = selected_add_on[:add_on]
  binding
  base_price = add_on.pricing.price_cents
  selected_qty = selected_add_on[:quantity]

  kids_price_data = find_kids_price_if_applicable(add_on)
  adult_price = calculate_adult_price(base_price, selected_qty)
  kids_price = calculate_kids_price(kids_price_data[:price]).cents

  adult_price = ensure_valid_adult_price(adult_price, add_on)

  build_selected_add_ons_response(add_on, selected_qty, kids_price, kids_price_data, base_price, adult_price)
end