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



91
92
93
# File 'app/models/spree/zone.rb', line 91

def self.default_tax
  Zone.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.



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

def self.match(address)
  return unless matches = self.order("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



81
82
83
# File 'app/models/spree/zone.rb', line 81

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

#contains?(target) ⇒ Boolean

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

Returns:

  • (Boolean)


97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'app/models/spree/zone.rb', line 97

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

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

#country_listObject

convenience method for returning the countries contained within a zone



68
69
70
71
72
73
74
75
76
77
78
79
# File 'app/models/spree/zone.rb', line 68

def country_list
  members.map { |zone_member|
    case zone_member.zoneable_type
    when 'Spree::Country'
      zone_member.zoneable
    when 'Spree::State'
      zone_member.zoneable.country
    else
      nil
    end
  }.flatten.compact.uniq
end

#in_zone?(address) ⇒ Boolean

TODO: Remove this method after 1.0 alias to the new include? method

Returns:

  • (Boolean)


33
34
35
36
# File 'app/models/spree/zone.rb', line 33

def in_zone?(address)
  ActiveSupport::Deprecation.warn '#in_zone? is deprecated and will be removed in Spree > 1.0. Use #include? instead.'
  include?(address)
end

#include?(address) ⇒ Boolean

Returns:

  • (Boolean)


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

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

attr_accessor :type



17
18
19
20
21
22
23
24
25
# File 'app/models/spree/zone.rb', line 17

def kind
  member = self.members.last

  case member && member.zoneable_type
  when 'Spree::State' then 'state'
  else
    'country'
  end
end

#kind=(value) ⇒ Object



27
28
29
# File 'app/models/spree/zone.rb', line 27

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

#zoneablesObject

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



87
88
89
# File 'app/models/spree/zone.rb', line 87

def zoneables
  members.collect { |m| m.zoneable }
end