summaryrefslogtreecommitdiff
path: root/app/models/location.rb
blob: 25879096b453f2b26cb8db0098e319a07e071b9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
class Location < ApplicationRecord
  belongs_to :locatable, polymorphic: true, optional: true
  before_save :assign_coordinates
  acts_as_mappable default_units: :kms,
    distance_field_name: :distance,
    lat_column_name: :latitude,
    lng_column_name: :longitude

  def full_address
    [
      try(:address),
      try(:city),
      try(:region),
      try(:country)
    ].join(", ")
  end

  def coordinates
    latitude == 0.0 && longitude == 0.0 ? [] : [latitude, longitude]
  end

  def url
    "https://maps.google.com/?q=#{latitude},#{longitude}"
  end

  class << self
    def build_from_ip(ip)
      result = search(ip)
      return nil if result.nil?
      new(
        address: result.address,
        city: result.city,
        region: result.state_code,
        country: result.country_code,
        postal_code: result.postal_code,
        latitude: result.latitude,
        longitude: result.longitude,
      )
    end

    def from(address, city, region, country)
      result = search("#{address}, #{city}, #{region}, #{country}")
      result.present? ? result.coordinates : [nil, nil]
    end

    def search(query)
      results = Geocoder.search(query)
      results.any? ? results.first : nil
    end
  end

  private

  def assign_coordinates
    return if latitude.present? || longitude.present?
    self.latitude, self.longitude =
      Location.from(address, city, region, country)
  end
end