app/models/zone.rb in spree-0.7.1 vs app/models/zone.rb in spree-0.8.0
- old
+ new
@@ -1,38 +1,43 @@
class Zone < ActiveRecord::Base
- has_many_polymorphs :members, :from => [:states, :countries, :zones], :through => :zone_members, :as => :parent
+ has_many :zone_members
+ validates_presence_of :name
validates_uniqueness_of :name
+ after_save :remove_defunct_members
+ alias :members :zone_members
+ accepts_nested_attributes_for :zone_members, :allow_destroy => true, :reject_if => proc { |a| a['zoneable_id'].blank? }
+
#attr_accessor :type
- def type
+ def kind
return "country" unless member = self.members.last
- return "state" if member.class == State
- return "zone" if member.class == Zone
+ return "state" if member.zoneable_type == "State"
+ return "zone" if member.zoneable_type == "Zone"
"country"
- end
+ end
- # virtual attributes for use with AJAX completion stuff
- def member_name
- # does nothing - just here to satisfy text_field_with_auto_complete (which requires a model property)
+ def kind=(value)
+ # do nothing - just here to satisfy the form
end
# alias to the new include? method
def in_zone?(address)
$stderr.puts "Warning: calling deprecated method :in_zone? use :include? instead."
include?(address)
end
- def include?(address)
+ def include?(address)
# NOTE: This is complicated by the fact that include? for HMP is broken in Rails 2.1 (so we use awkward index method)
- case self.type
+ case self.kind
when "country"
- return members.index(address.country).respond_to?(:integer?)
+ return members.select { |zone_member| zone_member.zoneable == address.country }.any?
when "state"
- return members.index(address.state).respond_to?(:integer?)
+ return members.select { |zone_member| zone_member.zoneable == address.state }.any?
+ #members.index(address.state).respond_to?(:integer?)
end
- members.each do |zone|
- return true if zone.include?(address)
+ members.each do |zone_member|
+ return true if zone_member.zoneable.include?(address)
end
false
end
def self.match(address)
@@ -42,10 +47,21 @@
end
# convenience method for returning the countries contained within a zone (different then the countries method which only
# returns the zones children and does not consider the grand children if the children themselves are zones)
def country_list
- return [] if type == "state"
- return countries if type == "country"
- members.collect { |zone| zone.country_list }.flatten
+ return [] if kind == "state"
+ return members.collect { |zone_member| zone_member.zoneable } if kind == "country"
+ members.collect { |zone_member| zone_member.zoneable.country_list }.flatten
end
-end
\ No newline at end of file
+
+ def <=>(other)
+ name <=> other.name
+ end
+
+ private
+ def remove_defunct_members
+ zone_members.each do |zone_member|
+ zone_member.destroy if zone_member.zoneable_id.nil?
+ end
+ end
+end