Permalink
Browse files

Removed ActiveRecord dependency; specify the attribute name with the …

…option :field.
  • Loading branch information...
fnando committed Sep 18, 2008
1 parent 134cea6 commit 000f44e43424723867a3137859506cff1ba1e05e
Showing with 20 additions and 45 deletions.
  1. +11 −12 README.markdown
  2. +0 −1 init.rb
  3. +9 −32 lib/has_calendar.rb
View
@@ -32,25 +32,24 @@ or, if you want to register some events:
<% 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 ActiveRecord method
-`has_calendar`:
-
- class Schedule < ActiveRecord::Base
- has_calendar :created_at
- end
-
-This call inserts a new method `to_calendar` that gets the attribute
-`created_at` and converts it using the `to_s(:number)` date method. This method
-is used to group all records that are passed as the option `:events`. So,
-instead of receiving a date, you receive a date plus all the records for that
-specific day. Your view will look like this:
+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 %>
+
+Formatting the calendar
+-----------------------
+
You can use this CSS to start:
#calendar {
View
@@ -1,5 +1,4 @@
require File.dirname(__FILE__) + "/lib/has_calendar"
require "stringio"
-ActiveRecord::Base.send :include, SimplesIdeias::Calendar::ActiveRecord
ActionView::Base.send :include, SimplesIdeias::Calendar::ActionView
View
@@ -1,38 +1,13 @@
module SimplesIdeias
module Calendar
- module ActiveRecord
- def self.included(base)
- base.extend ClassMethods
-
- class << base
- attr_accessor :has_calendar_options
- end
- end
-
- module ClassMethods
- def has_calendar(attribute = :created_at)
- include SimplesIdeias::Calendar::ActiveRecord::InstanceMethods
-
- self.has_calendar_options = {
- :attribute => attribute
- }
- end
- end
-
- module InstanceMethods
- def to_calendar
- @to_calendar ||= send(self.class.has_calendar_options[:attribute]).to_date.to_s(:number)
- end
- end
- end
-
module ActionView
def calendar(options={}, &block)
options = {
:year => Date.today.year,
:month => Date.today.month,
:today => 'Today',
- :events => nil
+ :events => nil,
+ :field => :created_at
}.merge(options)
cmd = 'cal '
@@ -56,13 +31,15 @@ def calendar(options={}, &block)
end
# group all records if data is provided
- records = {}
-
if options[:events]
- options[:events].each do |record|
- records[record.to_calendar] ||= []
- records[record.to_calendar] << record
+ records = options[:events].inject({}) do |memo, record|
+ stamp = record.send(options[:field]).to_date.to_s(:number)
+ memo[stamp] ||= []
+ memo[stamp] << record
+ memo
end
+
+ debugger
end
# building the calendar

0 comments on commit 000f44e

Please sign in to comment.