Class: Spree::Zone
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Spree::Zone
- Defined in:
- app/models/spree/zone.rb
Class Method Summary collapse
- .default_tax ⇒ Object
-
.match(address) ⇒ Object
Returns the matching zone with the highest priority zone type (State, Country, Zone.) Returns nil in the case of no matches.
Instance Method Summary collapse
- #<=>(other) ⇒ Object
-
#contains?(target) ⇒ Boolean
Indicates whether the specified zone falls entirely within the zone performing the check.
- #country_ids ⇒ Object
- #country_ids=(ids) ⇒ Object
-
#country_list ⇒ Object
convenience method for returning the countries contained within a zone.
- #include?(address) ⇒ Boolean
- #kind ⇒ Object
- #kind=(value) ⇒ Object
- #state_ids ⇒ Object
- #state_ids=(ids) ⇒ Object
-
#zoneables ⇒ Object
All zoneables belonging to the zone members.
Class Method Details
.default_tax ⇒ Object
14 15 16 |
# File 'app/models/spree/zone.rb', line 14 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.
20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'app/models/spree/zone.rb', line 20 def self.match(address) return unless matches = self.includes(:zone_members). order('zone_members_count', 'created_at'). select { |zone| zone.include? address } ['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
67 68 69 |
# File 'app/models/spree/zone.rb', line 67 def <=>(other) name <=> other.name end |
#contains?(target) ⇒ Boolean
Indicates whether the specified zone falls entirely within the zone performing the check.
115 116 117 118 119 120 121 122 123 124 125 |
# File 'app/models/spree/zone.rb', line 115 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_ids ⇒ Object
77 78 79 80 81 82 83 |
# File 'app/models/spree/zone.rb', line 77 def country_ids if kind == 'country' members.pluck(:zoneable_id) else [] end end |
#country_ids=(ids) ⇒ Object
93 94 95 96 97 98 99 100 101 |
# File 'app/models/spree/zone.rb', line 93 def country_ids=(ids) zone_members.destroy_all ids.reject{ |id| id.blank? }.map do |id| member = ZoneMember.new member.zoneable_type = 'Spree::Country' member.zoneable_id = id members << member end end |
#country_list ⇒ Object
convenience method for returning the countries contained within a zone
59 60 61 62 63 64 65 |
# File 'app/models/spree/zone.rb', line 59 def country_list @countries ||= case kind when 'country' then zoneables when 'state' then zoneables.collect(&:country) else nil end.flatten.compact.uniq end |
#include?(address) ⇒ Boolean
43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'app/models/spree/zone.rb', line 43 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
33 34 35 36 37 |
# File 'app/models/spree/zone.rb', line 33 def kind if members.any? && !members.any? { |member| member.try(:zoneable_type).nil? } members.last.zoneable_type.demodulize.underscore end end |
#kind=(value) ⇒ Object
39 40 41 |
# File 'app/models/spree/zone.rb', line 39 def kind=(value) # do nothing - just here to satisfy the form end |
#state_ids ⇒ Object
85 86 87 88 89 90 91 |
# File 'app/models/spree/zone.rb', line 85 def state_ids if kind == 'state' members.pluck(:zoneable_id) else [] end end |
#state_ids=(ids) ⇒ Object
103 104 105 106 107 108 109 110 111 |
# File 'app/models/spree/zone.rb', line 103 def state_ids=(ids) zone_members.destroy_all ids.reject{ |id| id.blank? }.map do |id| member = ZoneMember.new member.zoneable_type = 'Spree::State' member.zoneable_id = id members << member end end |
#zoneables ⇒ Object
All zoneables belonging to the zone members. Will be a collection of either countries or states depending on the zone type.
73 74 75 |
# File 'app/models/spree/zone.rb', line 73 def zoneables members.includes(:zoneable).collect(&:zoneable) end |