Class: Spree::Zone

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.default_taxObject



77
78
79
# File 'app/models/spree/zone.rb', line 77

def self.default_tax
  where(:default_tax => true).first
end

.match(address) ⇒ Object

Returns the matching zone with the highest priority zone type (State, Country, Zone.) Returns nil in the case of no matches.



43
44
45
46
47
48
49
50
51
52
# File 'app/models/spree/zone.rb', line 43

def self.match(address)
  return unless matches = self.includes(:zone_members).order('zone_members_count', 'created_at').select { |zone| zone.include? address }

  ['state', 'country'].each do |zone_kind|
    if match = matches.detect { |zone| zone_kind == zone.kind }
      return match
    end
  end
  matches.first
end

Instance Method Details

#<=>(other) ⇒ Object



67
68
69
# File 'app/models/spree/zone.rb', line 67

def <=>(other)
  name <=> other.name
end

#contains?(target) ⇒ Boolean

Indicates whether the specified zone falls entirely within the zone performing the check.

Returns:

  • (Boolean)


83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/models/spree/zone.rb', line 83

def contains?(target)
  return false if kind == 'state' && target.kind == 'country'
  return false if zone_members.empty? || target.zone_members.empty?

  if kind == target.kind
    target.zoneables.each do |target_zoneable|
      return false unless zoneables.include?(target_zoneable)
    end
  else
    target.zoneables.each do |target_state|
      return false unless zoneables.include?(target_state.country)
    end
  end
  true
end

#country_listObject

convenience method for returning the countries contained within a zone



55
56
57
58
59
60
61
62
63
64
65
# File 'app/models/spree/zone.rb', line 55

def country_list
  @countries ||=
  case kind
  when 'country'
    zoneables
  when 'state'
    zoneables.collect(&:country)
  else
    nil
  end.flatten.compact.uniq
end

#include?(address) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/models/spree/zone.rb', line 25

def include?(address)
  return false unless address

  # NOTE: This is complicated by the fact that include? for HMP is broken in Rails 2.1 (so we use awkward index method)
  members.any? do |zone_member|
    case zone_member.zoneable_type
    when 'Spree::Country'
      zone_member.zoneable_id == address.country_id
    when 'Spree::State'
      zone_member.zoneable_id == address.state_id
    else
      false
    end
  end
end

#kindObject



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

def kind
  return nil if members.empty? || members.any? { |member| member.try(:zoneable_type).nil? }
  members.last.zoneable_type.demodulize.downcase
end

#kind=(value) ⇒ Object



21
22
23
# File 'app/models/spree/zone.rb', line 21

def kind=(value)
  # do nothing - just here to satisfy the form
end

#zoneablesObject

All zoneables belonging to the zone members. Will be a collection of either countries or states depending on the zone type.



73
74
75
# File 'app/models/spree/zone.rb', line 73

def zoneables
  members.collect(&:zoneable)
end