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

page, spree_base_scopes

Methods included from Preferences::Preferable

#clear_preferences, #default_preferences, #defined_preferences, #get_preference, #has_preference!, #has_preference?, #preference_default, #preference_type, #set_preference

Class Method Details

.default_taxObject



27
28
29
# File 'app/models/spree/zone.rb', line 27

def self.default_tax
  find_by(default_tax: true)
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.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/models/spree/zone.rb', line 53

def self.match(address)
  return unless address &&
                matches = includes(:zone_members)
                .order('spree_zones.zone_members_count', 'spree_zones.created_at')
                .where("(spree_zone_members.zoneable_type = 'Spree::Country' AND " \
                                  'spree_zone_members.zoneable_id = ?) OR ' \
                                  "(spree_zone_members.zoneable_type = 'Spree::State' AND " \
                                  'spree_zone_members.zoneable_id = ?)', address.country_id, address.state_id)
                .references(:zones)

  %w[state country].each do |zone_kind|
    if match = matches.detect { |zone| zone_kind == zone.kind }
      return match
    end
  end
  matches.first
end

.potential_matching_zones(zone) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/models/spree/zone.rb', line 31

def self.potential_matching_zones(zone)
  if zone.country?
    # Match zones of the same kind with similar countries
    joins(countries: :zones)
      .where('zone_members_spree_countries_join.zone_id = ?', zone.id)
      .distinct
  else
    # Match zones of the same kind with similar states in AND match zones
    # that have the states countries in
    joins(:zone_members).where(
      "(spree_zone_members.zoneable_type = 'Spree::State' AND
        spree_zone_members.zoneable_id IN (?))
       OR (spree_zone_members.zoneable_type = 'Spree::Country' AND
        spree_zone_members.zoneable_id IN (?))",
      zone.state_ids,
      zone.states.pluck(:country_id)
    ).distinct
  end
end

Instance Method Details

#<=>(other) ⇒ Object



116
117
118
# File 'app/models/spree/zone.rb', line 116

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

#contains?(target) ⇒ Boolean

Indicates whether the specified zone falls entirely within the zone performing the check.

Returns:

  • (Boolean)


152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'app/models/spree/zone.rb', line 152

def contains?(target)
  return false if state? && target.country?
  return false if zone_members.empty? || target.zone_members.empty?

  if kind == target.kind
    if state?
      return false if (target.states.pluck(:id) - states.pluck(:id)).present?
    elsif country?
      return false if (target.countries.pluck(:id) - countries.pluck(:id)).present?
    end
  else
    return false if (target.states.pluck(:country_id) - countries.pluck(:id)).present?
  end
  true
end

#country?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'app/models/spree/zone.rb', line 81

def country?
  kind == 'country'
end

#country_idsObject



126
127
128
129
130
131
132
# File 'app/models/spree/zone.rb', line 126

def country_ids
  if country?
    members.pluck(:zoneable_id)
  else
    []
  end
end

#country_ids=(ids) ⇒ Object



142
143
144
# File 'app/models/spree/zone.rb', line 142

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

#country_listObject

convenience method for returning the countries contained within a zone



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

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)


89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'app/models/spree/zone.rb', line 89

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



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

def kind
  if kind?
    super
  else
    not_nil_scope = members.where.not(zoneable_type: nil)
    zone_type = not_nil_scope.order('created_at ASC').pluck(:zoneable_type).last
    zone_type.demodulize.underscore if zone_type
  end
end

#state?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'app/models/spree/zone.rb', line 85

def state?
  kind == 'state'
end

#state_idsObject



134
135
136
137
138
139
140
# File 'app/models/spree/zone.rb', line 134

def state_ids
  if state?
    members.pluck(:zoneable_id)
  else
    []
  end
end

#state_ids=(ids) ⇒ Object



146
147
148
# File 'app/models/spree/zone.rb', line 146

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.



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

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