Skip to content

Commit

Permalink
Handle invalid DateTime attributes gracefully
Browse files Browse the repository at this point in the history
The Date typecaster will gracefully handle invalid dates, but DateTime
does not. This changes the behavior of DateTime types to match the
behavior of Date types.
  • Loading branch information
brianstien authored and liveh2o committed Feb 28, 2018
1 parent d1a1b04 commit 56f27d2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/active_remote/typecasting/date_time_typecaster.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module Typecasting
class DateTimeTypecaster
def self.call(value)
value.to_datetime if value.respond_to?(:to_datetime)
rescue NoMethodError, ArgumentError
end
end
end
Expand Down
7 changes: 7 additions & 0 deletions spec/lib/active_remote/typecasting_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@
record = test_class.new(:birthday => "2016-01-01")
expect(record.birthday).to eq(DateTime.parse("2016-01-01"))
end

context "invalid date" do
it "sets attribute to nil" do
record = test_class.new(:birthday => "23451234")
expect(record.birthday).to be_nil
end
end
end

describe "float" do
Expand Down

0 comments on commit 56f27d2

Please sign in to comment.