Class: Spree::Zone

Inherits:
Base
  • Object
show all
Defined in:
app/models/spree/zone.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

display_includes

Methods included from Core::Permalinks

#generate_permalink, #save_permalink

Class Method Details

.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.


51
52
53
54
55
56
57
58
59
60
# File 'app/models/spree/zone.rb', line 51

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


96
97
98
# File 'app/models/spree/zone.rb', line 96

def <=>(other)
  name <=> other.name
end

#country_idsObject


106
107
108
109
110
111
112
# File 'app/models/spree/zone.rb', line 106

def country_ids
  if kind == 'country'
    members.pluck(:zoneable_id)
  else
    []
  end
end

#country_ids=(ids) ⇒ Object


122
123
124
# File 'app/models/spree/zone.rb', line 122

def country_ids=(ids)
  set_zone_members(ids, 'Spree::Country')
end

#country_listObject

convenience method for returning the countries contained within a zone


88
89
90
91
92
93
94
# File 'app/models/spree/zone.rb', line 88

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

Returns:

  • (Boolean)

72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'app/models/spree/zone.rb', line 72

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

#kindObject


62
63
64
65
66
# File 'app/models/spree/zone.rb', line 62

def kind
  if members.any? && members.none? { |member| member.try(:zoneable_type).nil? }
    members.last.zoneable_type.demodulize.underscore
  end
end

#kind=(value) ⇒ Object


68
69
70
# File 'app/models/spree/zone.rb', line 68

def kind=(value)
  # do nothing - just here to satisfy the form
end

#state_idsObject


114
115
116
117
118
119
120
# File 'app/models/spree/zone.rb', line 114

def state_ids
  if kind == 'state'
    members.pluck(:zoneable_id)
  else
    []
  end
end

#state_ids=(ids) ⇒ Object


126
127
128
# File 'app/models/spree/zone.rb', line 126

def state_ids=(ids)
  set_zone_members(ids, 'Spree::State')
end

#zoneablesObject

All zoneables belonging to the zone members. Will be a collection of either countries or states depending on the zone type.


102
103
104
# File 'app/models/spree/zone.rb', line 102

def zoneables
  members.includes(:zoneable).collect(&:zoneable)
end