Class: Spree::Zone
- Inherits:
-
Base
- Object
- ActiveRecord::Base
- Base
- Spree::Zone
show all
- Defined in:
- app/models/spree/zone.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Base
page
#clear_preferences, #default_preferences, #defined_preferences, #get_preference, #has_preference!, #has_preference?, #preference_default, #preference_type, #set_preference
Class Method Details
.default_tax ⇒ Object
18
19
20
|
# File 'app/models/spree/zone.rb', line 18
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.
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
# File 'app/models/spree/zone.rb', line 47
def self.match(address)
return unless address &&
matches = includes(:zone_members).
order('spree_zones.zone_members_count', 'spree_zones.created_at').
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
|
.potential_matching_zones(zone) ⇒ Object
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
# File 'app/models/spree/zone.rb', line 22
def self.potential_matching_zones(zone)
if zone.country?
joins(countries: :zones).
where('zone_members_spree_countries_join.zone_id = ? OR ' +
'spree_zones.default_tax = ?', zone.id, true).
uniq
else
joins(:zone_members).where(
"(spree_zone_members.zoneable_type = 'Spree::State' AND
spree_zone_members.zoneable_id IN (?))
OR (spree_zone_members.zoneable_type = 'Spree::Country' AND
spree_zone_members.zoneable_id IN (?))
OR default_tax = ?",
zone.state_ids,
zone.states.pluck(:country_id),
true
).uniq
end
end
|
Instance Method Details
#<=>(other) ⇒ Object
110
111
112
|
# File 'app/models/spree/zone.rb', line 110
def <=>(other)
name <=> other.name
end
|
#contains?(target) ⇒ Boolean
Indicates whether the specified zone falls entirely within the zone performing the check.
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
# File 'app/models/spree/zone.rb', line 146
def contains?(target)
return false if state? && target.country?
return false if zone_members.empty? || target.zone_members.empty?
if kind == target.kind
if state?
return false if (target.states.pluck(:id) - states.pluck(:id)).present?
elsif country?
return false if (target.countries.pluck(:id) - countries.pluck(:id)).present?
end
else
return false if (target.states.pluck(:country_id) - countries.pluck(:id)).present?
end
true
end
|
#country? ⇒ Boolean
75
76
77
|
# File 'app/models/spree/zone.rb', line 75
def country?
kind == 'country'
end
|
#country_ids ⇒ Object
120
121
122
123
124
125
126
|
# File 'app/models/spree/zone.rb', line 120
def country_ids
if kind == 'country'
members.pluck(:zoneable_id)
else
[]
end
end
|
#country_ids=(ids) ⇒ Object
136
137
138
|
# File 'app/models/spree/zone.rb', line 136
def country_ids=(ids)
set_zone_members(ids, 'Spree::Country')
end
|
#country_list ⇒ Object
convenience method for returning the countries contained within a zone
99
100
101
102
103
104
105
106
107
108
|
# File 'app/models/spree/zone.rb', line 99
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
# File 'app/models/spree/zone.rb', line 83
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
|
#kind ⇒ Object
65
66
67
68
69
70
71
72
73
|
# File 'app/models/spree/zone.rb', line 65
def kind
if kind?
super
else
not_nil_scope = members.where.not(zoneable_type: nil)
zone_type = not_nil_scope.order('created_at ASC').pluck(:zoneable_type).last
zone_type.demodulize.underscore if zone_type
end
end
|
#state? ⇒ Boolean
79
80
81
|
# File 'app/models/spree/zone.rb', line 79
def state?
kind == 'state'
end
|
#state_ids ⇒ Object
128
129
130
131
132
133
134
|
# File 'app/models/spree/zone.rb', line 128
def state_ids
if kind == 'state'
members.pluck(:zoneable_id)
else
[]
end
end
|
#state_ids=(ids) ⇒ Object
140
141
142
|
# File 'app/models/spree/zone.rb', line 140
def state_ids=(ids)
set_zone_members(ids, 'Spree::State')
end
|
#zoneables ⇒ Object
All zoneables belonging to the zone members. Will be a collection of either countries or states depending on the zone type.
116
117
118
|
# File 'app/models/spree/zone.rb', line 116
def zoneables
members.includes(:zoneable).collect(&:zoneable)
end
|