Skip to content
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

Merged
merged 1 commit into from
Nov 24, 2015
Merged

Don't try to parse empty dates #939

merged 1 commit into from
Nov 24, 2015

Conversation

awead
Copy link
Contributor

@awead awead commented Nov 4, 2015

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...

@awead awead changed the title Don't try to parse nil dates Don't try to parse empty dates Nov 4, 2015
@awead
Copy link
Contributor Author

awead commented Nov 4, 2015

Gah! Cut and pasted the wrong commit message...

@awead
Copy link
Contributor Author

awead commented Nov 4, 2015

Now I'm getting issues with calling empty? on FixNum. I'll update this... suggestions welcome.

@awead awead force-pushed the empty-dates branch 3 times, most recently from 55d04fc to 6588da3 Compare November 4, 2015 02:02
@awead
Copy link
Contributor Author

awead commented Nov 4, 2015

Ok, got this sorted.

@awead awead removed the in progress label Nov 5, 2015
@ojlyytinen
Copy link
Contributor

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

@awead
Copy link
Contributor Author

awead commented Nov 7, 2015

@ojlyytinen Good point! It's been amended.

@jcoyne
Copy link
Member

jcoyne commented Nov 7, 2015

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 DateTime and sometimes a string (e.g. "circa 1840").

@ojlyytinen
Copy link
Contributor

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

@awead
Copy link
Contributor Author

awead commented Nov 11, 2015

Why not just override adapt_single_attribute_value in your model? Or my original solution was to just return the value if it wasn't "datable." That would solve @jcoyne's problem of DateTime vs. String. In any case, we ought to merge something. I'm still having to pin to this branch because of the original parsing of empty values.

awead added a commit to aic-collections/aicdams-lakeshore that referenced this pull request Nov 12, 2015
@ojlyytinen
Copy link
Contributor

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.

@awead
Copy link
Contributor Author

awead commented Nov 24, 2015

@ojlyytinen @jcoyne how do we feel about this? Are we okay merging?

@ojlyytinen
Copy link
Contributor

I think this is fine to go in as is.

jcoyne added a commit that referenced this pull request Nov 24, 2015
@jcoyne jcoyne merged commit ca8bd86 into master Nov 24, 2015
@awead awead deleted the empty-dates branch November 30, 2015 16:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants