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
display_includes, #initialize_preference_defaults, page, preference
#default_preferences, #defined_preferences, #get_preference, #has_preference!, #has_preference?, #preference_default, #preference_type, #set_preference
Class Method Details
.default_tax ⇒ Object
44
45
46
|
# File 'app/models/spree/zone.rb', line 44
def self.default_tax
where(default_tax: true).first
end
|
.match(address) ⇒ Object
Returns the most specific matching zone for an address. Specific means: A State zone wins over a country zone, and a zone with few members wins over one with many members. If there is no match, returns nil.
51
52
53
54
55
56
57
58
59
60
61
62
63
|
# File 'app/models/spree/zone.rb', line 51
def self.match(address)
return unless address && (matches =
with_member_ids(address.state_id, address.country_id).
order(:zone_members_count, :created_at, :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
|
.with_shared_members(zone) ⇒ Object
Returns all zones that contain any of the zone members of the zone passed in. This also includes any country zones that contain the state of the current zone, if it’s a state zone. If the passed-in zone has members, it will also be in the result set.
69
70
71
72
73
74
75
76
77
78
|
# File 'app/models/spree/zone.rb', line 69
def self.with_shared_members(zone)
return none unless zone
states_and_state_country_ids = zone.states.pluck(:id, :country_id).to_a
state_ids = states_and_state_country_ids.map(&:first)
state_country_ids = states_and_state_country_ids.map(&:second)
country_ids = zone.countries.pluck(:id).to_a
with_member_ids(state_ids, country_ids + state_country_ids).distinct
end
|
Instance Method Details
#<=>(other) ⇒ Object
114
115
116
|
# File 'app/models/spree/zone.rb', line 114
def <=>(other)
name <=> other.name
end
|
#contains?(target) ⇒ Boolean
Indicates whether the specified zone falls entirely within the zone performing the check.
150
151
152
153
154
155
156
157
158
159
160
|
# File 'app/models/spree/zone.rb', line 150
def contains?(target)
return true if self == 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.collect(&:id) - zoneables.collect(&:id)).empty?
else
(target.zoneables.collect(&:country).collect(&:id) - zoneables.collect(&:id)).empty?
end
end
|
#country_ids ⇒ Object
124
125
126
127
128
129
130
|
# File 'app/models/spree/zone.rb', line 124
def country_ids
if kind == 'country'
members.pluck(:zoneable_id)
else
[]
end
end
|
#country_ids=(ids) ⇒ Object
140
141
142
|
# File 'app/models/spree/zone.rb', line 140
def country_ids=(ids)
set_zone_members(ids, 'Spree::Country')
end
|
#country_list ⇒ Object
convenience method for returning the countries contained within a zone
106
107
108
109
110
111
112
|
# File 'app/models/spree/zone.rb', line 106
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
# File 'app/models/spree/zone.rb', line 90
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
80
81
82
83
84
|
# File 'app/models/spree/zone.rb', line 80
def kind
if members.any? && !members.any? { |member| member.try(:zoneable_type).nil? }
members.last.zoneable_type.demodulize.underscore
end
end
|
#kind=(value) ⇒ Object
86
87
88
|
# File 'app/models/spree/zone.rb', line 86
def kind=(value)
end
|
#state_ids ⇒ Object
132
133
134
135
136
137
138
|
# File 'app/models/spree/zone.rb', line 132
def state_ids
if kind == 'state'
members.pluck(:zoneable_id)
else
[]
end
end
|
#state_ids=(ids) ⇒ Object
144
145
146
|
# File 'app/models/spree/zone.rb', line 144
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.
120
121
122
|
# File 'app/models/spree/zone.rb', line 120
def zoneables
members.includes(:zoneable).collect(&:zoneable)
end
|