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



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

def skip_geocoding
  @skip_geocoding
end

Class Method Details

.find_or_create_country_for(location) ⇒ Object



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

def self.find_or_create_country_for(location)
  Locale.first(:conditions => {:name => location.country_long_name}) || Locale.create_country_for(location)
end

.find_or_create_state_for(location) ⇒ Object



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

def self.find_or_create_state_for(location)
  Locale.first(:conditions => {:name => location.state_long_name}) || 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.



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

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

Instance Method Details

#city?Boolean

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

Returns:

  • (Boolean)


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

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

#country?Boolean

Returns:

  • (Boolean)


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

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

#destroyObject

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



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

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 unless self.country?
end

#friendly_nameObject

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



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

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)


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

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

#geocoding_addressObject

Returns this locale’s address for geocoding.



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

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)


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

def has_other_locations?
  location_locales.count > 1
end

#location?Boolean

Returns:

  • (Boolean)


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

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

#location_localesObject

Returns location-locales found within this locale.



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

def location_locales
  self.descendants.valid_location
end

#locationsObject

Returns locations found within this locale.



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

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.



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

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

#planet?Boolean

Returns:

  • (Boolean)


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

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

#post_processObject

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



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

def post_process
  set_kind
  save
end

#set_kindObject



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

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.



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

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.



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

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)


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

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

#to_paramObject

Returns the permalink for this locale.



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

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

#zoom_levelObject

Return an appropriate Google-Maps zoom level.



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

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