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
46
47
48
|
# File 'app/models/spree/zone.rb', line 46
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.
53
54
55
56
57
58
59
60
61
62
63
64
65
|
# File 'app/models/spree/zone.rb', line 53
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.
71
72
73
74
75
76
77
78
79
80
|
# File 'app/models/spree/zone.rb', line 71
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
116
117
118
|
# File 'app/models/spree/zone.rb', line 116
def <=>(other)
name <=> other.name
end
|
#contains?(target) ⇒ Boolean
Indicates whether the specified zone falls entirely within the zone performing the check.
152
153
154
155
156
157
158
159
160
161
162
|
# File 'app/models/spree/zone.rb', line 152
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
126
127
128
129
130
131
132
|
# File 'app/models/spree/zone.rb', line 126
def country_ids
if kind == 'country'
members.pluck(:zoneable_id)
else
[]
end
end
|
#country_ids=(ids) ⇒ Object
142
143
144
|
# File 'app/models/spree/zone.rb', line 142
def country_ids=(ids)
set_zone_members(ids, 'Spree::Country')
end
|
#country_list ⇒ Object
convenience method for returning the countries contained within a zone
108
109
110
111
112
113
114
|
# File 'app/models/spree/zone.rb', line 108
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
# File 'app/models/spree/zone.rb', line 92
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
82
83
84
85
86
|
# File 'app/models/spree/zone.rb', line 82
def kind
if members.any? && !members.any? { |member| member.try(:zoneable_type).nil? }
members.last.zoneable_type.demodulize.underscore
end
end
|
#kind=(value) ⇒ Object
88
89
90
|
# File 'app/models/spree/zone.rb', line 88
def kind=(value)
end
|
#state_ids ⇒ Object
134
135
136
137
138
139
140
|
# File 'app/models/spree/zone.rb', line 134
def state_ids
if kind == 'state'
members.pluck(:zoneable_id)
else
[]
end
end
|
#state_ids=(ids) ⇒ Object
146
147
148
|
# File 'app/models/spree/zone.rb', line 146
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.
122
123
124
|
# File 'app/models/spree/zone.rb', line 122
def zoneables
members.includes(:zoneable).collect(&:zoneable)
end
|