has_safe_dates 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +3 -1
- data/lib/has_safe_dates/core_ext.rb +10 -0
- data/lib/has_safe_dates/version.rb +1 -1
- data/spec/db/schema.rb +1 -0
- data/spec/has_safe_dates_spec.rb +34 -3
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7ad3c88881117642f2beceb579a7518c23a44706
|
4
|
+
data.tar.gz: a9fe9e4210ba6b11acd35136f2a228694b58cb80
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6a754711a149aded1183c1159fe55b7144b83411a920b9d149aca8ff50e2779efc9ce9c8f1accf8cb4f8385ae0e3d3a56b9d693d64f973d4f6d7729e6f08555f
|
7
|
+
data.tar.gz: 892c6860de80a36e91f0e4a53dca0c380b74cfa4cef32fde58e729135b121c764f5e2620e9318ad5950a3ec31ae618a21d7be6fd8ab2b0eee35be6cfb05944d5
|
data/Gemfile
CHANGED
@@ -64,6 +64,16 @@ module HasSafeDates
|
|
64
64
|
super # has_safe_dates is not enabled for the current field, so invoke the super method (original #read_date method).
|
65
65
|
end
|
66
66
|
end
|
67
|
+
|
68
|
+
# Overrides #read_time when has_safe_dates is enabled for the current field the multiparameter.
|
69
|
+
# Otherwise the original #read_date method is invoked.
|
70
|
+
def read_time
|
71
|
+
if ActiveRecord::Base.has_safe_fields_config[object.class.base_class] && ActiveRecord::Base.has_safe_fields_config[object.class.base_class][:fields].include?(name)
|
72
|
+
"#{ values.values_at(1,2,3).join("-")} #{ values.values_at(4,5).join(":") }" # Convert multiparameter parts into a Time string, e.g. "2011-4-23 12:34", return it, and allow CoreExt methods handle the result.
|
73
|
+
else
|
74
|
+
super # has_safe_dates is not enabled for the current field, so invoke the super method (original #read_time method).
|
75
|
+
end
|
76
|
+
end
|
67
77
|
end
|
68
78
|
end
|
69
79
|
|
data/spec/db/schema.rb
CHANGED
data/spec/has_safe_dates_spec.rb
CHANGED
@@ -7,7 +7,7 @@ class Post < ActiveRecord::Base
|
|
7
7
|
end
|
8
8
|
|
9
9
|
class Comment < ActiveRecord::Base
|
10
|
-
has_safe_dates :approved_at, :error_message => 'is not a real date'
|
10
|
+
has_safe_dates :approved_at, :published_at, :error_message => 'is not a real date'
|
11
11
|
end
|
12
12
|
|
13
13
|
describe "HasSafeDates" do
|
@@ -76,7 +76,7 @@ describe "HasSafeDates" do
|
|
76
76
|
|
77
77
|
it "doesn't blow up when given an incorrect values" do
|
78
78
|
invalid_post_attributes = {'published_date(1i)' => "2001", 'published_date(2i)' => "12", 'published_date(3i)' => "abc"}
|
79
|
-
invalid_comment_attributes = {'approved_at(1i)' => "2001", 'approved_at(2i)' => "
|
79
|
+
invalid_comment_attributes = {'approved_at(1i)' => "2001", 'approved_at(2i)' => "15", 'approved_at(3i)' => "17"}
|
80
80
|
expect {
|
81
81
|
@post.update_attributes(invalid_post_attributes)
|
82
82
|
@comment.update_attributes(invalid_comment_attributes)
|
@@ -91,11 +91,42 @@ describe "HasSafeDates" do
|
|
91
91
|
end
|
92
92
|
|
93
93
|
it "adds an error when Chronic returns nil" do
|
94
|
-
invalid_post_attributes = {'published_date(1i)' => "
|
94
|
+
invalid_post_attributes = {'published_date(1i)' => "2014", 'published_date(2i)' => "12", 'published_date(3i)' => "abc"}
|
95
95
|
@post.update_attributes(invalid_post_attributes)
|
96
96
|
@post.errors[:published_date].should == ['is not a real date']
|
97
97
|
end
|
98
98
|
|
99
|
+
context "with time" do
|
100
|
+
it "works" do
|
101
|
+
invalid_comment_attributes = {'published_at(1i)' => "2014", 'published_at(2i)' => "12", 'published_at(3i)' => "22", 'published_at(4i)' => "12", 'published_at(5i)' => "34"}
|
102
|
+
expect {
|
103
|
+
@comment.update_attributes(invalid_comment_attributes)
|
104
|
+
}.to_not raise_error
|
105
|
+
expect(@comment.published_at.utc.strftime("%FT%T%:z")).to eq(Time.new(2014, 12, 22, 12, 34).utc.strftime("%FT%T%:z"))
|
106
|
+
end
|
107
|
+
|
108
|
+
it "doesn't blow up when given an incorrect values" do
|
109
|
+
invalid_post_attributes = {'published_date(1i)' => "2001", 'published_date(2i)' => "12", 'published_date(3i)' => "17", 'published_date(4i)' => "12", 'published_date(5i)' => "34"}
|
110
|
+
invalid_comment_attributes = {'published_at(1i)' => "2001", 'published_at(2i)' => "15", 'published_at(3i)' => "17", 'published_at(4i)' => "12", 'published_at(5i)' => "34"}
|
111
|
+
expect {
|
112
|
+
@post.update_attributes(invalid_post_attributes)
|
113
|
+
@comment.update_attributes(invalid_comment_attributes)
|
114
|
+
}.to_not raise_error
|
115
|
+
end
|
116
|
+
|
117
|
+
it "does not interfere with a date column that it has not been told to make safe" do
|
118
|
+
invalid_attributes = {'unsafe_date(1i)' => "2011", 'unsafe_date(2i)' => "9", 'unsafe_date(3i)' => "83", 'unsafe_date(4i)' => "12", 'unsafe_date(5i)' => "34"}
|
119
|
+
expect {
|
120
|
+
@post.update_attributes(invalid_attributes)
|
121
|
+
}.to raise_error(ActiveRecord::MultiparameterAssignmentErrors)
|
122
|
+
end
|
123
|
+
|
124
|
+
it "adds an error when Chronic returns nil" do
|
125
|
+
invalid_comment_attributes = {'published_at(1i)' => "2009", 'published_at(2i)' => "12", 'published_at(3i)' => "90", 'published_at(4i)' => "12", 'published_at(5i)' => "34"}
|
126
|
+
@comment.update_attributes(invalid_comment_attributes)
|
127
|
+
@comment.errors[:published_at].should == ['is not a real date']
|
128
|
+
end
|
129
|
+
end
|
99
130
|
end
|
100
131
|
|
101
132
|
end
|