Class: Locale

Inherits:
Object
  • Object
show all
Includes:
Geocoder::Model::Mongoid, Mongoid::Document, Mongoid::Timestamps, Mongoid::Tree
Defined in:
app/models/locale.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#skip_geocodingObject

Macros =========================================================================================



35
36
37
# File 'app/models/locale.rb', line 35

def skip_geocoding
  @skip_geocoding
end

Class Method Details

.find_or_create_country_for(location) ⇒ Object



45
46
47
# File 'app/models/locale.rb', line 45

def self.find_or_create_country_for(location)
  Locale.countries.where(:name => location.country_long_name).first || Locale.create_country_for(location)
end

.find_or_create_state_for(location) ⇒ Object



49
50
51
# File 'app/models/locale.rb', line 49

def self.find_or_create_state_for(location)
  Locale.states.where(:abbreviation => location.state).first || Locale.find_or_create_country_for(location).children.create(:abbreviation => location.state, :name => location.state_long_name)
end

.init_rootObject

Initialize the root-level locale.



41
42
43
# File 'app/models/locale.rb', line 41

def self.init_root
  Locale.find_or_create_by(:name => 'Earth')
end

Instance Method Details

#city?Boolean

Instance Methods: Typing =======================================================================

Returns:

  • (Boolean)


144
145
146
# File 'app/models/locale.rb', line 144

def city?
  self.kind == 'city'
end

#country?Boolean

Returns:

  • (Boolean)


148
149
150
# File 'app/models/locale.rb', line 148

def country?
  self.kind == 'country'
end

#destroyObject

Instance Methods: Tree =========================================================================



103
104
105
106
107
108
109
110
111
112
# File 'app/models/locale.rb', line 103

def destroy
  if self.parent && self.parent.children.count == 1
    Rails.logger.info "Destroyed #{self.parent.name}"
    self.parent.destroy
  elsif self.parent
    Rails.logger.info "Did not destroy #{self.parent.name}: has #{self.parent.children.to_a.map{|x|x.name.to_s}.sort * ', '}"
  end

  super if ! country? || (country? && slug.blank?)
end

#friendly_nameObject

Instance methods: Overrides ====================================================================



60
61
62
# File 'app/models/locale.rb', line 60

def friendly_name
  self.city? ? "#{self.name}, #{self.parent.name}" : self.name
end

#geocoded?Boolean

Returns true if this location has a latitude and longitude.

Returns:

  • (Boolean)


76
77
78
# File 'app/models/locale.rb', line 76

def geocoded?
  self.coordinates.is_a?(Array)
end

#geocoding_addressObject

Returns this locale’s address for geocoding.



81
82
83
84
85
86
87
88
89
90
# File 'app/models/locale.rb', line 81

def geocoding_address
  a = case self.kind
    when 'country' then self.name
    when 'state' then self.name
    when 'city' then [self.name, self.parent.name]
    when 'location' then nil
  end

  a && a.select{|s| ! s.blank?} * ' '
end

#has_other_locations?Boolean

Returns:

  • (Boolean)


114
115
116
# File 'app/models/locale.rb', line 114

def has_other_locations?
  location_locales.count > 1
end

#location?Boolean

Returns:

  • (Boolean)


152
153
154
# File 'app/models/locale.rb', line 152

def location?
  self.kind == 'location'
end

#location_localesObject

Returns location-locales found within this locale.



119
120
121
# File 'app/models/locale.rb', line 119

def location_locales
  self.descendants.valid_location
end

#locationsObject

Returns locations found within this locale.



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

def locations
  (location_locales.map{ |l| l.location } | [self.location]).compact
end

#non_root_ancestors_and_selfObject

Returns an array of this locale’s ancestors and itself. Countries are omitted.



129
130
131
# File 'app/models/locale.rb', line 129

def non_root_ancestors_and_self
  self.ancestors_and_self.select{ |l| ! l.root? }
end

#planet?Boolean

Returns:

  • (Boolean)


156
157
158
# File 'app/models/locale.rb', line 156

def planet?
  self.kind == 'planet'
end

#post_processObject

Instance methods: Initialization ===============================================================



54
55
56
57
# File 'app/models/locale.rb', line 54

def post_process
  set_kind
  save
end

#set_kindObject



164
165
166
167
168
169
170
171
172
# File 'app/models/locale.rb', line 164

def set_kind
  case self.depth
    when 0; self.kind = 'planet'
    when 1; self.kind = 'country'
    when 2; self.kind = 'state'
    when 3; self.kind = 'city'
    when 4; self.kind = 'location'
  end
end

#set_slugObject

Sets the slug for this locale. Slugs from the locale tree are used to build this locale’s URL.



134
135
136
# File 'app/models/locale.rb', line 134

def set_slug
  self.slug = self.root? ? nil : "#{(non_root_ancestors_and_self_names * '/').to_url.gsub('-slash-', '/')}"
end

#sorted_childrenObject

Returns this locale’s sorted children.



65
66
67
68
69
70
71
# File 'app/models/locale.rb', line 65

def sorted_children
  if self.root?
    self.children.map{ |l| l.children }.flatten.sort_by{ |c| c.name.to_s }
  else
    self.children.sort_by{ |c| c.name.to_s }
  end
end

#state?Boolean

Returns:

  • (Boolean)


160
161
162
# File 'app/models/locale.rb', line 160

def state?
  self.kind == 'state'
end

#to_paramObject

Returns the permalink for this locale.



139
140
141
# File 'app/models/locale.rb', line 139

def to_param
  "/#{CampfireLogic.directory_slug}/#{slug}"
end

#zoom_levelObject

Return an appropriate Google-Maps zoom level.



93
94
95
96
97
98
99
100
# File 'app/models/locale.rb', line 93

def zoom_level
  case self.kind
    when 'country' then 3
    when 'state' then 5
    when 'city' then 9
    when 'location' then 10
  end
end