One topic missing from the documentation is how searchlogic handles Dates, but the functionality is in there. Here is how you use it.
You treat a date effectively as a number and you can use 'equals', 'gte', 'lte', etc. as comparison operators.
Assuming you have a column named 'date', of type 'date' in your model, you can put something like this in your form:
<%= f.label :date, "Date" %>
<%= f.text_field :date_gte, :size => 12 %> -
<%= f.text_field :date_lte, :size => 12 %>
You can then enter dates into one or both of the fields to retrieve records that fall into that date range.
But what date format should you use? This is where it gets clever. It wants to use YYYY-MM-DD format but if you enter MM/DD/YYYY it accepts is and converts it for you. Brilliant! It even takes text like 'Dec 1 2009' and converts that. Interestingly it won't accept invalid dates like '2009-11-31' and just gives you back the form with the field cleared out.
With a column of type 'Datetime' it converts your entries as well but this time it converts '2010-01-01' into 'Fri Jan 01 00:00:00 -0800 2010'. I'm sure it does something similar with columns of type 'Time'.
Undoubtedly the code is leveraging the Ruby time and date libraries to make the conversions, which can be lifesavers. Incorporating the functionality into searchlogic makes your forms much more tolerant of user inputs and it does so in a totally unobtrusive way.
No comments:
Post a Comment