Class: Spree::Zone
- Defined in:
- app/models/spree/zone.rb
Class Method Summary collapse
-
.default_tax ⇒ Object
deprecated
Deprecated.
Please run the ‘solidus:migrations:create_vat_prices` rake task
-
.match(address) ⇒ Object
Returns the most specific matching zone for an address.
-
.with_shared_members(zone) ⇒ Object
Returns all zones that contain any of the zone members of the zone passed in.
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.
Methods inherited from Base
display_includes, #initialize_preference_defaults, page, preference
Methods included from Preferences::Preferable
#default_preferences, #defined_preferences, #get_preference, #has_preference!, #has_preference?, #preference_default, #preference_type, #set_preference
Class Method Details
.default_tax ⇒ Object
Please run the ‘solidus:migrations:create_vat_prices` rake task
Returns the zone marked as ‘default_tax`.
48 49 50 51 52 53 54 |
# File 'app/models/spree/zone.rb', line 48 def self.default_tax default_tax_zone = where(default_tax: true).first if default_tax_zone Spree::Deprecation.warn("Please run the `solidus:migrations:create_vat_prices` rake task.", caller) default_tax_zone end 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.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'app/models/spree/zone.rb', line 59 def self.match(address) Spree::Deprecation.warn("Spree::Zone.match is deprecated. Please use Spree::Zone.for_address instead.", caller) 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.
79 80 81 82 83 84 85 86 87 88 |
# File 'app/models/spree/zone.rb', line 79 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
124 125 126 |
# File 'app/models/spree/zone.rb', line 124 def <=>(other) name <=> other.name end |
#contains?(target) ⇒ Boolean
Indicates whether the specified zone falls entirely within the zone performing the check.
160 161 162 163 164 165 166 167 168 169 170 |
# File 'app/models/spree/zone.rb', line 160 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
134 135 136 137 138 139 140 |
# File 'app/models/spree/zone.rb', line 134 def country_ids if kind == 'country' members.pluck(:zoneable_id) else [] end end |
#country_ids=(ids) ⇒ Object
150 151 152 |
# File 'app/models/spree/zone.rb', line 150 def country_ids=(ids) set_zone_members(ids, 'Spree::Country') end |
#country_list ⇒ Object
convenience method for returning the countries contained within a zone
116 117 118 119 120 121 122 |
# File 'app/models/spree/zone.rb', line 116 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
100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'app/models/spree/zone.rb', line 100 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
90 91 92 93 94 |
# File 'app/models/spree/zone.rb', line 90 def kind if members.any? && !members.any? { |member| member.try(:zoneable_type).nil? } members.last.zoneable_type.demodulize.underscore end end |
#kind=(value) ⇒ Object
96 97 98 |
# File 'app/models/spree/zone.rb', line 96 def kind=(value) # do nothing - just here to satisfy the form end |
#state_ids ⇒ Object
142 143 144 145 146 147 148 |
# File 'app/models/spree/zone.rb', line 142 def state_ids if kind == 'state' members.pluck(:zoneable_id) else [] end end |
#state_ids=(ids) ⇒ Object
154 155 156 |
# File 'app/models/spree/zone.rb', line 154 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.
130 131 132 |
# File 'app/models/spree/zone.rb', line 130 def zoneables members.includes(:zoneable).collect(&:zoneable) end |