date_parity 0.0.3 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +2 -0
- data/README.md +9 -6
- data/date_parity.gemspec +1 -1
- data/lib/date_parity/core_ext/date.rb +6 -6
- data/lib/date_parity/version.rb +1 -1
- data/test/date_parity_test.rb +31 -14
- metadata +33 -24
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -2,13 +2,13 @@
|
|
2
2
|
|
3
3
|
Date formatting and parsing per request in Rails with parity between date.to\_s.and string.to\_date.
|
4
4
|
|
5
|
-
Rails
|
5
|
+
Rails has some great Date formatting and parsing options available. But, it still requires a lot of helping code to consistently parse and display dates in a particular format . 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
6
|
|
7
7
|
Rails also provides the ability to localize your Date formats. But, that requires decisions made for all users in a particular locale.
|
8
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.
|
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; especially when parsing user form posted date strings. You end up writing a lot helpers around date fields, and code to normalize posted date values.
|
10
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\
|
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\_action or around\_action.
|
12
12
|
|
13
13
|
For example:
|
14
14
|
```ruby
|
@@ -51,6 +51,9 @@ date.to_s
|
|
51
51
|
|
52
52
|
## Installation
|
53
53
|
|
54
|
+
Date Parity >= 0.1.0 works with Rails 4.0 onwards.
|
55
|
+
Date Parity 0.0.3 works with Rails 3.
|
56
|
+
|
54
57
|
Add this line to your application's Gemfile:
|
55
58
|
|
56
59
|
gem 'date_parity'
|
@@ -65,7 +68,7 @@ Or install it yourself as:
|
|
65
68
|
|
66
69
|
## Usage
|
67
70
|
|
68
|
-
In a Rails controller before\
|
71
|
+
In a Rails controller before\_action:
|
69
72
|
```Date.format = '%m/%d/%Y'```
|
70
73
|
|
71
74
|
Generally, you will want to set that string based on user preference, as in:
|
@@ -77,9 +80,9 @@ Then in any view assuming @model\_started\_on is a date field, and Date.format w
|
|
77
80
|
#=> '12/31/2012'
|
78
81
|
```
|
79
82
|
|
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.\
|
83
|
+
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
84
|
|
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.\
|
85
|
+
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
86
|
|
84
87
|
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
88
|
```ruby
|
data/date_parity.gemspec
CHANGED
@@ -11,7 +11,7 @@ Gem::Specification.new do |gem|
|
|
11
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
|
-
gem.add_dependency 'activesupport', '>=
|
14
|
+
gem.add_dependency 'activesupport', '>= 4.0.0'
|
15
15
|
|
16
16
|
gem.add_development_dependency 'rake'
|
17
17
|
gem.add_development_dependency 'minitest'
|
@@ -7,16 +7,16 @@ class Date
|
|
7
7
|
Thread.current[:date_format] = format
|
8
8
|
end
|
9
9
|
|
10
|
-
def
|
11
|
-
return
|
10
|
+
def parse_with_date_parity_format(string, comp=false)
|
11
|
+
return parse_without_date_parity_format(string, comp) if string.blank? or Date.format.blank?
|
12
12
|
# normalize date by first respecting custom date_parity Date.format,
|
13
13
|
# then formatting back to iso/db string for normal _parse call.
|
14
|
-
|
15
|
-
rescue
|
16
|
-
|
14
|
+
parse_without_date_parity_format(Date.strptime(string, Date.format).to_s(:db), comp)
|
15
|
+
#rescue
|
16
|
+
#parse_without_date_parity_format(string, comp)
|
17
17
|
end
|
18
18
|
|
19
|
-
alias_method_chain :
|
19
|
+
alias_method_chain :parse, :date_parity_format
|
20
20
|
end
|
21
21
|
|
22
22
|
end
|
data/lib/date_parity/version.rb
CHANGED
data/test/date_parity_test.rb
CHANGED
@@ -1,10 +1,15 @@
|
|
1
1
|
require 'date_parity/test_helper'
|
2
|
+
require 'debugger'
|
2
3
|
|
3
4
|
class DateParityTest < MiniTest::Unit::TestCase
|
4
5
|
|
6
|
+
def setup
|
7
|
+
# Reset Date.format from previous test
|
8
|
+
Date.format = nil
|
9
|
+
end
|
10
|
+
|
5
11
|
def test_date_to_string_respects_format
|
6
12
|
date = Date.parse("2012-08-12")
|
7
|
-
|
8
13
|
Date.format = "%m/%d/%Y"
|
9
14
|
assert_equal "08/12/2012", date.to_s
|
10
15
|
|
@@ -29,44 +34,56 @@ class DateParityTest < MiniTest::Unit::TestCase
|
|
29
34
|
date_hash = { :year => 2012, :mon => 8, :mday => 12 }
|
30
35
|
|
31
36
|
Date.format = "%m/%d/%Y"
|
32
|
-
assert_equal date_hash, Date.
|
37
|
+
assert_equal date_hash[:mday], Date.parse("08/12/2012").mday
|
38
|
+
assert_equal date_hash[:mon], Date.parse("08/12/2012").mon
|
39
|
+
assert_equal date_hash[:year], Date.parse("08/12/2012").year
|
33
40
|
|
34
41
|
Date.format = "%d/%m/%Y"
|
35
|
-
assert_equal date_hash, Date.
|
42
|
+
assert_equal date_hash[:mday], Date.parse("12/08/2012").mday
|
43
|
+
assert_equal date_hash[:mon], Date.parse("12/08/2012").mon
|
44
|
+
assert_equal date_hash[:year], Date.parse("12/08/2012").year
|
36
45
|
|
37
46
|
Date.format = '%Y-%m-%d'
|
38
|
-
assert_equal date_hash, Date.
|
47
|
+
assert_equal date_hash[:mday], Date.parse("2012-08-12").mday
|
48
|
+
assert_equal date_hash[:mon], Date.parse("2012-08-12").mon
|
49
|
+
assert_equal date_hash[:year], Date.parse("2012-08-12").year
|
39
50
|
|
40
51
|
Date.format = '%d.%m.%Y'
|
41
|
-
assert_equal date_hash, Date.
|
52
|
+
assert_equal date_hash[:mday], Date.parse("12.08.2012").mday
|
53
|
+
assert_equal date_hash[:mon], Date.parse("12.08.2012").mon
|
54
|
+
assert_equal date_hash[:year], Date.parse("12.08.2012").year
|
42
55
|
|
43
56
|
Date.format = '%Y.%m.%d'
|
44
|
-
assert_equal date_hash, Date.
|
57
|
+
assert_equal date_hash[:mday], Date.parse("2012.08.12").mday
|
58
|
+
assert_equal date_hash[:mon], Date.parse("2012.08.12").mon
|
59
|
+
assert_equal date_hash[:year], Date.parse("2012.08.12").year
|
45
60
|
|
46
61
|
Date.format = '%Y/%m/%d'
|
47
|
-
assert_equal date_hash, Date.
|
62
|
+
assert_equal date_hash[:mday], Date.parse("2012/08/12").mday
|
63
|
+
assert_equal date_hash[:mon], Date.parse("2012/08/12").mon
|
64
|
+
assert_equal date_hash[:year], Date.parse("2012/08/12").year
|
48
65
|
end
|
49
66
|
|
50
67
|
def test_string_to_date_respects_format
|
51
68
|
# This works because Rails ActiveSupport provides
|
52
69
|
# string.to_date, which uses Date._parse
|
53
70
|
Date.format = "%m/%d/%Y"
|
54
|
-
assert_equal Date.
|
71
|
+
assert_equal Date.parse_without_date_parity_format("2012-08-12"), "08/12/2012".to_date
|
55
72
|
|
56
73
|
Date.format = "%d/%m/%Y"
|
57
|
-
assert_equal Date.
|
74
|
+
assert_equal Date.parse_without_date_parity_format("2012-08-12"), "12/08/2012".to_date
|
58
75
|
|
59
76
|
Date.format = '%Y-%m-%d'
|
60
|
-
assert_equal Date.
|
77
|
+
assert_equal Date.parse_without_date_parity_format("2012-08-12"), "2012-08-12".to_date
|
61
78
|
|
62
79
|
Date.format = '%d.%m.%Y'
|
63
|
-
assert_equal Date.
|
80
|
+
assert_equal Date.parse_without_date_parity_format("2012-08-12"), "12.08.2012".to_date
|
64
81
|
|
65
82
|
Date.format = '%Y.%m.%d'
|
66
|
-
assert_equal Date.
|
83
|
+
assert_equal Date.parse_without_date_parity_format("2012-08-12"), "2012.08.12".to_date
|
67
84
|
|
68
85
|
Date.format = '%Y/%m/%d'
|
69
|
-
assert_equal Date.
|
86
|
+
assert_equal Date.parse_without_date_parity_format("2012-08-12"), "2012/08/12".to_date
|
70
87
|
end
|
71
88
|
|
72
89
|
def test_invalid_date_still_raises_argument_error
|
@@ -80,7 +97,7 @@ class DateParityTest < MiniTest::Unit::TestCase
|
|
80
97
|
|
81
98
|
def full_date_parity
|
82
99
|
Date.format = "%m/%d/%Y"
|
83
|
-
date = Date.
|
100
|
+
date = Date.parse_without_date_parity_format("2012-12-31")
|
84
101
|
assert date, date.to_s.to_date
|
85
102
|
|
86
103
|
assert "12/31/2012", "12/31/2012".to_date.to_s
|
metadata
CHANGED
@@ -1,49 +1,64 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: date_parity
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.3
|
5
4
|
prerelease:
|
5
|
+
version: 0.1.0
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Scott Stewart
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2013-06-30 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
|
-
|
16
|
-
requirement: &70323438102960 !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
15
|
+
version_requirements: !ruby/object:Gem::Requirement
|
18
16
|
requirements:
|
19
17
|
- - ! '>='
|
20
18
|
- !ruby/object:Gem::Version
|
21
|
-
version:
|
19
|
+
version: 4.0.0
|
20
|
+
none: false
|
21
|
+
name: activesupport
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
24
|
+
requirement: !ruby/object:Gem::Requirement
|
25
|
+
requirements:
|
26
|
+
- - ! '>='
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
version: 4.0.0
|
28
29
|
none: false
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
version_requirements: !ruby/object:Gem::Requirement
|
29
32
|
requirements:
|
30
33
|
- - ! '>='
|
31
34
|
- !ruby/object:Gem::Version
|
32
35
|
version: '0'
|
36
|
+
none: false
|
37
|
+
name: rake
|
33
38
|
type: :development
|
34
39
|
prerelease: false
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
40
|
+
requirement: !ruby/object:Gem::Requirement
|
41
|
+
requirements:
|
42
|
+
- - ! '>='
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: '0'
|
39
45
|
none: false
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
version_requirements: !ruby/object:Gem::Requirement
|
40
48
|
requirements:
|
41
49
|
- - ! '>='
|
42
50
|
- !ruby/object:Gem::Version
|
43
51
|
version: '0'
|
52
|
+
none: false
|
53
|
+
name: minitest
|
44
54
|
type: :development
|
45
55
|
prerelease: false
|
46
|
-
|
56
|
+
requirement: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ! '>='
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
none: false
|
47
62
|
description: Rails Date formatting and parsing with full parity between date.to_s.and
|
48
63
|
string.to_date per request.
|
49
64
|
email: scottpstewart@gamil.com
|
@@ -69,26 +84,20 @@ rdoc_options: []
|
|
69
84
|
require_paths:
|
70
85
|
- lib
|
71
86
|
required_ruby_version: !ruby/object:Gem::Requirement
|
72
|
-
none: false
|
73
87
|
requirements:
|
74
88
|
- - ! '>='
|
75
89
|
- !ruby/object:Gem::Version
|
76
90
|
version: '0'
|
77
|
-
segments:
|
78
|
-
- 0
|
79
|
-
hash: -101513963914939075
|
80
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
81
91
|
none: false
|
92
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
82
93
|
requirements:
|
83
94
|
- - ! '>='
|
84
95
|
- !ruby/object:Gem::Version
|
85
96
|
version: '0'
|
86
|
-
|
87
|
-
- 0
|
88
|
-
hash: -101513963914939075
|
97
|
+
none: false
|
89
98
|
requirements: []
|
90
99
|
rubyforge_project:
|
91
|
-
rubygems_version: 1.8.
|
100
|
+
rubygems_version: 1.8.23
|
92
101
|
signing_key:
|
93
102
|
specification_version: 3
|
94
103
|
summary: Rails Date formatting and parsing with full parity between date.to_s.and
|