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, #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

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



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

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



94
95
96
# File 'app/models/spree/zone.rb', line 94

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

#country_idsObject



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

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

#country_ids=(ids) ⇒ Object



120
121
122
# File 'app/models/spree/zone.rb', line 120

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

#country_listObject

convenience method for returning the countries contained within a zone



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

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)


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

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



60
61
62
63
64
# File 'app/models/spree/zone.rb', line 60

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

#kind=(value) ⇒ Object



66
67
68
# File 'app/models/spree/zone.rb', line 66

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

#state_idsObject



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

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

#state_ids=(ids) ⇒ Object



124
125
126
# File 'app/models/spree/zone.rb', line 124

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.



100
101
102
# File 'app/models/spree/zone.rb', line 100

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