has_safe_dates 0.0.5 → 0.0.6

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1706f8d6cdbbd3d98e0fd78162be5d1a7e85c4f7
4
- data.tar.gz: 5caf2882827bb4717238fa46fd553a18df1148e7
3
+ metadata.gz: 7ad3c88881117642f2beceb579a7518c23a44706
4
+ data.tar.gz: a9fe9e4210ba6b11acd35136f2a228694b58cb80
5
5
  SHA512:
6
- metadata.gz: 9bff2a615d7ce86c75221117ed9ee75067bc62bd88c2fbd871ed6ff0e44ab31619cb707d78e1284ec3940e1192b38cccd0e32f57db2dd28d1fe092d59cc0fe12
7
- data.tar.gz: 2fc82cd206c61761404981f6a73b98c757dabeaae97d2b84f9e688f0c0949655f8f54e3a7aeb659b93c368ce58b9fd3096ed492a49aeecf7b5d136ccad2c95b0
6
+ metadata.gz: 6a754711a149aded1183c1159fe55b7144b83411a920b9d149aca8ff50e2779efc9ce9c8f1accf8cb4f8385ae0e3d3a56b9d693d64f973d4f6d7729e6f08555f
7
+ data.tar.gz: 892c6860de80a36e91f0e4a53dca0c380b74cfa4cef32fde58e729135b121c764f5e2620e9318ad5950a3ec31ae618a21d7be6fd8ab2b0eee35be6cfb05944d5
data/Gemfile CHANGED
@@ -2,4 +2,6 @@ source "http://rubygems.org"
2
2
 
3
3
  gemspec
4
4
 
5
- # gem 'ruby-debug19'
5
+ # gem 'ruby-debug19'
6
+
7
+ gem "byebug"
@@ -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
 
@@ -1,3 +1,3 @@
1
1
  module HasSafeDates
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
data/spec/db/schema.rb CHANGED
@@ -7,6 +7,7 @@ ActiveRecord::Schema.define(:version => 0) do
7
7
  t.date "approved_at"
8
8
  t.date "created_at"
9
9
  t.date "updated_at"
10
+ t.datetime "published_at"
10
11
  end
11
12
 
12
13
  create_table "posts", :force => true do |t|
@@ -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)' => "12", 'approved_at(3i)' => "abc"}
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)' => "2001", 'published_date(2i)' => "12", 'published_date(3i)' => "abc"}
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: has_safe_dates
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - kylejginavan