wiser_timezone 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NTQyN2U3NWM0NjAyY2VlYTNlYWMxM2Q4N2E1NGI5ZTBiMDg0YzNiMQ==
5
+ data.tar.gz: !binary |-
6
+ NmNmYjlhMTQyYWU2MGYyNGFkYTc5NTk0Y2FkZTc3YWUzMzJhODFmMg==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ Y2IxYTJjZDk0N2VlNWRjZGFkNmU5MGVlNGJiMDdkOWI2NWM2ODJkOWJiMjg1
10
+ M2Q5ZDI3OGMxZjcwZTVjNDQ3ZTFiNWZlOTZjNDAyNWY1NDliMzE5NDUyNGIx
11
+ ZDQ2MjJjZWEyOTBkMDRlNjcwZDhjODA2Zjg4YmM0ZmFjZThhZDM=
12
+ data.tar.gz: !binary |-
13
+ ODdjODQ5NDMyMGUxYWY1Yjc4YzkzMzhlYTdjZDM0YThlZmFmMmE1YWNjM2Nk
14
+ ZjUzNTViNDY4MDM3YzJhM2I3MDE3YjllMTNhMmQ3YjVhYTYxYjcwMWJjMjUx
15
+ YTA4NzdhNWI1YWJmY2Y0ZWRjYmUzZGUwYTc3NDI2YjU2ZjZjODA=
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in wiser_timezone.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Kenneth John Balgos
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.
data/README.md ADDED
@@ -0,0 +1,114 @@
1
+ # WiserTimezone
2
+
3
+ Friendly Timezone on Steroids
4
+
5
+ ## Requirements
6
+
7
+ _WiserTimezone_ only support Rails >= 3.2 with jQuery.
8
+
9
+ ## Setup
10
+
11
+ ### Gem Installation
12
+
13
+ You can do normal gem installation for `wiser_timezone` from your terminal:
14
+
15
+ gem install wiser_timezone
16
+
17
+ or add this line in your Gemfile:
18
+
19
+ gem 'wiser_timezone'
20
+
21
+ Be sure to restart your application if it is already running.
22
+
23
+ ### Database (optional)
24
+
25
+ **Note:**
26
+ Use the database migration **ONLY** if you have `users` database table and `current_user` helper method.
27
+
28
+ Generate a migration for wiser_timezone and run the database migration (in your Rails project):
29
+
30
+ rails g wiser_timezone:migration
31
+ rake db:migrate
32
+
33
+ ## Installation
34
+
35
+ ### Javascript
36
+
37
+ Include the `wiser_timezone` javascript into your `app/assets/javascripts/application.js`:
38
+
39
+ //= require jquery
40
+ //= require wiser_timezone
41
+
42
+ Reload using the `wiser_timezone_reload()` function from your javascript `ready` event:
43
+
44
+ $(document).ready(function(){
45
+ wiser_timezone_reload();
46
+ })
47
+
48
+ ### Turbolinks (optional)
49
+
50
+ Reload using the `wiser_timezone_reload()` function from your javascript `page:load` event:
51
+
52
+ $(document).on("page:load", function(){
53
+ wiser_timezone_reload();
54
+ })
55
+
56
+ ### Stylesheet
57
+
58
+ Include the `wiser_timezone` stylesheet into your `app/assets/stylesheets/application.css`:
59
+
60
+ *= require wiser_timezone
61
+
62
+ ### Layout
63
+
64
+ Render the `wiser_timezone_initialize` view below the `body` of your layout file:
65
+
66
+ <body>
67
+ <%= wiser_timezone_initialize %>
68
+ <!-- More Codes -->
69
+ </body>
70
+
71
+
72
+ ## Usage
73
+
74
+ All dates must be printed using the custom helper `wiser_timezone()` from the `wiser_timezone`.
75
+
76
+ ### Examples
77
+
78
+ You can do the normal usage:
79
+
80
+ <%= wiser_timezone(@post.created_at) %>
81
+ // Return: 2014-03-15 02:37:07 +0800
82
+
83
+ Or even format the value:
84
+
85
+ <%= wiser_timezone(@post.created_at).strftime('%Y-%m-%d %H:%M:%S') %>
86
+ // Return: 2014-03-15 02:37:07
87
+
88
+ ### Extra
89
+
90
+ You can get the current timezone using the `current_timezone` method:
91
+
92
+ My timezone is:
93
+ <%= current_timezone %>
94
+ // Return: (GMT+08:00) Beijing
95
+
96
+
97
+ ## Contributing
98
+
99
+ 1. Fork it
100
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
101
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
102
+ 4. Push to the branch (`git push origin my-new-feature`)
103
+ 5. Create new Pull Request
104
+
105
+ ## Support
106
+ Open an issue in https://github.com/kennethjohnbalgos/wiser_timezone if you need further support or want to report a bug.
107
+
108
+ ## License
109
+
110
+ The MIT License (MIT) Copyright (c) 2014 iKennOnline
111
+
112
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
113
+
114
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,20 @@
1
+ $(document).ready(function(){
2
+ wiser_timezone_reload();
3
+ });
4
+
5
+ var wiser_timezone_reload = function(){
6
+ if($('#wiser_timezone_container').is(':visible')){
7
+ browser_offset = (-(new Date()).getTimezoneOffset() / 60).toString();
8
+ setting_offset = $('#wiser_timezone_space').data('offset')
9
+
10
+
11
+ if(browser_offset != setting_offset){
12
+ $('body').css('margin-top', '50px')
13
+ $('#wiser_timezone_container').show()
14
+ url = $('#wiser_timezone_link').attr('href')
15
+ $('#wiser_timezone_link').attr('href', url + '?offset=' + encodeURIComponent(browser_offset))
16
+ }else{
17
+ $('#wiser_timezone_container').remove()
18
+ }
19
+ }
20
+ }
@@ -0,0 +1,30 @@
1
+ #wiser_timezone_container {
2
+ /* Position */
3
+ position: absolute;
4
+ top: 0;
5
+ left: 0;
6
+ z-index: 999;
7
+ /* Layout */
8
+ width: 100%;
9
+ }
10
+ #wiser_timezone_container #wiser_timezone_space {
11
+ /* Layout */
12
+ padding: 10px 15px;
13
+ /* Font */
14
+ font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;
15
+ color: #555;
16
+ /* Border */
17
+ border-bottom: 1px solid #3e5e71;
18
+ /* Background */
19
+ background: #f5f9ff;
20
+ }
21
+ #wiser_timezone_container #wiser_timezone_space a {
22
+ /* Font */
23
+ text-decoration: none;
24
+ color: #6fa8c9;
25
+ }
26
+ #wiser_timezone_container #wiser_timezone_space a:hover {
27
+ /* Font */
28
+ text-decoration: none;
29
+ color: #3e5e71;
30
+ }
@@ -0,0 +1,14 @@
1
+ module WiserTimezone
2
+ class WiserTimezoneController < ApplicationController
3
+ def set_timezone
4
+ if params[:offset].present?
5
+ if current_user.present? && current_user.try(:timezone).present?
6
+ current_user.update_attribute(:timezone, params[:offset])
7
+ else
8
+ cookies[:wiser_timezone_offset] = params[:offset]
9
+ end
10
+ end
11
+ redirect_to :back
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,42 @@
1
+ module WiserTimezone
2
+ module WiserTimezoneHelper
3
+
4
+ def current_timezone
5
+ return ActiveSupport::TimeZone[offset.to_i]
6
+ end
7
+
8
+ def wiser_timezone(date)
9
+ begin
10
+ date_time = date.to_datetime
11
+ return date_time.in_time_zone(current_timezone)
12
+ rescue
13
+ raise "WiserTimezone can only accept date object."
14
+ end
15
+ end
16
+
17
+ def wiser_timezone_initialize
18
+ link = link_to('Click Here', set_timezone_path, :method => 'post', :data => {:offset => "#{offset}"}, :id => 'wiser_timezone_link')
19
+ if offset.present?
20
+ msg = "Your computer's timezone does not appear to match the current setting #{current_timezone}. #{link} to update the timezone."
21
+ else
22
+ msg = "You do not have timezone in your user setting. #{link} to save your time zone."
23
+ end
24
+ space = "<div id='wiser_timezone_space' data-offset='#{offset}'>#{msg}</div>"
25
+ html = "<div id='wiser_timezone_container' style='display:none;'>#{space}</div>"
26
+ return html.html_safe
27
+ end
28
+
29
+ private
30
+
31
+ def offset
32
+ if current_user.present? && current_user.try(:timezone).present?
33
+ offset = current_user.timezone
34
+ elsif cookies[:wiser_timezone_offset].present?
35
+ offset = cookies[:wiser_timezone_offset]
36
+ else
37
+ offset = 0
38
+ end
39
+ return offset.to_i
40
+ end
41
+ end
42
+ end
data/config/routes.rb ADDED
@@ -0,0 +1,3 @@
1
+ Rails.application.routes.draw do
2
+ post 'set_timezone' => 'wiser_timezone/wiser_timezone#set_timezone', :as => 'set_timezone'
3
+ end
@@ -0,0 +1,17 @@
1
+ require 'generators/wiser_timezone'
2
+ require 'rails/generators/active_record'
3
+
4
+ module WiserTimezone
5
+ module Generators
6
+ # Migration generator that creates migration file from template
7
+ class MigrationGenerator < ActiveRecord::Generators::Base
8
+ extend Base
9
+
10
+ argument :name, :type => :string, :default => 'add_wiser_timezone_field'
11
+ # Create migration in project's folder
12
+ def generate_files
13
+ migration_template 'migration.rb', "db/migrate/#{name}"
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,11 @@
1
+ class AddWiserTimezoneField < ActiveRecord::Migration
2
+
3
+ def self.up
4
+ add_column :users, :timezone, :string
5
+ end
6
+
7
+ def self.down
8
+ remove_column :users, :timezone
9
+ end
10
+
11
+ end
@@ -0,0 +1,12 @@
1
+ require 'rails/generators/named_base'
2
+
3
+ module WiserTimezone
4
+ module Generators
5
+ module Base
6
+ # Get path for migration template
7
+ def source_root
8
+ @_wiser_trails_source_root ||= File.expand_path(File.join('../wiser_timezone', generator_name, 'templates'), __FILE__)
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,7 @@
1
+ module WiserTimezone
2
+ class Engine < Rails::Engine
3
+ end
4
+
5
+ class Railtie < Rails::Railtie
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module WiserTimezone
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,7 @@
1
+ require 'wiser_timezone/version'
2
+
3
+ module WiserTimezone
4
+
5
+ end
6
+
7
+ require 'wiser_timezone/engine'
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'wiser_timezone/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "wiser_timezone"
8
+ spec.version = WiserTimezone::VERSION
9
+ spec.authors = ["Kenneth John Balgos"]
10
+ spec.email = ["kennethjohnbalgos@gmail.com"]
11
+ spec.description = ""
12
+ spec.summary = "Friendly Timezone on Steroids"
13
+ spec.homepage = "https://github.com/kennethjohnbalgos/wiser_timezone"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wiser_timezone
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Kenneth John Balgos
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: ''
42
+ email:
43
+ - kennethjohnbalgos@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - .gitignore
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - app/assets/javascripts/wiser_timezone.js
54
+ - app/assets/stylesheets/wiser_timezone.css
55
+ - app/controllers/wiser_timezone/wiser_timezone_controller.rb
56
+ - app/helpers/wiser_timezone/wiser_timezone_helper.rb
57
+ - config/routes.rb
58
+ - lib/generators/wiser_timezone.rb
59
+ - lib/generators/wiser_timezone/migration/migration_generator.rb
60
+ - lib/generators/wiser_timezone/migration/templates/migration.rb
61
+ - lib/wiser_timezone.rb
62
+ - lib/wiser_timezone/engine.rb
63
+ - lib/wiser_timezone/version.rb
64
+ - wiser_timezone.gemspec
65
+ homepage: https://github.com/kennethjohnbalgos/wiser_timezone
66
+ licenses:
67
+ - MIT
68
+ metadata: {}
69
+ post_install_message:
70
+ rdoc_options: []
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ requirements: []
84
+ rubyforge_project:
85
+ rubygems_version: 2.0.3
86
+ signing_key:
87
+ specification_version: 4
88
+ summary: Friendly Timezone on Steroids
89
+ test_files: []