Sha256: c1432dd11d4cb9e8a725306b45fd261a5452187fdffb140be581629de9024524

Contents?: true

Size: 1.34 KB

Versions: 1

Compression:

Stored size: 1.34 KB

Contents

class Address
  # include BasicDocument
  include Mongoid::Document
  include Mongoid::Dirty

  after_update :perform_geocoding

  def self.geo_address_fields
    %w{city state state_code province province_code postal_code country country_code}
  end

  def self.all_geo_address_fields
    geo_address_fields + %w{latitude longitude}
  end
  
  def self.address_fields
    %w{street number floor_adr} + geo_address_fields
  end

  address_fields.each do |name|
    field name, type: String    
  end

  field :full, type: String, default: ""

  embedded_in :addressable, polymorphic: true

  # validates :street, presence: true

  before_create :set_defaults

  def floor
    floor_adr.to_i
  end

  def region
    state || province
  end

  def xtra_fields
    [:floor_adr]
  end

  def to_s
    Address.address_fields.inject("") do |res, name|
      res << "#{name}: #{send(name)}\n"
    end
  end

  def geolocatable?
    !full.blank?
  end

  def perform_geocoding
    return unless geolocatable?
    addressable.perform_geocoding if addressable.respond_to? :perform_geocoding
  end
  alias_method :perform_geocoding!, :perform_geocoding  

  protected

  def set_defaults
    write_attribute :country_code, 'DK'
  end

  def set_full
    self.full = Address.address_fields.select do |name|
      !send(name).blank?
    end.map{|name| send(name) }.join(', ')
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
on_the_map-0.1.0 lib/address.rb