rails_browser_timezone 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +5 -0
- data/README.md +79 -0
- data/lib/rails_browser_timezone/filter.rb +30 -0
- data/lib/rails_browser_timezone/setting.rb +23 -0
- data/lib/rails_browser_timezone/version.rb +3 -0
- data/rails_browser_timezone.gemspec +27 -0
- metadata +86 -0
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
rails_browser_timezone
|
2
|
+
======================
|
3
|
+
|
4
|
+
The purpose of this gem is to track browser's timezone and run rails controller code in the time zone
|
5
|
+
detected for each request.
|
6
|
+
|
7
|
+
For background and thoughts, please refer - http://developerstream.blogspot.in/2015/09/handling-requests-in-browser-time-zone.html
|
8
|
+
|
9
|
+
Instructions to use
|
10
|
+
==================
|
11
|
+
|
12
|
+
Installation -
|
13
|
+
--------------------
|
14
|
+
|
15
|
+
```
|
16
|
+
gem 'rails_browser_timezone', '0.2.0', :git => 'https://github.com/udayakiran/rails_browser_timezone'
|
17
|
+
```
|
18
|
+
|
19
|
+
Usage -
|
20
|
+
---------
|
21
|
+
|
22
|
+
1) Create a file in your initializers and set baseline year. This step is optional. This means we are worried about time zone changes that occurred till this year. My suggesstion is to keep this year same as either the year you are starting this project or the year when your rails version is released.
|
23
|
+
Use "current" as the value if you want to stay on the edge. But, note that rails and your browsers need to be supporting this as well.
|
24
|
+
|
25
|
+
```
|
26
|
+
# Say in config/initializers/rails_browser_tz_init.rb
|
27
|
+
RailsBrowserTimezone::Setting.baseline_year = 2014 #default value is 2011. Accepted values - any valid year or string - "current"
|
28
|
+
```
|
29
|
+
|
30
|
+
2) Include the around filter in every controller that needs to run code in user's timezone. If you need it for all controllers obviously add it to the application controller.
|
31
|
+
|
32
|
+
```
|
33
|
+
#Rails 4.1.x or earlier (inlcuding Rails 2,3 and 4)
|
34
|
+
|
35
|
+
class MyTimeZoneController < ApplicationController #Or in your application_controller.rb
|
36
|
+
|
37
|
+
prepend_around_filter RailsBrowserTimezone::Filter
|
38
|
+
.......
|
39
|
+
.......
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
#Rails 4.2.x or later
|
44
|
+
|
45
|
+
class MyTimeZoneController < ApplicationController #Or in your application_controller.rb
|
46
|
+
|
47
|
+
prepend_around_action RailsBrowserTimezone::Filter
|
48
|
+
.......
|
49
|
+
.......
|
50
|
+
|
51
|
+
end
|
52
|
+
```
|
53
|
+
|
54
|
+
3) Add the js code that sets the browser offsets in your js files. Code can be found in assets directory based on the js lib you use.
|
55
|
+
If you use jquery, copy the js code from 'assets/set_browser_offset_cokies_jquery.js' and paste it in your js file which is inlcuded in every page.
|
56
|
+
If you use prototypejs, copy the js code 'from assets/set_browser_offset_cokies_prototype.js' and paste it in your js file which is inlcuded in every page.
|
57
|
+
|
58
|
+
Saving time zone in the database -
|
59
|
+
--------------------------------
|
60
|
+
|
61
|
+
If you like to save the last_known_timezone of any user in the database, it can be done by using "Time.zone.name" any where in your controller,
|
62
|
+
once 'RailsBrowserTimezone::Filter' is done with the determining of the time zone from offsets.
|
63
|
+
|
64
|
+
Practices in your code -
|
65
|
+
-------------------------
|
66
|
+
|
67
|
+
1. Use Time.zone.* not Time.* :- Most of the scenarios we need to deal with times in the current time zone not in the system time zone on which app is running. So, we should use Time.zone.now, Time.zone.parse and time_obj.in_time_zone(Time.zone) when we are dealing with time information.
|
68
|
+
|
69
|
+
2. Use Time.use_zone :- When we need to operate in other time zones than the current system, enlose that code in Time.use_zone block. This sets back the system time zone once the code completes execution or even when exception occurs. Otherwise we should always remember to set the system's time zone back to default.
|
70
|
+
|
71
|
+
To do -
|
72
|
+
-------
|
73
|
+
|
74
|
+
Add specs and tests.
|
75
|
+
|
76
|
+
Contributing -
|
77
|
+
----------
|
78
|
+
|
79
|
+
Please help with your contribution by filing any issues if found. Pull requests are welcomed :)
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# Runs the controller code in the user's time zone which is determined through the offsets passed via cookies.
|
2
|
+
module RailsBrowserTimezone
|
3
|
+
class Filter
|
4
|
+
|
5
|
+
def self.filter(controller, &block)
|
6
|
+
cookies = controller.send(:cookies)
|
7
|
+
last_known_tz = cookies[:last_known_tz] ? cookies[:last_known_tz] : Time.zone_default.name
|
8
|
+
current_time_zone = time_zone_from_utc_offset(cookies[:utc_offset_summer].to_i, cookies[:utc_offset_winter].to_i, last_known_tz)
|
9
|
+
controller.response.set_cookie(:last_known_tz, {:path => "/", :value => current_time_zone.name})
|
10
|
+
|
11
|
+
Time.use_zone(current_time_zone) do
|
12
|
+
yield
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
# Returns the time zone based on parsed *utc_offset_summer* and *utc_offset_winter*
|
17
|
+
# Returns the default TimeZone if none is resolved
|
18
|
+
def self.time_zone_from_utc_offset(utc_offset_summer, utc_offset_winter, last_known_tz)
|
19
|
+
# ActiveSupport::TimeZone[offset] - will return the first time zone that matches the offset.
|
20
|
+
# But, we need to get the exact time zone inorder to reflect the daylight savings.
|
21
|
+
# So, we get the user's time zone exactly by matching the summer offset and winter offset both.
|
22
|
+
[ActiveSupport::TimeZone[last_known_tz], ActiveSupport::TimeZone.all].flatten.compact.detect(ifnone = Time.method(:zone_default)) do |zone|
|
23
|
+
Time.use_zone(zone.name) do
|
24
|
+
Time.zone.parse(Setting.mid_summer_date_str).utc_offset == utc_offset_summer && Time.zone.parse(Setting.mid_winter_date_str).utc_offset == utc_offset_winter
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module RailsBrowserTimezone
|
2
|
+
class Setting
|
3
|
+
class << self
|
4
|
+
attr_writer :baseline_year
|
5
|
+
|
6
|
+
def baseline_year
|
7
|
+
@baseline_year ||= 2011
|
8
|
+
end
|
9
|
+
|
10
|
+
def mid_summer_date_str
|
11
|
+
"#{year}-6-21"
|
12
|
+
end
|
13
|
+
|
14
|
+
def mid_winter_date_str
|
15
|
+
"#{year}-12-21"
|
16
|
+
end
|
17
|
+
|
18
|
+
def year
|
19
|
+
(baseline_year.to_sym == :current) ? Time.zone.now.year : baseline_year
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "rails_browser_timezone/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "rails_browser_timezone"
|
7
|
+
s.version = RailsBrowserTimezone::VERSION
|
8
|
+
s.authors = ["Udaya Kiran"]
|
9
|
+
s.email = ["udaykiran.vit@gmail.com"]
|
10
|
+
s.homepage = ""
|
11
|
+
s.summary = %q{Rails Browser Timezone client gem}
|
12
|
+
s.description = %q{Simple code to detect browser time zone and handle it in rails app}
|
13
|
+
|
14
|
+
s.rubyforge_project = "rails_browser_timezone"
|
15
|
+
|
16
|
+
s.files = %w[
|
17
|
+
Gemfile
|
18
|
+
README.md
|
19
|
+
lib/rails_browser_timezone/filter.rb
|
20
|
+
lib/rails_browser_timezone/version.rb
|
21
|
+
lib/rails_browser_timezone/setting.rb
|
22
|
+
rails_browser_timezone.gemspec
|
23
|
+
]
|
24
|
+
s.require_paths = ["lib"]
|
25
|
+
|
26
|
+
s.add_runtime_dependency "activesupport", '> 2'
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rails_browser_timezone
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 23
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 2
|
9
|
+
- 0
|
10
|
+
version: 0.2.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Udaya Kiran
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2015-10-05 00:00:00 +05:30
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: activesupport
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">"
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 7
|
30
|
+
segments:
|
31
|
+
- 2
|
32
|
+
version: "2"
|
33
|
+
type: :runtime
|
34
|
+
version_requirements: *id001
|
35
|
+
description: Simple code to detect browser time zone and handle it in rails app
|
36
|
+
email:
|
37
|
+
- udaykiran.vit@gmail.com
|
38
|
+
executables: []
|
39
|
+
|
40
|
+
extensions: []
|
41
|
+
|
42
|
+
extra_rdoc_files: []
|
43
|
+
|
44
|
+
files:
|
45
|
+
- Gemfile
|
46
|
+
- README.md
|
47
|
+
- lib/rails_browser_timezone/filter.rb
|
48
|
+
- lib/rails_browser_timezone/version.rb
|
49
|
+
- lib/rails_browser_timezone/setting.rb
|
50
|
+
- rails_browser_timezone.gemspec
|
51
|
+
has_rdoc: true
|
52
|
+
homepage: ""
|
53
|
+
licenses: []
|
54
|
+
|
55
|
+
post_install_message:
|
56
|
+
rdoc_options: []
|
57
|
+
|
58
|
+
require_paths:
|
59
|
+
- lib
|
60
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
hash: 3
|
66
|
+
segments:
|
67
|
+
- 0
|
68
|
+
version: "0"
|
69
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
|
+
none: false
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
hash: 3
|
75
|
+
segments:
|
76
|
+
- 0
|
77
|
+
version: "0"
|
78
|
+
requirements: []
|
79
|
+
|
80
|
+
rubyforge_project: rails_browser_timezone
|
81
|
+
rubygems_version: 1.3.7
|
82
|
+
signing_key:
|
83
|
+
specification_version: 3
|
84
|
+
summary: Rails Browser Timezone client gem
|
85
|
+
test_files: []
|
86
|
+
|