user_time_zones 0.1.0 → 0.1.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.
- checksums.yaml +4 -4
- data/.travis.yml +5 -0
- data/README.md +49 -9
- data/lib/generators/user_time_zones/install/install_generator.rb +13 -1
- data/lib/user_time_zones/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 63c040d850767d25e365889bd61d08a8d294ce2e
|
4
|
+
data.tar.gz: 827e347f78558d703189a9ccea451e4614697aa4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 859e1635bf09a51578e24e536ced576fb4bef2191a8fbfcb625d1b421ee00c30975f154bbd0e4588892438206459fd1421c9b9b9b93ac944a0fde0ff0d296c56
|
7
|
+
data.tar.gz: df2f3f787e7f59c62c87402fb7eb59d3a166f8b380edb9022c008bf991eee6f8ab3a7e6220a4d6220f4c38648838e0423174383da550e9c37e89d5296379d089
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -1,11 +1,22 @@
|
|
1
1
|
# UserTimeZones
|
2
2
|
|
3
|
-
UserTimeZones provides an
|
3
|
+
UserTimeZones provides an easy way to work with multiple user time zones.
|
4
4
|
|
5
|
-
UserTimeZones is
|
6
|
-
support for per-user time zones
|
5
|
+
UserTimeZones is small, simple, and tested. A large percentage of applications will require
|
6
|
+
support for per-user time zones; this gem can help. The solution is simple, you could implement it
|
7
7
|
yourself, but this library will let you implement the solution the same way, every time, without having
|
8
|
-
to expend
|
8
|
+
to expend much effort.
|
9
|
+
|
10
|
+
[](https://badge.fury.io/rb/user_time_zones)  
|
11
|
+
|
12
|
+
|
13
|
+
## Overview
|
14
|
+
|
15
|
+
Your `User` model gets a `time_zone` attribute to record the `ActiveSupport::TimeZone` for each user, and
|
16
|
+
methods to set/get the time zone as an offset (in hours) from `UTC`. Every controller action is executed in
|
17
|
+
the context of the user's time zone. And, finally, some javascript will help you initially calculate the
|
18
|
+
user's time zone when they sign up.
|
19
|
+
|
9
20
|
|
10
21
|
## Requirements
|
11
22
|
|
@@ -15,7 +26,7 @@ to expend excess effort.
|
|
15
26
|
|
16
27
|
## Install
|
17
28
|
|
18
|
-
To get started, add the
|
29
|
+
To get started, add the gem to your `Gemfile` and run `bundle install` to install it:
|
19
30
|
|
20
31
|
```ruby
|
21
32
|
gem 'user_time_zones'
|
@@ -36,7 +47,7 @@ The install generator will:
|
|
36
47
|
* Insert `include UserTimeZones::User` into your `User` model.
|
37
48
|
* Insert `include UserTimeZones::Controller` into your `ApplicationController`.
|
38
49
|
|
39
|
-
|
50
|
+
Include the javascript to calculate a new user's timezone in your `application.js`:
|
40
51
|
```javascript
|
41
52
|
//= require user_time_zones
|
42
53
|
```
|
@@ -92,15 +103,18 @@ You can allow the user to edit their time zone later on, in user#edit or elsewhe
|
|
92
103
|
|
93
104
|
### Applying the user's time zone
|
94
105
|
|
95
|
-
|
96
|
-
|
106
|
+
The `UserTimeZones::Controller` module provides an `around_action` that wraps each action a user takes, setting
|
107
|
+
the time zone to the user's `time_zone` attribute for the duration of the controller action.
|
108
|
+
|
109
|
+
`UserTimeZones::Controller` was inserted into your `ApplicationController` by the UserTimeZone's install generator.
|
110
|
+
Any controller that needs time zone support should have `include UserTimeZones::Controller`.
|
97
111
|
|
98
112
|
|
99
113
|
### The User model and time zones
|
100
114
|
|
101
115
|
UserTimeZones gives the user a new attribute, `time_zone`, and new methods `time_zone_offset` and `time_zone_offset=`.
|
102
116
|
The `time_zone` user attribute is the name of a `ActiveSupport::TimeZone`. The offset methods are the offset of
|
103
|
-
|
117
|
+
that time zone from `UTC`, in hours. Setting the `time_zone_offset` to a new value will change the user's `time_zone`.
|
104
118
|
|
105
119
|
Example:
|
106
120
|
```ruby
|
@@ -112,8 +126,34 @@ Example:
|
|
112
126
|
=> "Alaska"
|
113
127
|
>> user.time_zone_offset
|
114
128
|
=> -9
|
129
|
+
>> user.time_zone_offset = -7
|
130
|
+
=> -7
|
131
|
+
>> user.time_zone
|
132
|
+
=> "Arizona"
|
115
133
|
```
|
116
134
|
|
135
|
+
Your user model must `include UserTimeZones::User` into your user model. This is done for you by UserTimeZone's
|
136
|
+
install generator.
|
137
|
+
|
138
|
+
### Selecting a time zone
|
139
|
+
|
140
|
+
Typically a user is allowed to manually select their time zone in a `user#edit` action. Nothing special here,
|
141
|
+
just use the Rails form helper time_zone_select. For example:
|
142
|
+
```erbruby
|
143
|
+
<%= form_for @user do |f| %>
|
144
|
+
<!-- ... -->
|
145
|
+
<%= f.label :time_zone, 'Time Zone' %>
|
146
|
+
<%= f.time_zone_select :time_zone, ActiveSupport::TimeZone.all %>
|
147
|
+
<!-- ... -->
|
148
|
+
<% end %>
|
149
|
+
```
|
150
|
+
|
151
|
+
You can limit your user to U.S. time zones:
|
152
|
+
```erbruby
|
153
|
+
<%= f.time_zone_select :time_zone, ActiveSupport::TimeZone.us_zones %>
|
154
|
+
```
|
155
|
+
|
156
|
+
|
117
157
|
|
118
158
|
### A summary of do’s and don'ts with time zones
|
119
159
|
|
@@ -9,12 +9,19 @@ module UserTimeZones
|
|
9
9
|
include UserTimeZone::Generators::Helpers
|
10
10
|
|
11
11
|
source_root File.expand_path('../templates', __FILE__)
|
12
|
+
|
12
13
|
class_option :model,
|
13
14
|
optional: true,
|
14
15
|
type: :string,
|
15
16
|
banner: 'model',
|
16
17
|
desc: "Specify the model class name if you will use anything other than 'User'"
|
17
18
|
|
19
|
+
class_option :javascript,
|
20
|
+
type: :boolean,
|
21
|
+
aliases: '-j',
|
22
|
+
default: false,
|
23
|
+
desc: 'Install javascript "require" into application.js'
|
24
|
+
|
18
25
|
def initialize(*)
|
19
26
|
super
|
20
27
|
assign_names!(model_class_name)
|
@@ -45,7 +52,12 @@ module UserTimeZones
|
|
45
52
|
)
|
46
53
|
end
|
47
54
|
|
48
|
-
|
55
|
+
def inject_javascript
|
56
|
+
javascript = File.join('app', 'assets', 'javascripts', 'application.js')
|
57
|
+
if options.javascript && File.exist?(javascript)
|
58
|
+
append_to_file 'app/assets/javascripts/application.js', '//= require user_time_zones'
|
59
|
+
end
|
60
|
+
end
|
49
61
|
|
50
62
|
private
|
51
63
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: user_time_zones
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Justin Tomich
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-06-
|
11
|
+
date: 2017-06-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|