August 11th, 2007
cpetersen
Introduction
We posted the
ActiveCalendar project a couple of months ago (incidentally it was the first post on this blog as well). Since then, the response has been great. It seems like people are actually using it. For that, thank you, we're very happy people have found this code useful. Some of you have been kind enough to post comments and questions. In response, we've been making incremental changes (hopefully improvements) to the code base. Rather than continue to detail them in the comments of
the original post, I decided roll them up and describe them here.
validates_as_date
The plugin originally left the text fields uneditable, so the only way to edit the date was through the calendar. It became clear that this wasn't ideal. Not only is it sometimes quicker to type your date out, as Art pointed out, once you selected a date, there was no way to blank it out. To alleviate this problem, we simply made the text fields editable. However, this create another potential problem, invalid dates. This is where validates_as_date was born.
To validate your dates, add the following code to your model:
|
validates_as_date :date_field |
That's it! validates_as_date is a slight adaptation of the code snippet by Stuart Rackham found
here.
Add Dynarch Parameters
The
Dynarch Calendar is very flexible... up until recently our plugin was not. Many of you wanted to access the flexibility, but were unable to. We recently checked in some code that allows you to pass options to the date_select and datetime_select methods and have the options passed into Dynarch's Calendar.
For instance, Mike Figley wanted to use the date status function, which allows you to specify some dates as special. Now you can, so using the example from Dynarch's documentation, you could now use ActiveCalendar to highlight certain dates
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
<style>
.special { background-color: #000; color: #fff; }
</style>
<script>
// this table holds your special days, so that we can automatize
// things a bit:
var SPECIAL_DAYS = {
0 : [ 13, 24 ], // special days in January
2 : [ 1, 6, 8, 12, 18 ], // special days in March
8 : [ 21, 11 ], // special days in September
11 : [ 25, 28 ] // special days in December
};
// this function returns true if the passed date is special
function dateIsSpecial(year, month, day) {
var m = SPECIAL_DAYS[month];
if (!m) return false;
for (var i in m) if (m[i] == day) return true;
return false;
}
// this is the actual date status handler. Note that it receives the
// date object as well as separate values of year, month and date, for
// your confort.
function dateStatusHandler(date, y, m, d) {
if (dateIsSpecial(y, m, d)) return "special"
else return false;
// return true above if you want to disable other dates
}
</script>
<%= f.date_select :arrived_on, :dateStatusFunc => "dateStatusHandler" %> |
Notice the last line, now any extra options are passed directly to the Calendar.setup method.
Included in the list of possible options you can pass is ifFormat, which is the date format ActiveCalendar uses. Uma Shankar Ladha wanted to be able to just select a month and year, I'm not sure if ActiveCalendar is the best way to do that, but you could use the following code to make sure that no matter what date the user selects, your program would only get the month and year:
|
<%= f.date_select :arrived_on, :ifFormat => "%m/%Y" %> |
That format is used by both Ruby and Javascript for displaying dates, so you have to make sure that your format is parsable by both Ruby and the Calendar Javascript. Luckily their formats are extremely similar. The following list contains all the tags recognizable by both:
%a abbreviated weekday name
%A full weekday name
%b abbreviated month name
%B full month name
%d the day of the month ( 00 .. 31 )
%H hour ( 00 .. 23 )
%I hour ( 01 .. 12 )
%j day of the year ( 000 .. 366 )
%m month ( 01 .. 12 )
%M minute ( 00 .. 59 )
%n a newline character
%p �PM� or �AM�
%S second ( 00 .. 59 )
%U, %W the week number
%w the day of the week ( 0 .. 6, 0 = SUN )
%y year without the century ( 00 .. 99 )
%Y year including the century ( ex. 1979 )
%% a literal % character
In case you are interested the Ruby formats you
can't use are:
%x - Preferred representation for the date alone, no time
%X - Preferred representation for the time alone, no date
%Z - Time zone name
And the Dynarch codes you
can't use are:
%C century number
%e the day of the month ( 0 .. 31 )
%k hour ( 0 .. 23 )
%l hour ( 1 .. 12 )
%P �pm� or �am�
%s number of seconds since Epoch (since Jan 01 1970 00:00:00 UTC)
%t a tab character
%V the week number5
%u the day of the week ( 1 .. 7, 1 = MON )
Also note that if you change the formatting, validates_as_date will not work properly. I plan on fixing that in the future.
Overridable IDs
The last change worth mentioning (there have been others) was contributed by Peer Allen. He was using ActiveCalendar in conjunction with AJAX and needed to override the id property. His code allows the programmer to specify the name and id of the html entries used by ActiveCalendar. Thanks Peer!
Conclusion
Thank you all for using and contributing to ActiveCalendar. Please let us know how you're using it and what problems you've encountered.
August 12th, 2007 at 01:01 PM The updates are great and I have another one for you. I wanted to use the date selector for a reporting system I am creating. So I added an override to the "select_date" method in ActionView::Helpers. I did it really quickly this evening and I didn't do the datetime part yet, Its too big to put here so if you want a patch file drop me a line. Thanks (again)!
August 13th, 2007 at 05:41 AM Minor bug fix : When trying to edit a record with a date column in the rails console, I am getting an error while saving a valid date. Exemple :
>> person = Person.find :first >> p.birthday => # >> p.birthday.to_s => "2007-06-21" >> p.valid? => false >> person.errors[:birthday] => "is an invalid date"The problem seems to be with the console only, because I am able to use the plugin in the browser... If somebody else encounters this problem, here's how to patch it :# File : vendor/plugins/activecalendar/lib/assay_depot/calendar.rb # # Delete (or comment) line 182 to 207, and add these lines : # result = nil begin d = Date.parse(string) dt = DateTime.parse(string) result = (d == dt) ? d : dt rescue raise ArgumentError end resultSeptember 5th, 2007 at 12:45 PM Hi, ActiveCalendar is great and worked very well with ActiveScaffold. The only things I cannot work out are how to remove time portion when edit the record in ActiveScaffold and also how to format the date in %d%m%Y format in ActiveScaffold. Thanks!
October 4th, 2007 at 07:52 AM calendar.rb appears to generate IDs w/ invalid chars from time to time: Here is a patch: Index: lib/assay_depot/calendar.rb =================================================================== --- lib/assay_depot/calendar.rb (revision 999) +++ lib/assay_depot/calendar.rb (working copy) @@ -9,7 +9,9 @@ if(options[:id].blank?) options[:id] = options[:name] end - + + options[:id] = options[:id].gsub /[^-_0-9a-zA-Z]/, '-' + options[:ifFormat] ||= "%m/%d/%Y" if value(object) == nil
October 6th, 2007 at 09:43 AM Hi Jeffry, The time portion is added automatically for fields of type datetime. As for the format problem, did you try adding <macro:code lang="ruby"> :ifFormat => "%d/%m/%Y" </macro:code> Chris
January 30th, 2008 at 04:54 AM Peer Allan: could you email me a copy of your select_date patch, please? randrews at geekfu dot org. Thanks!
March 22nd, 2008 at 04:16 PM good work man dogfuck 72848 dragonballxxx lnmly crazysextaxi 95204 chinasex 28979 duckyporn mrdja bornpornstars fbyz familyporn tplfc bigdickshotchicks 92310 drunksex 303932 babygotboobs =-DDD