auto_timezone 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9901a6357efef35d0cde1aa805aa999b5a8e658b
4
+ data.tar.gz: 705672de63cafabf10d84f4b823a164355bf286a
5
+ SHA512:
6
+ metadata.gz: 0436d6a733b2d80affc27b091e525bfcc80c61685dff72582a7f0aa94d35b419ab20f5b6260daad03cecdfb30ed594eff344c20bf9a0e14bf541f1b56ae06c07
7
+ data.tar.gz: 6d815ce6e38fdc83d275ff0f8d3639c098a4a9e647d95a01278fbad842f218746f5f201edbbd1fcd76bdc38cb6e5eb5a9362223801dd4d039862b9fb929739f5
@@ -0,0 +1,16 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+ .DS_Store
16
+ .env
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Nathan Griffith
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,60 @@
1
+ # AutoTimezone
2
+
3
+ Automatically infer and set `Time.zone` with each request. The available (and configurable)
4
+ strategies include:
5
+
6
+ * Using the timezone offset provided by the browser. **[Now supports Daylight Savings Time]**
7
+ * Using a value stored on `current_user` (or anything accessible in the context of the controller)
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'auto_timezone'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Lastly, add this line to the top of your `application.js` (it has no dependencies):
22
+
23
+ ```javascript
24
+ //= require auto_timezone
25
+ ```
26
+
27
+ ## Usage
28
+
29
+ Without any additional configuration, your application should start honoring timezone by asking
30
+ the browser for the offset from UTC time.
31
+
32
+ To install the `auto_timezone.rb` configuration initializer, execute:
33
+
34
+ $ rails g auto_timezone:install
35
+
36
+ Check out the generated `auto_timezone.rb` for more configuration settings.
37
+
38
+ ## Limitations
39
+
40
+ Currently, the auto-inferred timezone will only take effect *after* the first pageload.
41
+
42
+ For the first pageload, you may want to set a more useful default timezone in `application.rb`:
43
+
44
+ ```ruby
45
+ config.time_zone = 'Eastern Time (US & Canada)'
46
+ ```
47
+
48
+ ## To Do
49
+
50
+ * Implement `jsTimezoneDetect` strategy
51
+ * Implement tests
52
+ * Infer or approximate timezone during initial web request (somehow...)
53
+
54
+ ## Contributing
55
+
56
+ 1. Fork it ( https://github.com/smudge/auto_timezone/fork )
57
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
58
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
59
+ 4. Push to the branch (`git push origin my-new-feature`)
60
+ 5. Create a new Pull Request
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'auto_timezone/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'auto_timezone'
8
+ spec.version = AutoTimezone::VERSION
9
+ spec.authors = ['smudge']
10
+ spec.email = ['nathan@ngriffith.com']
11
+ spec.summary = 'Simple and unobtrusive timezone configuration for Rails'
12
+ spec.description = 'Automatically infer and set `Time.zone` with each web request.'
13
+ spec.homepage = 'http://github.com/smudge/auto_timezone'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(/^bin\//) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(/^(test|spec|features)\//)
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_dependency 'railties', '~> 3.2'
22
+
23
+ spec.add_development_dependency 'bundler', '~> 1.7'
24
+ spec.add_development_dependency 'rake', '~> 10.0'
25
+ end
@@ -0,0 +1,10 @@
1
+ require 'auto_timezone/version'
2
+ require 'auto_timezone/config'
3
+ require 'auto_timezone/controller'
4
+
5
+ module AutoTimezone
6
+ class Engine < ::Rails::Engine
7
+ end
8
+ end
9
+
10
+ ActionController::Base.send :include, AutoTimezone::Controller
@@ -0,0 +1,28 @@
1
+ module AutoTimezone
2
+ class Config
3
+ attr_accessor :preferred_timezones
4
+ attr_accessor :default_timezone_block
5
+
6
+ def initialize
7
+ set_defaults
8
+ end
9
+
10
+ def default_timezone(&block)
11
+ self.default_timezone_block = block
12
+ end
13
+
14
+ private
15
+
16
+ def set_defaults
17
+ self.preferred_timezones = %w(US/Eastern US/Central US/Mountain US/Pacific)
18
+ end
19
+ end
20
+
21
+ def self.configure(&block)
22
+ block.call(config)
23
+ end
24
+
25
+ def self.config
26
+ @config ||= Config.new
27
+ end
28
+ end
@@ -0,0 +1,15 @@
1
+ require_relative 'sundial'
2
+
3
+ module AutoTimezone
4
+ module Controller
5
+ def self.included(base)
6
+ base.prepend_around_filter :use_auto_timezone
7
+ end
8
+
9
+ private
10
+
11
+ def use_auto_timezone(&block)
12
+ Time.use_zone(Sundial.new(self).current_timezone, &block)
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,44 @@
1
+ module AutoTimezone
2
+ class Sundial
3
+ def initialize(ctx)
4
+ @ctx = ctx
5
+ return if !config.default_timezone_block || @ctx.respond_to?(:auto_timezone_default)
6
+ @ctx.define_singleton_method :auto_timezone_default, config.default_timezone_block
7
+ end
8
+
9
+ def current_timezone
10
+ @current_timezone ||= Time.find_zone(timezone_name)
11
+ end
12
+
13
+ def self.preferred_zones
14
+ @preferred_zones ||= config.preferred_timezones.map { |z| Time.find_zone(z) }
15
+ end
16
+
17
+ private
18
+
19
+ def timezone_name
20
+ cookies[:auto_timezone] ||= timezone_from_config_block
21
+ cookies[:auto_timezone] ||= timezone_from_offset
22
+ end
23
+
24
+ def timezone_from_config_block
25
+ @ctx.auto_timezone_default if @ctx.respond_to?(:auto_timezone_default)
26
+ end
27
+
28
+ def timezone_from_offset
29
+ find_zone_by_offset(cookies[:auto_timezone_offset].presence.try(:to_i)).try(:name)
30
+ end
31
+
32
+ def find_zone_by_offset(offset)
33
+ self.class.preferred_zones.find { |z| z.utc_offset == offset } || Time.find_zone(offset)
34
+ end
35
+
36
+ def cookies
37
+ @ctx.send(:cookies)
38
+ end
39
+
40
+ def config
41
+ AutoTimezone.config
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,3 @@
1
+ module AutoTimezone
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,12 @@
1
+ module AutoTimezone
2
+ module Generators
3
+ class InstallGenerator < Rails::Generators::Base
4
+ desc 'Copies auto_timezone config to your initializers.'
5
+ source_root File.expand_path('../templates', __FILE__)
6
+
7
+ def create_config_file
8
+ template 'config_initializer.rb', 'config/initializers/auto_timezone.rb'
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,15 @@
1
+ AutoTimezone.configure do |config|
2
+ # To allow users to customize their timezone (once logged-in), uncomment the following line
3
+ # and modify to support your desired method of retrieving a timezone:
4
+ #
5
+ # config.default_timezone do
6
+ # current_user.timezone
7
+ # end
8
+
9
+ # Because multiple named timezones fall under the same UTC offset, we have no way of knowing
10
+ # the exact timezone (by name). You may customize a list of "preferred" timezones if you have a
11
+ # general idea of where your users reside, within a specific UTC offset.
12
+ # The default config value is: ['US/Eastern', 'US/Central', 'US/Mountain', 'US/Pacific']
13
+ #
14
+ # config.preferred_timezones += %w(Hawaii Alaska)
15
+ end
@@ -0,0 +1,7 @@
1
+ Date.prototype.getDSTTimezoneOffset = function() {
2
+ var jan = new Date(this.getFullYear(), 0, 1);
3
+ var jul = new Date(this.getFullYear(), 6, 1);
4
+ return Math.max(jan.getTimezoneOffset(), jul.getTimezoneOffset());
5
+ }
6
+
7
+ document.cookie = "auto_timezone_offset=" + -60 * new Date().getDSTTimezoneOffset();
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: auto_timezone
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - smudge
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-06-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: railties
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ description: Automatically infer and set `Time.zone` with each web request.
56
+ email:
57
+ - nathan@ngriffith.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - auto_timezone.gemspec
68
+ - lib/auto_timezone.rb
69
+ - lib/auto_timezone/config.rb
70
+ - lib/auto_timezone/controller.rb
71
+ - lib/auto_timezone/sundial.rb
72
+ - lib/auto_timezone/version.rb
73
+ - lib/generators/auto_timezone/install_generator.rb
74
+ - lib/generators/auto_timezone/templates/config_initializer.rb
75
+ - vendor/assets/javascripts/auto_timezone.js
76
+ homepage: http://github.com/smudge/auto_timezone
77
+ licenses:
78
+ - MIT
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.2.2
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: Simple and unobtrusive timezone configuration for Rails
100
+ test_files: []
101
+ has_rdoc: