Class: Spree::TemandoQuote

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/spree/temando_quote.rb

Overview

TemandoQuote object is a representation of the selected Quote provided by the Temando API.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.new_or_update_from_quote(calculator, object, quote, address) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/models/spree/temando_quote.rb', line 29

def self.new_or_update_from_quote(calculator, object, quote, address)
  q = object.temando_quotes.find_by_calculator_id(calculator.id) || object.temando_quotes.new
  q.calculator = calculator

  if object.is_a?(Spree::Order) then
    q.order = object
  else
    q.shipment = object
  end

  [ :total_price, :tax, :currency, :minimum_eta, :maximum_eta, :name, :base_price, :guaranteed_eta, :carrier_id, :carrier_name, :carrier_phone, :delivery_method ].each do |field|
    q.send("#{field}=".to_sym, quote.send(field))
  end

  q.address = address
  q.cached_items_hash = q.current_items_hash

  q
end

Instance Method Details

#address_hashObject



20
21
22
# File 'app/models/spree/temando_quote.rb', line 20

def address_hash
  Digest::SHA1.hexdigest(self.address.attributes.to_s)
end

#calculator_hashObject



16
17
18
# File 'app/models/spree/temando_quote.rb', line 16

def calculator_hash
  Digest::SHA1.hexdigest(self.calculator.preferences.to_s)
end

#current_items_hashObject



24
25
26
27
# File 'app/models/spree/temando_quote.rb', line 24

def current_items_hash
  data = (shipment || order).line_items.order(:variant_id).collect { |li| [ li.variant_id, li.quantity ].join(',') }.join(';')
  Digest::SHA1.hexdigest(calculator_hash + address_hash + data)
end

#outdated?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'app/models/spree/temando_quote.rb', line 12

def outdated?
  self.current_items_hash != self.cached_items_hash
end