Consider expressions that cannot be type cast to be invalid #37

Merged
merged 1 commit into from Mar 29, 2013

Conversation

Projects
None yet
4 participants
@robworley
Contributor

robworley commented Feb 28, 2013

Useful for ActiveRecord models where string a value is assigned to using Model#attributes=. ActiveRecord will try to type cast the string to a date. When the type cast fails because the string cannot be parsed, it assigns nil to the attribute.

Prior to this change if you specified :allow_nil => true or :allow_blank => true it would incorrectly consider the attribute to be valid. I added some logic to check the value before type cast, taking care to preserve backwards compatibility.

It should resolve issue #25. Hope it helps.

@robworley

This comment has been minimized.

Show comment Hide comment
@robworley

robworley Mar 4, 2013

Contributor

The travis build succeeded for the most part, but there's a problem with rbx-2.0. It looks like a travis-specific issue, rather than anything code-related. Maybe someone with rights could try a rebuild?

Contributor

robworley commented Mar 4, 2013

The travis build succeeded for the most part, but there's a problem with rbx-2.0. It looks like a travis-specific issue, rather than anything code-related. Maybe someone with rights could try a rebuild?

@amnesia7

This comment has been minimized.

Show comment Hide comment
@amnesia7

amnesia7 Mar 4, 2013

@robworley, does this allow for virtual attributes as well as actual database-driven attributes?

I have a virtual date attribute in my form which I don't insert directly into a date field in my db. I'm not sure whether or not virtual attributes have _before_type_cast values or not but I don't think so.

Thanks

amnesia7 commented Mar 4, 2013

@robworley, does this allow for virtual attributes as well as actual database-driven attributes?

I have a virtual date attribute in my form which I don't insert directly into a date field in my db. I'm not sure whether or not virtual attributes have _before_type_cast values or not but I don't think so.

Thanks

@robworley

This comment has been minimized.

Show comment Hide comment
@robworley

robworley Mar 21, 2013

Contributor

@amnesia7 sorry for the delayed response.

It's true that virtual ActiveRecord attributes don't have _before_type_cast methods. This change only calls _before_type_cast methods for persistent attributes, so in that sense it's fully backwards compatible.

Contributor

robworley commented Mar 21, 2013

@amnesia7 sorry for the delayed response.

It's true that virtual ActiveRecord attributes don't have _before_type_cast methods. This change only calls _before_type_cast methods for persistent attributes, so in that sense it's fully backwards compatible.

@korny

This comment has been minimized.

Show comment Hide comment
@korny

korny Mar 28, 2013

Works great for us, thank you!

korny commented Mar 28, 2013

Works great for us, thank you!

@robworley

This comment has been minimized.

Show comment Hide comment
@robworley

robworley Mar 28, 2013

Contributor

@oriolgual, @txus is this PR of interest to you? Anything I need to add or change?

Contributor

robworley commented Mar 28, 2013

@oriolgual, @txus is this PR of interest to you? Anything I need to add or change?

@oriolgual

This comment has been minimized.

Show comment Hide comment
@oriolgual

oriolgual Mar 29, 2013

Member

Sorry for the late response. Seems OK to me!

Member

oriolgual commented Mar 29, 2013

Sorry for the late response. Seems OK to me!

oriolgual added a commit that referenced this pull request Mar 29, 2013

Merge pull request #37 from robworley/invalid-values-before-type-cast
Consider expressions that cannot be type cast to be invalid

@oriolgual oriolgual merged commit 26a6462 into codegram:master Mar 29, 2013

1 check failed

default The Travis build could not complete due to an error
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment