-
Notifications
You must be signed in to change notification settings - Fork 9
/
zip_code_zone_extension.rb
63 lines (52 loc) · 1.66 KB
/
zip_code_zone_extension.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# Uncomment this if you reference any of your controllers in activate
# require_dependency 'application'
class ZipCodeZoneExtension < Spree::Extension
version "1.0"
description "Zones for U.S. ZIP Codes"
url "http://github.com/wapcaplet/spree-zip-code-zone"
def activate
# This is mostly copied from the Spree 0.10.2 Zone methods,
# with added support for ZipCodeRange zone members
Zone.class_eval do
def kind
member = self.members.last
case member && member.zoneable_type
when "State" then "state"
when "Zone" then "zone"
when "ZipCodeRange" then "zip_code_range"
else
"country"
end
end
# Check for whether an address.zipcode is
# between the start/end of a ZipCodeRange
def include?(address)
return false unless address
members.any? do |zone_member|
case zone_member.zoneable_type
when "Zone"
zone_member.zoneable.include?(address)
when "Country"
zone_member.zoneable == address.country
when "State"
zone_member.zoneable == address.state
when "ZipCodeRange"
address.zipcode and address.zipcode.between?(
zone_member.zoneable.start_zip,
zone_member.zoneable.end_zip)
else
false
end
end
end
end # Zone
Admin::ZonesController.class_eval do
def load_data
@countries = Country.all.sort
@states = State.all.sort
@zip_code_ranges = ZipCodeRange.all.sort
@zones = Zone.all.sort
end
end # Admin::ZonesController
end
end