timezone_detection 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
@@ -1,17 +1,21 @@
|
|
1
1
|
module TimezoneDetection
|
2
2
|
|
3
3
|
class IpTimezone < ActiveRecord::Base
|
4
|
-
|
4
|
+
extend TimezoneDetection::IpConvertion
|
5
5
|
|
6
6
|
attr_accessor :ip
|
7
7
|
attr_accessible :ip, :utc_offset
|
8
8
|
|
9
9
|
before_save :cancel_if_current_ip_already_exists
|
10
10
|
|
11
|
+
def self.find_ip(ip)
|
12
|
+
IpTimezone.where(ip_in_longint: IpTimezone.ip2long(ip))
|
13
|
+
end
|
14
|
+
|
11
15
|
private
|
12
16
|
|
13
17
|
def cancel_if_current_ip_already_exists
|
14
|
-
self.ip_in_longint = ip2long(ip)
|
18
|
+
self.ip_in_longint = IpTimezone.ip2long(ip)
|
15
19
|
!IpTimezone.where(ip_in_longint: self.ip_in_longint).exists? #Stops saving if return false
|
16
20
|
end
|
17
21
|
end
|
@@ -1,9 +1,12 @@
|
|
1
1
|
module TimezoneDetection
|
2
2
|
module ClassMethods
|
3
3
|
def set_timezone
|
4
|
-
|
5
|
-
|
6
|
-
|
4
|
+
ip_timezone = IpTimezone.find_ip(request.remote_ip).first
|
5
|
+
unless ip_timezone.present?
|
6
|
+
ip_info = IpInfoDB.new
|
7
|
+
offset = ip_info.get_timezone_offset(request.remote_ip)
|
8
|
+
ip_timezone = IpTimezone.create(ip: request.remote_ip, utc_offset: offset)
|
9
|
+
end
|
7
10
|
Time.zone = ActiveSupport::TimeZone[ip_timezone.utc_offset]
|
8
11
|
end
|
9
12
|
end
|
data/test/dummy/log/test.log
CHANGED
@@ -937,3 +937,32 @@ Connecting to database specified by database.yml
|
|
937
937
|
[1m[35m (0.1ms)[0m rollback transaction
|
938
938
|
[1m[36m (0.1ms)[0m [1mbegin transaction[0m
|
939
939
|
[1m[35m (0.0ms)[0m rollback transaction
|
940
|
+
Connecting to database specified by database.yml
|
941
|
+
[1m[36m (0.6ms)[0m [1mbegin transaction[0m
|
942
|
+
[1m[35m (0.1ms)[0m rollback transaction
|
943
|
+
[1m[36m (0.1ms)[0m [1mbegin transaction[0m
|
944
|
+
[1m[35m (0.1ms)[0m rollback transaction
|
945
|
+
[1m[36m (0.0ms)[0m [1mbegin transaction[0m
|
946
|
+
[1m[35mTimezoneDetection::IpTimezone Load (0.1ms)[0m SELECT "timezone_detection_ip_timezones".* FROM "timezone_detection_ip_timezones"
|
947
|
+
[1m[36m (0.1ms)[0m [1mSAVEPOINT active_record_1[0m
|
948
|
+
[1m[35mTimezoneDetection::IpTimezone Exists (0.2ms)[0m SELECT 1 AS one FROM "timezone_detection_ip_timezones" WHERE "timezone_detection_ip_timezones"."ip_in_longint" = 1245284686 LIMIT 1
|
949
|
+
[1m[36mSQL (20.4ms)[0m [1mINSERT INTO "timezone_detection_ip_timezones" ("ip_in_longint", "utc_offset") VALUES (?, ?)[0m [["ip_in_longint", 1245284686], ["utc_offset", 3]]
|
950
|
+
[1m[35m (0.1ms)[0m RELEASE SAVEPOINT active_record_1
|
951
|
+
[1m[36m (0.1ms)[0m [1mSELECT COUNT(*) FROM "timezone_detection_ip_timezones" [0m
|
952
|
+
[1m[35m (0.2ms)[0m rollback transaction
|
953
|
+
[1m[36m (0.1ms)[0m [1mbegin transaction[0m
|
954
|
+
[1m[35m (0.0ms)[0m SAVEPOINT active_record_1
|
955
|
+
[1m[36mTimezoneDetection::IpTimezone Exists (0.2ms)[0m [1mSELECT 1 AS one FROM "timezone_detection_ip_timezones" WHERE "timezone_detection_ip_timezones"."ip_in_longint" = 1245284686 LIMIT 1[0m
|
956
|
+
[1m[35mSQL (0.2ms)[0m INSERT INTO "timezone_detection_ip_timezones" ("ip_in_longint", "utc_offset") VALUES (?, ?) [["ip_in_longint", 1245284686], ["utc_offset", 3]]
|
957
|
+
[1m[36m (0.1ms)[0m [1mRELEASE SAVEPOINT active_record_1[0m
|
958
|
+
[1m[35m (0.1ms)[0m SAVEPOINT active_record_1
|
959
|
+
[1m[36mTimezoneDetection::IpTimezone Exists (0.2ms)[0m [1mSELECT 1 AS one FROM "timezone_detection_ip_timezones" WHERE "timezone_detection_ip_timezones"."ip_in_longint" = 1245284686 LIMIT 1[0m
|
960
|
+
[1m[35m (0.1ms)[0m ROLLBACK TO SAVEPOINT active_record_1
|
961
|
+
[1m[36m (0.2ms)[0m [1mSELECT COUNT(*) FROM "timezone_detection_ip_timezones" [0m
|
962
|
+
[1m[35m (0.2ms)[0m rollback transaction
|
963
|
+
[1m[36m (0.1ms)[0m [1mbegin transaction[0m
|
964
|
+
[1m[35m (0.1ms)[0m rollback transaction
|
965
|
+
[1m[36m (0.1ms)[0m [1mbegin transaction[0m
|
966
|
+
[1m[35m (0.1ms)[0m rollback transaction
|
967
|
+
[1m[36m (0.1ms)[0m [1mbegin transaction[0m
|
968
|
+
[1m[35m (0.1ms)[0m rollback transaction
|