Class: Spree::Zone

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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

page

Methods included from Preferences::Preferable

#default_preferences, #defined_preferences, #get_preference, #has_preference!, #has_preference?, #preference_default, #preference_type, #set_preference

Class Method Details

.default_taxObject



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

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.



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/models/spree/zone.rb', line 22

def self.match(address)
  return unless address and matches = self.includes(:zone_members).
    order('spree_zones.zone_members_count', 'spree_zones.created_at', 'spree_zones.id').
    where("(spree_zone_members.zoneable_type = 'Spree::Country' AND spree_zone_members.zoneable_id = ?) OR (spree_zone_members.zoneable_type = 'Spree::State' AND spree_zone_members.zoneable_id = ?)", address.country_id, address.state_id).
    references(:zones)

  ['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



70
71
72
# File 'app/models/spree/zone.rb', line 70

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

#contains?(target) ⇒ Boolean

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

Returns:

  • (Boolean)


106
107
108
109
110
111
112
113
114
115
116
# File 'app/models/spree/zone.rb', line 106

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
    return false if (target.zoneables.collect(&:id) - zoneables.collect(&:id)).present?
  else
    return false if (target.zoneables.collect(&:country).collect(&:id) - zoneables.collect(&:id)).present?
  end
  true
end

#country_idsObject



80
81
82
83
84
85
86
# File 'app/models/spree/zone.rb', line 80

def country_ids
  if kind == 'country'
    members.pluck(:zoneable_id)
  else
    []
  end
end

#country_ids=(ids) ⇒ Object



96
97
98
# File 'app/models/spree/zone.rb', line 96

def country_ids=(ids)
  set_zone_members(ids, 'Spree::Country')
end

#country_listObject

convenience method for returning the countries contained within a zone



62
63
64
65
66
67
68
# File 'app/models/spree/zone.rb', line 62

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

#include?(address) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/models/spree/zone.rb', line 46

def include?(address)
  return false unless address

  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



36
37
38
39
40
# File 'app/models/spree/zone.rb', line 36

def kind
  if members.any? && !members.any? { |member| member.try(:zoneable_type).nil? }
    members.last.zoneable_type.demodulize.underscore
  end
end

#kind=(value) ⇒ Object



42
43
44
# File 'app/models/spree/zone.rb', line 42

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

#state_idsObject



88
89
90
91
92
93
94
# File 'app/models/spree/zone.rb', line 88

def state_ids
  if kind == 'state'
    members.pluck(:zoneable_id)
  else
    []
  end
end

#state_ids=(ids) ⇒ Object



100
101
102
# File 'app/models/spree/zone.rb', line 100

def state_ids=(ids)
  set_zone_members(ids, 'Spree::State')
end

#zoneablesObject

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



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

def zoneables
  members.includes(:zoneable).collect(&:zoneable)
end