Sha256: 1eff1e40b580918c0a0a1f521c68af19853d74e5db6c0f8ceb18453c850159da

Contents?: true

Size: 987 Bytes

Versions: 2

Compression:

Stored size: 987 Bytes

Contents

#
# Cities
#
class City
  include Mongoid::Document
  include Mongoid::Geospatial
  include Geopolitocracy

  field :zip,    type: String
  field :area,   type: Integer  # m2 square area
  field :souls,  type: Integer  # Population
  field :geom,   type: Point,   spatial: true

  alias_method :population, :souls

  spatial_scope :geom

  attr_writer :x, :y, :z

  belongs_to :region
  belongs_to :nation
  has_many :hoods

  validates :name, uniqueness: { scope: :nation_id }

  before_validation :set_defaults, on: [:create]

  def set_defaults
    self.nation ||= region.try(:nation)
    return unless City.where(slug: slug).first
    self.slug += "-#{region.abbr}"
    return unless City.where(slug: slug).first
    fail "Two cities with the same name in #{region}: '#{slug}'"
  end

  def abbr
    return unless region || nation
    region ? region.abbr : nation.abbr
  end

  def ==(other)
    other && slug == other.slug
  end

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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
geopolitical-0.8.7 app/models/city.rb
geopolitical-0.8.6 app/models/city.rb