Class: Locale
- Inherits:
-
Object
- Object
- Locale
- Includes:
- Geocoder::Model::Mongoid, Mongoid::Document, Mongoid::Timestamps, Mongoid::Tree
- Defined in:
- app/models/locale.rb
Instance Attribute Summary collapse
-
#skip_geocoding ⇒ Object
Macros =========================================================================================.
Class Method Summary collapse
- .find_or_create_country_for(location) ⇒ Object
- .find_or_create_state_for(location) ⇒ Object
-
.init_root ⇒ Object
Initialize the root-level locale.
Instance Method Summary collapse
-
#city? ⇒ Boolean
Instance Methods: Typing =======================================================================.
- #country? ⇒ Boolean
-
#destroy ⇒ Object
Instance Methods: Tree =========================================================================.
-
#friendly_name ⇒ Object
Instance methods: Overrides ====================================================================.
-
#geocoded? ⇒ Boolean
Returns true if this location has a latitude and longitude.
-
#geocoding_address ⇒ Object
Returns this locale’s address for geocoding.
- #has_other_locations? ⇒ Boolean
- #location? ⇒ Boolean
-
#location_locales ⇒ Object
Returns location-locales found within this locale.
-
#locations ⇒ Object
Returns locations found within this locale.
-
#non_root_ancestors_and_self ⇒ Object
Returns an array of this locale’s ancestors and itself.
- #planet? ⇒ Boolean
-
#post_process ⇒ Object
Instance methods: Initialization ===============================================================.
- #set_kind ⇒ Object
-
#set_slug ⇒ Object
Sets the slug for this locale.
-
#sorted_children ⇒ Object
Returns this locale’s sorted children.
- #state? ⇒ Boolean
-
#to_param ⇒ Object
Returns the permalink for this locale.
-
#zoom_level ⇒ Object
Return an appropriate Google-Maps zoom level.
Instance Attribute Details
#skip_geocoding ⇒ Object
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_root ⇒ Object
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 =======================================================================
144 145 146 |
# File 'app/models/locale.rb', line 144 def city? self.kind == 'city' end |
#country? ⇒ Boolean
148 149 150 |
# File 'app/models/locale.rb', line 148 def country? self.kind == 'country' end |
#destroy ⇒ Object
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_name ⇒ Object
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.
76 77 78 |
# File 'app/models/locale.rb', line 76 def geocoded? self.coordinates.is_a?(Array) end |
#geocoding_address ⇒ Object
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
114 115 116 |
# File 'app/models/locale.rb', line 114 def has_other_locations? location_locales.count > 1 end |
#location? ⇒ Boolean
152 153 154 |
# File 'app/models/locale.rb', line 152 def location? self.kind == 'location' end |
#location_locales ⇒ Object
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 |
#locations ⇒ Object
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_self ⇒ Object
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
156 157 158 |
# File 'app/models/locale.rb', line 156 def planet? self.kind == 'planet' end |
#post_process ⇒ Object
Instance methods: Initialization ===============================================================
54 55 56 57 |
# File 'app/models/locale.rb', line 54 def post_process set_kind save end |
#set_kind ⇒ Object
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_slug ⇒ Object
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_children ⇒ Object
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
160 161 162 |
# File 'app/models/locale.rb', line 160 def state? self.kind == 'state' end |
#to_param ⇒ Object
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_level ⇒ Object
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 |