-
Notifications
You must be signed in to change notification settings - Fork 63
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Don't try to parse empty dates #939
Conversation
Gah! Cut and pasted the wrong commit message... |
Now I'm getting issues with calling |
55d04fc
to
6588da3
Compare
Ok, got this sorted. |
In the case of an empty string value for something that should be a date, I think adapt_single_attribute_value should probably return nil. What I hoped to do with the pull request that introduced this bit of code was that date attributes would consistently be DateTimes, or nils if they're not present, rather than sometimes also be strings. This still leaves the possibility that adapt_single_attribute_value might encounter a value that should be a date and is a non-empty string that's not parseable as a date. I think raising an exception is the right thing to do in this case. So I would change this to something like def adapt_single_attribute_value(value, attribute_name)
if date_attribute?(attribute_name)
return nil unless value.present?
DateTime.parse(value)
else
value
end
end |
@ojlyytinen Good point! It's been amended. |
Why don't we store dates in the profile json serialized to iso8601, then we can just see if the field looks like a data and then parse it? That would avoid a lot of these problems which I'm encountering too. We have a date field that is sometimes a |
I wouldn't return a date only on the basis that something looks like a date. That could lead to unexpectedly getting DateTimes where you expect a String. For example, "101-001" is a valid iso8601 date. Even ignoring unusual, but valid, dates someone could put just a date in a field meant for textual notes which could then unexpectedly come back as a DateTime. I think it might be a good idea to have some kind of easy way to parse any field with a user supplied method. Then anyone could add any kind of parsing behaviour they wanted. Could we have something similar to serialize in ActiveRecord? Then you could do something along these lines. class Foo < ActiveFedora::Base
property :test, predicate: ::RDF::URI.new('http://www.example.com/test')
serialize :test, SomeClass
end
class SomeClass
def self.dump(obj)
# serialize obj
end
def self.load(data)
# deserialize data
end
end |
Why not just override |
As far as I'm concerned, I think this PR is fine as it is. Overriding adapt_single_attribute_value works if anyone needs special parsing behaviour. |
@ojlyytinen @jcoyne how do we feel about this? Are we okay merging? |
I think this is fine to go in as is. |
I'm getting
invalid date
when I have a date property that's empty:''
Not sure if this is a valid use case or not, but I'm getting it in the context of a Sufia app. Maybe I need to rectify this at the form/presenter level instead? Otherwise, here's the PR...