summaryrefslogtreecommitdiff
path: root/app/models/location.rb
blob: 0756476c9def186ff786096fbf9d6653f6e525ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Location < ApplicationRecord
  belongs_to :locatable, polymorphic: true
  validates :locatable, presence: true

  GEO_IP=GeoIP.new('config/GeoLiteCity.dat')

  class << self
    def build_from_ip(ip)
      city = GEO_IP.city(ip)
      return nil unless city
      Location.new(
        latitude: city.latitude,
        longitude: city.longitude,
        city: city.city_name,
        country: city.country_name
      )
    end
  end
end