ActiveRecord date field with non-english text - ruby-on-rails

I have a form submitting to a controller action with a field that is in the "%B %d, %Y" date format. Our site has recently added multi-language support and now this is breaking when we translate the text in the field to non-english.
Example in ENGLISH: (This works as expected and populates the field with a date)
#model.datetime_field = "May 01, 2016"
Example in GERMAN. (This will not assign the field with a date)
#model.datetime_field = "Mai 01, 2016"
I've looked into the ActiveRecord source code and also at a few gems (like Alchemy) and I haven't found this problem to be solved. Please don't suggest that I change the form value, because due to the nature of the application/business requirements it needs to stay as is. Also to note, I need the solution to work for mass-assignment, e.g. #model.save(params[:model]) and #model.update(params[:model])
Does anyone know if there is something in Rails/rails-18n/3rd-party gem that solves this problem or do I have to write my own custom parser?
Thanks in advance for your suggestions.

This gem looks like it does what you want:
https://github.com/clemens/delocalize

Related

zf2 validator - valid only if date is a weekday

Using ZF2 \Zend\Validate, is it possible to validate a date only if it is a weekday? So weekends, regardless of month or year, will fail the validation?
The input is a normal text field.
I don't think that Zend\Validate has already a validator to check that.
You'll probably need to write your own custom validator to do your check.
Have a look at this answer to see how to perform the actual check (actually that answer check the converse, but it'll be easy to adapt it)

Displaying a different date than is submitted rails datetime_field

I have several datetime_field in my application. By default, they are displaying the date as the rails format, as I dictated that for my bootstrap-datepicker:
format: 'YYYY-MM-DD HH:mm:ss'
The issue: I want the date on the views to display in a different format, say:
'MM-DD-YYYY HH:mm:ss'
I've been scratching my head for a while on this one. Every change I make to the view side, be it by Javascript, jQuery, rails methods, helpers, whatever - they all affect the submit data, which then becomes invalid, because Rails expects the first format.
I don't want to change the default date format - we have other aspects of the website that need the database in the normal rails format. I just want to change the display of the date in the datetime_field, without changing the submitted date.
My guess would be it was an option in the datetime_field, but I can't seem to find one:
<%= f.datetime_field(:end_date, {?????})%>
Any suggestions on how to accomplish this?

Grails Timepicker

I need to save Date Time in the (oracle) database in one column, which is sqlType of timestamp (looks like 01-JAN-14 12.00.00.000000 AM). While learning grails I've been using the Joda lib with it's "time picker".
The Joda timepicker has worked well, but now that I'm looking to go primetime I'm looking for something a little more user friendly. Frankly, text boxes might be more user friendly than the drops downs joda gives you.
Anyway, I'd like to remove joda and use something like this:
http://trentrichardson.com/examples/timepicker/
but I can't figure out how to implement it in grails. In my view, if I put:
<input type="text" name="endDate" id="endDate" value="${exampleInstance?.endDate}" />
in place of the g:datePicker, it works fine (the picker that is), except nothing gets saved to the database, and no errors are generated. I hit Save and the Show view comes up with an empty endDate field. Do I need more input tags?
Is there some easy way to implement a modern looking date+time picker that I've missed?
Furthermore, I see there is a plugin for this picker here
http://grails.org/plugin/jquery-ui-timepicker
But being that there isn't any documentation, I'm not sure how to use that either (?)
ANSWER
in controller save/update put something like:
def endDate = params.date('endDate', 'yy-MM-dd h:mm')
//println "Date from Picker was "+endDate
params.endDate = endDate
No further casting was necessary being that it ended up I could format the datepicker control to a very close format as what's in the database, but had I needed to cast from one odd format, or a string, to another, I toyed with this code, which is more psuedo than anything as I was thinking through the process (I'm sure there's a totally Groovy way to do this same thing):
SimpleDateFormat inputFormat = new SimpleDateFormat("yy-MM-dd HH:mm:ss.S");
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yy h.mm.ss.S a");
def v = params.endDate
Date date = inputFormat.parse(v);
String temp = sdf.format(date)
Date dateOut = sdf.parse(temp)
println dateOut
The datepicker, is your UI component therefore, you can have any library that you wish for UI and anything else for back-end. Mostly they are easy to implement, if they provide a little bit of documentation!!.
The timepicker for jQuery ui plugin, that you provided the link, is exposing a resource called jqueryUiTimePicker which depends on jQuery and jQuery-ui. So simply by including this resource into you resources configuration you should be able to utilize it. Its no different than defining your own resource and use it.
About saving issue that you have, on your save pass parameter failOnError:true so you can see the errors if any.
I have created a sample project that utilizes this plugin hope it helps
In your controller, you will need to parse the parameter value to a Date value. Something like,
def endDate = params.date('endDate', 'dd-MM-yyyy')
dd-MM-yyyy is whatever format the jquery plugin submits the date value. (println params for this or look up the plugin documentation)
If you want a date format binding to happen automatically, check the Date Formats For Data Binding in the doc http://grails.org/doc/latest/guide/single.html#dataBinding for a way to globally specify the format

x-editable with meteor textarea no line breaks and date timezone issue

I'm currently trying to work in bootstrap x-editable to my meteor application. I'm using the atmosphere package for this: https://github.com/nate-strauser/meteor-x-editable-bootstrap. I'm having a couple of issue so far which are:
When I select a date using the date data-type I get a javascript date object back that is 4 hours behind what I actually picked(assuming this is because I'm in -4 timezone).
When I edit a textarea, the line breaks are saved to the database, but when bring up the editable to edit it the line breaks are striped.
It looks like this might be the intended behavior. Its hard to be sure without more information.
With the date, javascript stores date in unixtime. This is because its very easy to switch timezones and not worry about having javascript itself having to keep track of DST and the other complications of keeping time.
If you use new Date(<the javascript timestamp>); you should get the time in your timezone.
With the textarea it looks like some kind of text-encoding conversion is taking place. You should check to see what the character codes of those stripes are and convert them to newlines like \n. One scenario this could occur is if you're copy-pasting stuff with a different encoding into the textarea.

Turn data from a datetimepicker (human readable) into Time class (machine readable)

Right now I'm using a datetimepicker to make it easy for users to select an exact date/time and present that in a readable format. However, rails does not like that format and can't Time.parse it.
I've tried out the Chronic gem, and it's close, but it still can't parse something like:
02/27/2013 08:36:57 PM
How can I make user inputted time into an acceptable format for my database? (mongo)
I'm using this gem: https://github.com/asgeo1/bootstrap-datetimepicker-rails
based on this datetimepicker: https://github.com/tarruda/bootstrap-datetimepicker
demonstrated here: http://tarruda.github.com/bootstrap-datetimepicker/
Check out the timeliness gem:
https://github.com/adzap/timeliness
The date picker will always return the value in a standard format, so you can parse it.
#02/27/2013 08:36:57 PM
month = input[0..1]
date = input[3..4]
year = input[6..9]
datetime = DateTime.civil(input[6..9], input[0..1], input[3..4],
input[11..12], input[14..15], input[17..18])

Resources