Class: Workarea::Checkout::ShippingOptions

Inherits:
Object
  • Object
show all
Defined in:
app/models/workarea/checkout/shipping_options.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(order, shipping) ⇒ ShippingOptions

Returns a new instance of ShippingOptions.



12
13
14
15
# File 'app/models/workarea/checkout/shipping_options.rb', line 12

def initialize(order, shipping)
  @order = order
  @shipping = shipping
end

Instance Attribute Details

#orderObject (readonly)

Returns the value of attribute order.



10
11
12
# File 'app/models/workarea/checkout/shipping_options.rb', line 10

def order
  @order
end

#shippingObject (readonly)

Returns the value of attribute shipping.



10
11
12
# File 'app/models/workarea/checkout/shipping_options.rb', line 10

def shipping
  @shipping
end

Instance Method Details

#availableArray<ShippingOption>

All available and valid shipping services for this order. Includes the discounts on the shipping options.

Returns:



22
23
24
25
26
# File 'app/models/workarea/checkout/shipping_options.rb', line 22

def available
  @available ||= all_options.each do |option|
    option.price_adjustments = price_adjustments[option.name] || []
  end
end

#find_valid(name) ⇒ ShippingOption?

Find a valid shipping option against this shipping service. Defaults to the first to ensure no blank shipping service ends up set on an order. Will return nil if no available and valid shipping option.

Returns:



35
36
37
# File 'app/models/workarea/checkout/shipping_options.rb', line 35

def find_valid(name)
  available.detect { |o| o.name == name } || available.first
end

#valid?Boolean

Checks whether the currently selected shipping service and its pricing are valid. Used when placing order to determine if the current state is good to go.

Sets a validation message on the shipping when the result is false so the user is told.

Returns:

  • (Boolean)


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/models/workarea/checkout/shipping_options.rb', line 48

def valid?
  return false if shipping.blank? || shipping.shipping_service.blank?

  current_shipping_option = all_options.detect do |shipping_option|
    shipping_option.carrier == shipping.shipping_service.carrier &&
      shipping_option.name == shipping.shipping_service.name &&
      shipping_option.service_code == shipping.shipping_service.service_code
  end

  result = shipping.shipping_service.present? &&
            current_shipping_option.present? &&
            current_shipping_option.base_price == shipping.base_price

  unless result
    shipping.errors.add(
      :shipping_service,
      I18n.t('workarea.errors.messages.must_be_updated')
    )
  end

  result
end