Sha256: a46fe0655d5903629eae047a96b691b38d44ee48eed75c9530f61831057377fe

Contents?: true

Size: 1.5 KB

Versions: 7

Compression:

Stored size: 1.5 KB

Contents

module Enjoy::Mappable
  extend ActiveSupport::Concern

  included do

    if Enjoy.mongoid?
      include ::Geocoder::Model::Mongoid
      field :coordinates, type: Array
      field :address, type: String

      field :map_address, type: String
      field :map_hint, type: String

      field :lat, type: Float
      field :lon, type: Float
    end

    geocoded_by :geo_address
    after_validation :do_geocode
  end

  if Enjoy.active_record?
    def coordinates
      if latitude.nil? || longitude.nil?
        nil
      else
        [longitude, latitude]
      end
    end
  end

  def do_geocode
    if geo_address.blank?
      if Enjoy.mongoid?
        self.coordinates = nil
      else
        self.latitude = nil
        self.longitude = nil
      end
    else
      if (lat.nil? || lon.nil?) && (new_record? || address_changed? || coordinates.nil? || map_address_changed?)
        geocode
      end
    end
  end

  def get_lat
    if lat.blank?
      if coordinates.nil?
        nil
      else
        coordinates[1]
      end
    else
      lat
    end
  end
  def get_lon
    if lon.blank?
      if coordinates.nil?
        nil
      else
        coordinates[0]
      end
    else
      lon
    end
  end

  def has_map?
    (!lat.blank? && !lon.blank?) || !coordinates.nil?
  end

  def to_map
    {
      id: id.to_s,
      hint: map_hint,
      addr: address,
      lat: get_lat,
      lon: get_lon,
    }
  end

  def geo_address
    if map_address.blank?
      address
    else
      map_address
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
enjoy_cms-0.3.7 app/models/concerns/enjoy/mappable.rb
enjoy_cms-0.3.6.2 app/models/concerns/enjoy/mappable.rb
enjoy_cms-0.3.6.1 app/models/concerns/enjoy/mappable.rb
enjoy_cms-0.3.6 app/models/concerns/enjoy/mappable.rb
enjoy_cms-0.3.5 app/models/concerns/enjoy/mappable.rb
enjoy_cms-0.3.4 app/models/concerns/enjoy/mappable.rb
enjoy_cms-0.3.3 app/models/concerns/enjoy/mappable.rb