Sha256: 913fbeb156518ba49e5d788db1b86dc712bd2ff40d52030221d00ac1fb3d419a

Contents?: true

Size: 1.34 KB

Versions: 3

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

3 entries across 3 versions & 1 rubygems

Version Path
on_the_map-0.1.4 lib/address.rb
on_the_map-0.1.3 lib/address.rb
on_the_map-0.1.1 lib/address.rb