has_calendar

has_calendar is a view helper that creates a calendar using a table. You can easily add events with any content.

This plugin relies on the cal command, available on *nix systems. Instead of calculating dates, I parse the output.

NOTE: Only on Mac OS X (Unix?) the first day of week is Sunday; Linux (tested on Ubuntu) uses the argument -m, starting on Monday.

Want this to work on Windows systems? Make sure you have a command cal on your PATH that returns exactly the *nix output (haven't tested though).

Instalation

  1. Install the plugin with script/plugin install git://github.com/fnando/has_calendar.git

Usage

<%= calendar :year => 2008, :month => 9 %>

or, if you want to register some events:

<% calendar :year => 2008, :month => 9 do |date| %>
    <% for event in Schedule.find_by_date(date) %>
        <%= link_to event.title, event_path(event) %>
    <% end %>
<% end %>

As you can see, this will hit your database up to 31 times (one hit for each day) if you don't optimize it. Fortunately, you can use the options :events:

<% calendar :events => Schedule.all do |date, events| %>
    <% for event in events %>
        <%= link_to event.title, event_path(event) %>
    <% end %>
<% end %>

By default, each record will use the created_at attribute as date grouping. You can specify a different attribute with the option :field:

<% calendar :events => Schedule.all, :field => :scheduled_at do |date, events| %>
    <!-- do something -->
<% end %>

By default, has_calendar will look for a date format called :day\_of\_week to use on the header of the calendar, you can change it using the :header_format option:

    <%= calendar :header_format => :short_day_name %>

You can also change the caption provided by has_calendar (that defaults to the :default format):

    <%= calendar :header_format => :short_day_name, :caption_format => :month_year %>

To set formats do something like this:

Date::DATE_FORMATS[:short_day_name] = '%a' # => (Sun..Sat)

Or on your locale file, if your application is internationalized.

You can set the HTML id: <%= calendar :id => 'cal' %>

Formatting the calendar

You can use this CSS to start:

#calendar {
    border-collapse: collapse;
    width: 100%;
}

#calendar td,
#calendar th {
    color: #ccc;
    font-family: "Lucida Grande",arial,helvetica,sans-serif;
    font-size: 10px;
    padding: 6px;
}

#calendar th {
    border: 1px solid #ccc;
    background: #ccc;
    color: #666;
    text-align: left;
}

#calendar td {
    background: #f0f0f0;
    border: 1px solid #ddd;
}

#calendar span {
    display: block;
}

#calendar td.events {
    background: #fff;
}

#calendar td.today {
    background: #ffc;
    color: #666;
}

#calendar caption {
  display: none;
}

TO-DO

  • Write some specs as soon as possible!

Contributors

Copyright (c) 2008 Nando Vieira, released under the MIT license



has_calendar

has_calendar是使用表创建日历的视图助手。您可以 轻松添加任何内容的事件。

此插件依赖于* nix系统上提供的 cal 命令。代替 计算日期,我解析输出。

注意:只有在Mac OS X(Unix?)上,星期几是星期天; Linux(测试 在Ubuntu上)使用参数 -m ,从星期一开始。

希望这可以在Windows系统上工作?确保你有一个命令 cal 你的PATH会返回* nix输出(尚未测试)。

Instalation

  1. Install the plugin with script/plugin install git://github.com/fnando/has_calendar.git

用法

<%= calendar :year => 2008, :month => 9 %>

或者,如果你想注册一些事件:

<% calendar :year => 2008, :month => 9 do |date| %>
    <% for event in Schedule.find_by_date(date) %>
        <%= link_to event.title, event_path(event) %>
    <% end %>
<% end %>

如您所见,这将会触发您的数据库达31次(每次命中一次) 天),如果你不优化它。幸运的是,您可以使用选项:events

<% calendar :events => Schedule.all do |date, events| %>
    <% for event in events %>
        <%= link_to event.title, event_path(event) %>
    <% end %>
<% end %>

默认情况下,每个记录将使用 created_at 属性作为日期分组。 您可以使用选项:field 指定不同的属性:

<% calendar :events => Schedule.all, :field => :scheduled_at do |date, events| %>
    <!– do something –>
<% end %>

By default, has_calendar will look for a date format called :day_of_week to use on the header of the calendar, you can change it using the :header_format option:

    <%= calendar :header_format => :short_day_name %>

您还可以更改has_calendar提供的标题(默认为 :默认格式):

    <%= calendar :header_format => :short_day_name, :caption_format => :month_year %>

要设置格式,请执行以下操作:

Date::DATE_FORMATS[:short_day_name] = '%a' # => (Sun..Sat)

或您的区域设置文件,如果您的应用程序是国际化的。

您可以设置HTML标识: &lt;%= calendar:id =&gt; ‘cal’%&gt;

格式化日历

您可以使用此CSS开始:

#calendar {
    border-collapse: collapse;
    width: 100%;
}

#calendar td, #calendar th { color: #ccc; font-family: "Lucida Grande",arial,helvetica,sans-serif; font-size: 10px; padding: 6px; }

#calendar th { border: 1px solid #ccc; background: #ccc; color: #666; text-align: left; }

#calendar td { background: #f0f0f0; border: 1px solid #ddd; }

#calendar span { display: block; }

#calendar td.events { background: #fff; }

#calendar td.today { background: #ffc; color: #666; }

#calendar caption { display: none; }

TO-DO

  • 尽快写一些规格!

贡献者

版权所有(c)2008 Nando Vieira,根据麻省理工学院许可证发布




相关问题推荐