date_parity 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,10 +1,53 @@
1
- # DateParity
1
+ # Date Parity
2
2
 
3
- Consistent date formatting and parsing for Rails applications. Creates parity between date.to\_s and
4
- string.to\_date in Rails applications.
3
+ Date formatting and parsing per request in Rails with parity between date.to\_s.and string.to\_date.
5
4
 
6
- This is implemented with a "Date.format" syntax and usage, similar to "Time.zone" in rails. This can be
7
- set per request (i.e. per user) in a controller before or around filter.
5
+ Rails already has some great Date formatting and parsing options available. For example, Date.DATE\_FORMATS makes it easy to configure and reuse common formats for your application. But, other then the :default it require an explicit field.to\_s(:format) to be applied. Additionally, nothing happens automatically when parsing those dates back from user edits. This generally require some explicit steps per field in your model or controller.
6
+
7
+ Rails also provides the ability to localize your Date formats. But, that requires decisions made for all users in a particular locale.
8
+
9
+ Often, we need to allow users, companies, organizations, etc. the ability to choose preferred date formats. Both of the Rails standard approaches using Date.DATE\_FORMATS or localization fall short at making that easy. You end up writing a lot helpers around date fields, and code to normalize posted date values.
10
+
11
+ This is where date\_parity helps. It is implemented by setting a `Date.format` string, similar to a per user `Time.zone` in Rails. This can be set per request (i.e. per user) in a controller's before\_filter or around\_filter.
12
+
13
+ For example:
14
+ ```ruby
15
+ Date.format = "%m/%d/%Y"
16
+ date = "12/31/2012".to_date
17
+ => Mon, 31 Dec 2012
18
+ date.to_s
19
+ => "12/31/2012"
20
+
21
+ Date.format = "%d/%m/%Y"
22
+ date = "31/12/2012".to_date
23
+ => Mon, 31 Dec 2012
24
+ date.to_s
25
+ => "31/12/2012"
26
+
27
+ Date.format = '%Y-%m-%d'
28
+ date = "2012-12-31".to_date
29
+ => Mon, 31 Dec 2012
30
+ date.to_s
31
+ => "2012-12-31"
32
+
33
+ Date.format = '%d.%m.%Y'
34
+ date = "31.12.2012".to_date
35
+ => Mon, 31 Dec 2012
36
+ date.to_s
37
+ => "31.12.2012"
38
+
39
+ Date.format = '%Y.%m.%d'
40
+ date = "2012.12.31".to_date
41
+ => Mon, 31 Dec 2012
42
+ date.to_s
43
+ => "2012.12.31"
44
+
45
+ Date.format = '%Y/%m/%d'
46
+ date = "2012/12/31".to_date
47
+ => Mon, 31 Dec 2012
48
+ date.to_s
49
+ => "2012/12/31"
50
+ ```
8
51
 
9
52
  ## Installation
10
53
 
@@ -22,41 +65,37 @@ Or install it yourself as:
22
65
 
23
66
  ## Usage
24
67
 
25
- In Rails controller before\_filter:
26
- Date.format = '%m/%d/%Y'
27
-
28
- or from a persisted field that stores the desired format:
29
- Date.format = current\_user.date\_format
30
-
31
- In any view assuming @model\_created\_on is a date, and Date.format
32
- was set as above:
33
- <%=@modele.created\_on %>
34
- => 12/31/2012
35
-
36
- And then you get parity when a form with this field is posted.
37
- Rails will attempt to convert the posted string value of "12/31/2012"
38
- using Date.\_parse, which DateParity overrides and respects your Date.format.
39
- Without DateParity gem, @model.start\_on would have ended up nil because
40
- "12/31/2012" is invalid argument Ruby Date.\_parse. However, when date\_parity is
41
- included, Date will use the format string stored on Date.format
42
- (if it exist) to correctly parse "12/31/2012", as December 31, 2012, when
43
- Date.format = "%m/%d/%Y"
44
-
45
- Note: Rails text\_field form helper uses @model.attribute\before\_type\_cast by default.
46
- When using date\_parity on plain text fields, you will probably want to set your value
47
- to explicitly use @model.attribute if it exists. Otherwise, you won't see the formatted
48
- date field:
49
- For example:
50
- <%= f.text_field :created_on, :value => @user.created\_on || @user.created\_on\_before\_type\_cast %>
51
-
52
- Date.format can be set to any date format that is parsable by strptime.
53
- For example, the following are all valid:<br>
54
- Date.format = "%m/%d/%Y"<br>
55
- Date.format = "%d/%m/%Y"<br>
56
- Date.format = '%Y-%m-%d'<br>
57
- Date.format = '%d.%m.%Y'<br>
58
- Date.format = '%Y.%m.%d'<br>
59
- Date.format = '%Y/%m/%d'<br>
68
+ In a Rails controller before\_filter:
69
+ ```Date.format = '%m/%d/%Y'```
70
+
71
+ Generally, you will want to set that string based on user preference, as in:
72
+ ```Date.format = current_user.date_format```
73
+
74
+ Then in any view assuming @model\_started\_on is a date field, and Date.format was set as above to '%m/%d/%Y':
75
+ ```ruby
76
+ <%=@modele.started_on %>
77
+ #=> '12/31/2012'
78
+ ```
79
+
80
+ And then you get parity when a form with this field is posted. Rails will attempt to convert the posted string value of "12/31/2012" using Date.\_parse, which DateParity overrides and respects your Date.format.
81
+
82
+ Without the date\_parity gem, @model.start\_on would have ended up nil because "12/31/2012" is an invalid argument for ruby Date.\_parse. However, when date\_parity is included, Date will use the format string set on Date.format to correctly parse "12/31/2012", as December 31, 2012, when Date.format = "%m/%d/%Y".
83
+
84
+ Note: Rails text\_field form helper uses ```@model.attribute_before_type_cast``` by default. When using date\_parity on plain text fields, you will probably want to set your value to explicitly use @model.attribute if it exists. Otherwise, you won't see the formatted date field: For example:
85
+ ```ruby
86
+ <%= f.text_field :start_on, :value => @user.started_on || @user.started_on_before_type_cast %>
87
+ ```
88
+
89
+ Date.format can be set to any date format that can be parsed by strptime.
90
+ For example, the following are all valid:
91
+ ```ruby
92
+ Date.format = "%m/%d/%Y"
93
+ Date.format = "%d/%m/%Y"
94
+ Date.format = '%Y-%m-%d'
95
+ Date.format = '%d.%m.%Y'
96
+ Date.format = '%Y.%m.%d'
97
+ Date.format = '%Y/%m/%d'
98
+ ```
60
99
 
61
100
  ## Contributing
62
101
 
data/date_parity.gemspec CHANGED
@@ -7,8 +7,8 @@ Gem::Specification.new do |gem|
7
7
 
8
8
  gem.author = 'Scott Stewart'
9
9
  gem.email = 'scottpstewart@gamil.com'
10
- gem.description = 'Consistent date formatting done simple'
11
- gem.summary = 'Consistent date formatting done simple'
10
+ gem.description = 'Rails Date formatting and parsing with full parity between date.to_s.and string.to_date per request.'
11
+ gem.summary = 'Rails Date formatting and parsing with full parity between date.to_s.and string.to_date per request.'
12
12
  gem.homepage = ""
13
13
 
14
14
  gem.add_dependency 'activesupport', '>= 3.2'
@@ -1,3 +1,3 @@
1
1
  module DateParity
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: date_parity
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-13 00:00:00.000000000 Z
12
+ date: 2012-08-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
16
- requirement: &70261701192120 !ruby/object:Gem::Requirement
16
+ requirement: &70323438102960 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '3.2'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70261701192120
24
+ version_requirements: *70323438102960
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rake
27
- requirement: &70261701191700 !ruby/object:Gem::Requirement
27
+ requirement: &70323438102540 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70261701191700
35
+ version_requirements: *70323438102540
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: minitest
38
- requirement: &70261701191240 !ruby/object:Gem::Requirement
38
+ requirement: &70323438102080 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,8 +43,9 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70261701191240
47
- description: Consistent date formatting done simple
46
+ version_requirements: *70323438102080
47
+ description: Rails Date formatting and parsing with full parity between date.to_s.and
48
+ string.to_date per request.
48
49
  email: scottpstewart@gamil.com
49
50
  executables: []
50
51
  extensions: []
@@ -75,7 +76,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
75
76
  version: '0'
76
77
  segments:
77
78
  - 0
78
- hash: 2984063008747520348
79
+ hash: -101513963914939075
79
80
  required_rubygems_version: !ruby/object:Gem::Requirement
80
81
  none: false
81
82
  requirements:
@@ -84,13 +85,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
84
85
  version: '0'
85
86
  segments:
86
87
  - 0
87
- hash: 2984063008747520348
88
+ hash: -101513963914939075
88
89
  requirements: []
89
90
  rubyforge_project:
90
91
  rubygems_version: 1.8.11
91
92
  signing_key:
92
93
  specification_version: 3
93
- summary: Consistent date formatting done simple
94
+ summary: Rails Date formatting and parsing with full parity between date.to_s.and
95
+ string.to_date per request.
94
96
  test_files:
95
97
  - test/date_parity_test.rb
96
98
  - test/lib/date_parity/test_helper.rb