adzap / validates_timeliness

Date and time validation plugin for ActiveModel and Rails. Supports multiple ORMs and allows custom date/time formats.
MIT License
1.59k stars 227 forks source link

How to validate time between 20 minutes to 48 hours? #146

Closed haikalhussein closed 8 years ago

haikalhussein commented 8 years ago

I'm using rails 4 and the gem works. But now i got a situation whereby a user's time is between 20 minutes to 48 hours. Is it possible to validate ?

adzap commented 8 years ago

If I understand you correctly you probably want something like this

validate_datetime :some_attribute, on_or_after: proc {|r| 20.minutes.since(r.other_attribute) },
                                   on_or_before: proc {|r| 48.hours.since(r.other_attribute) }

Which depends on other_attribute being present of course. Is that what you are looking for?

haikalhussein commented 8 years ago

Sorry my bad. But my data type in psql is interval. Can the gem validates for column type interval, where it can store data like "48:00:00". Because currently the timespan does not require any specific end points.

adzap commented 8 years ago

no doesn't support intervals. I imagine you just validate intervals as a value object. I've not handled intervals in Rails, just directly in sql.