app/models/city.rb in geopolitical-0.0.1 vs app/models/city.rb in geopolitical-0.8.0
- old
+ new
@@ -1,54 +1,53 @@
class City
include Mongoid::Document
include Mongoid::Geospatial
+ include Geopolitical::Helpers
- # include GeoHelper
+ field :gid, type: Integer
+ field :slug, type: String
+ field :name, type: String, localize: true
+ field :area, type: Integer
+ field :zip, type: Integer
+ field :souls, type: Integer
+ field :geom, type: Point, spatial: true
+ # field :ascii, type: String
- field :slug
- field :name
- field :area
- field :gid, type: Integer
- field :zip, type: Integer
- field :souls, type: Integer
- field :geom, type: Point, spatial: true
+ spatial_scope :geom
- # index [[ :geom, Mongo::GEO2D ]], min: 200, max: 200
- index :slug
- spatial_index :geom
+ attr_writer :x, :y, :z
- belongs_to :province
- belongs_to :country, index: true
+ belongs_to :region
+ belongs_to :nation
has_many :hoods
- before_validation :set_defaults
- validates :name, :country, presence: true
- validates_uniqueness_of :name, :scope => :province_id
+ index name: 1
- index [[:name, 1]]
+ scope :ordered, order_by(name: 1)
- scope :ordered, order_by(:name, 1)
+ validates :slug, presence: true, uniqueness: true
+ validates :name, uniqueness: { :scope => :region_id }
- def abbr
- province ? province.abbr : country.abbr
- end
+ # scope :close_to, GeoHelper::CLOSE
+ before_validation :set_defaults
+
+
def set_defaults
- self.country ||= province.try(:country)
- self.slug ||= name.try(:downcase) # don't use slugize
+ self.nation ||= region.try(:nation)
end
- def self.search(search, page)
- cities = search ? where(:field => /#{search}/i) : all
- cities.page(page)
+ def abbr
+ region ? region.abbr : nation.abbr
end
- def self.drop_down
- ordered.map{|c| [c.name, c.id]}
+ def self.search txt
+ where(slug: /#{txt}/i)
end
def <=> other
- self.name <=> other.name
+ self.slug <=> other.slug
end
+
end