What we're listening to:
Jared

The Album Leaf:
Into The Blue Again
Jeff

Paper Route:
Absence

Event Calendar Rails Plugin

July 23rd, 2009

For a recent project we needed to show events on a calendar. The existing Rails plugins we tried didn’t allow us to satisfactorily show multiple, overlapping events across calendar days and rows.

I found the same complaint here and began to adapt it to our needs.

I’ve extracted the result and put it on github in the hopes that others might find it useful and/or add improvements.

http://github.com/elevation/event_calendar

The above link has more details, as well as how to install and use the plugin.

Here’s the 1000 word screenshot:

Event Calendar Screenshot

Using the use_all_day option:

Event Calendar using all day option

PLEASE:

Thanks and enjoy!

  • Clark Richey

    Does this require rails 2.x? I’m running trying to use it on a legacy site that is rails 1.2 and I get this error when trying to generate the calendar:

    You have a nil object when you didn’t expect it! (NoMethodError)

  • Clark Richey

    Does this require rails 2.x? I’m running trying to use it on a legacy site that is rails 1.2 and I get this error when trying to generate the calendar:

    You have a nil object when you didn’t expect it! (NoMethodError)

  • Jeff Schuil

    @Jeff

    To have the event name text wrap within the calendar day, instead of cutting it off, you’re going to have to make a change to the plugin.

    In the plugin directory open:
    event_calendar/lib/event_calendar/calendar_helper.rb
    and look for the ‘event_content’ method. You’ll see the height of that div is being set. You can remove setting the height, but you’ll probably get some overlapping if you have multiple events on the same day, with long event names. Let me know if you get it working, and I’ll add it as an option to the plugin.

    For the second question, just override the color method on your event model.

    def color
    # color logic based on event type
    # or maybe associated calendar?, ie
    # self.calendar.color
    end

  • http://yourelevation.com Jeff Schuil

    @Jeff

    To have the event name text wrap within the calendar day, instead of cutting it off, you’re going to have to make a change to the plugin.

    In the plugin directory open:
    event_calendar/lib/event_calendar/calendar_helper.rb
    and look for the ‘event_content’ method. You’ll see the height of that div is being set. You can remove setting the height, but you’ll probably get some overlapping if you have multiple events on the same day, with long event names. Let me know if you get it working, and I’ll add it as an option to the plugin.

    For the second question, just override the color method on your event model.

    def color
    # color logic based on event type
    # or maybe associated calendar?, ie
    # self.calendar.color
    end

  • Jeff Schuil

    @Clark

    I haven’t tried it on earlier versions of Rails, but based on your error I think it does require 2.x. You may only have to make a few changes to get it working on 1.x though…

  • http://yourelevation.com Jeff Schuil

    @Clark

    I haven’t tried it on earlier versions of Rails, but based on your error I think it does require 2.x. You may only have to make a few changes to get it working on 1.x though…

  • Clark Richey

    @Jeff

    Thanks. I upgraded the site to Rails 2.3 and it works well now.

  • Clark Richey

    @Jeff

    Thanks. I upgraded the site to Rails 2.3 and it works well now.

  • Clark Richey

    Does this require some other gem? Your calls to Time.zone (e.g. Time.zone.now.year) don’t work for me as there is no such method on Time. I’ve confirmed this with IRB in Ruby 1.8.7

    I have it working now by removing the call to zone but seems like I must have something wrong

  • Clark Richey

    Does this require some other gem? Your calls to Time.zone (e.g. Time.zone.now.year) don’t work for me as there is no such method on Time. I’ve confirmed this with IRB in Ruby 1.8.7

    I have it working now by removing the call to zone but seems like I must have something wrong

  • Jeff Schuil

    @Clark

    Time.zone was introduced in Rails 2.1. Try it out in the rails console, instead of just IRB.

    http://api.rubyonrails.org/classes/ActiveSupport/TimeZone.html#M001520

  • http://yourelevation.com Jeff Schuil

    @Clark

    Time.zone was introduced in Rails 2.1. Try it out in the rails console, instead of just IRB.

    http://api.rubyonrails.org/classes/ActiveSupport/TimeZone.html#M001520

  • Clark Richey

    No good on Rails 2.3.2. See output from rails console:
    Loading development environment (Rails 2.3.2)

    *******************************************************************
    * config.breakpoint_server has been deprecated and has no effect. *
    *******************************************************************

    Time.zone.now.year
    NoMethodError: You have a nil object when you didn’t expect it!
    The error occurred while evaluating nil.now
    from (irb):1
    Time.zone
    => nil
    Time.now
    => Fri Oct 02 14:16:19 -0400 2009

  • Clark Richey

    No good on Rails 2.3.2. See output from rails console:
    Loading development environment (Rails 2.3.2)

    *******************************************************************
    * config.breakpoint_server has been deprecated and has no effect. *
    *******************************************************************

    Time.zone.now.year
    NoMethodError: You have a nil object when you didn’t expect it!
    The error occurred while evaluating nil.now
    from (irb):1
    Time.zone
    => nil
    Time.now
    => Fri Oct 02 14:16:19 -0400 2009

  • http://yourelevation.com Jeff Schuil

    @Clark

    I think I know what’s happening. When you upgraded Rails, you now need to set your time_zone in environment.rb

    # Run “rake -D time” for a list of tasks for finding time zone names.
    config.time_zone = ‘UTC’

  • Jeff Schuil

    @Clark

    I think I know what’s happening. When you upgraded Rails, you now need to set your time_zone in environment.rb

    # Run “rake -D time” for a list of tasks for finding time zone names.
    config.time_zone = ‘UTC’

  • Clark Richey

    Bingo. Thanks!

  • Clark Richey

    Bingo. Thanks!

  • Brandon

    I am getting an argument error in my controller that says invalid date. Can you help with that?

    Thanks

  • Brandon

    I am getting an argument error in my controller that says invalid date. Can you help with that?

    Thanks

  • Jeff Schuil

    @Brandon

    Sounds like the same problem @TS had in a comment above:

    “Move the “map.calendar” route above the default routes.”

  • http://yourelevation.com Jeff Schuil

    @Brandon

    Sounds like the same problem @TS had in a comment above:

    “Move the “map.calendar” route above the default routes.”

  • http://izolashower.com/ Leo

    I am receiving the follwing NoMethodError in Calendar#index

    “undefined method `name’ for Thu, 01 Oct 2009:Date”

    Thanks in advance Jeff for being active on this post.

    -Leo

  • http://izolashower.com Leo

    I am receiving the follwing NoMethodError in Calendar#index

    “undefined method `name’ for Thu, 01 Oct 2009:Date”

    Thanks in advance Jeff for being active on this post.

    -Leo

  • Jeff Schuil

    @Leo

    I think I need more info. Are you following the example in the README? It should be calling ‘name’ on an Event type object, not a Date. Look for where it’s calling event.name (in a helper or view) and see why you have a Date.

  • http://yourelevation.com Jeff Schuil

    @Leo

    I think I need more info. Are you following the example in the README? It should be calling ‘name’ on an Event type object, not a Date. Look for where it’s calling event.name (in a helper or view) and see why you have a Date.

  • http://izolashower.com/ Leo

    Thanks Jeff,

    Found out I hadn’t erased the has_calendar plugin.

    -Leo

  • http://izolashower.com Leo

    Thanks Jeff,

    Found out I hadn’t erased the has_calendar plugin.

    -Leo

  • http://izolashower.com/ Leo

    Jeff,

    I am trying to put the event calendar in a view other than the calendar view but I’m getting the following error:

    NoMethodError in Store#calendar

    Showing app/views/store/calendar.html.erb where line #3 raised:

    You have a nil object when you didn’t expect it!
    The error occurred while evaluating nil.strftime
    Extracted source (around line #3):

    1: ‘top’ %>
    2:
    3:
    4: ‘event’, :action => ‘new’ %>
    5:

    It seems like I am not able to pull the events from here. Any thoughts on how I could do this?

    Thanks again

  • http://izolashower.com Leo

    Jeff,

    I am trying to put the event calendar in a view other than the calendar view but I’m getting the following error:

    NoMethodError in Store#calendar

    Showing app/views/store/calendar.html.erb where line #3 raised:

    You have a nil object when you didn’t expect it!
    The error occurred while evaluating nil.strftime
    Extracted source (around line #3):

    1: ‘top’ %>
    2:
    3:
    4: ‘event’, :action => ‘new’ %>
    5:

    It seems like I am not able to pull the events from here. Any thoughts on how I could do this?

    Thanks again

  • http://izolashower.com/ Leo

    Nevermind.

  • http://izolashower.com Leo

    Nevermind.

  • http://izolashower.com/ Leo

    What would be the easiest way to auto populate my calendar with three blank events each day?

    Thanks in advance

    Leo

  • http://izolashower.com Leo

    What would be the easiest way to auto populate my calendar with three blank events each day?

    Thanks in advance

    Leo

  • http://jewforme.com/ Avishai

    Hi,

    I’m running Rails 2.3.4 on Passenger and the app is failing to start since including the plugin and generated Event model:

    Error message:
    no such file to load — application.rb (MissingSourceFile)
    Exception class:
    PhusionPassenger::UnknownError

    And I know that since a few releases ago, application.rb is called application_controller.rb; I can’t find any references to application.rb in the code. Any thoughts?

  • http://jewforme.com Avishai

    Hi,

    I’m running Rails 2.3.4 on Passenger and the app is failing to start since including the plugin and generated Event model:

    Error message:
    no such file to load — application.rb (MissingSourceFile)
    Exception class:
    PhusionPassenger::UnknownError

    And I know that since a few releases ago, application.rb is called application_controller.rb; I can’t find any references to application.rb in the code. Any thoughts?

  • Jeff Schuil

    @Avishai

    I can’t think of a reason why you’re getting that error. It seems to be ok in a fresh Rails 2.3.4 project. I just ran these steps and got a working calendar.

    1. rails test_cal
    2. cd test_cal
    3. script/plugin install git://github.com/elevation/event_calendar.git
    4. script/generate event_calendar
    5. rake db:migrate
    6. script/server

    I get a calendar at http://localhost:3000/calendar. I also added an event in the db and it came up as well without an error.

    Must be a conflict with your current application? Some people have had problems when they forgot to remove the original Calendar Helper plugin.

  • http://yourelevation.com Jeff Schuil

    @Avishai

    I can’t think of a reason why you’re getting that error. It seems to be ok in a fresh Rails 2.3.4 project. I just ran these steps and got a working calendar.

    1. rails test_cal
    2. cd test_cal
    3. script/plugin install git://github.com/elevation/event_calendar.git
    4. script/generate event_calendar
    5. rake db:migrate
    6. script/server

    I get a calendar at http://localhost:3000/calendar. I also added an event in the db and it came up as well without an error.

    Must be a conflict with your current application? Some people have had problems when they forgot to remove the original Calendar Helper plugin.

  • Nicolas

    Hello,

    I am testing the plugin and I was wondering if it is possible, in the :link_to_day_action, to have a restful route like new_user_event(current_user) if I have a calendar by user.

    Thanks
    Nicolas

  • Nicolas

    Hello,

    I am testing the plugin and I was wondering if it is possible, in the :link_to_day_action, to have a restful route like new_user_event(current_user) if I have a calendar by user.

    Thanks
    Nicolas

  • Jeff Schuil

    @Nicolas

    Override the day_link helper method. For example,
    In helpers/calendar_helper.rb
    def day_link(text, date, day_action)
    link_to(text, new_user_event_path(current_user), :class => ‘day_link’)
    end

  • http://yourelevation.com Jeff Schuil

    @Nicolas

    Override the day_link helper method. For example,
    In helpers/calendar_helper.rb

    def day_link(text, date, day_action)
      link_to(text, new_user_event_path(current_user), :class => 'day_link')
    end
    
  • Anko Painting

    I can’t push to github through my work proxy, but i found a bug – If you have more than 4 events and one of the later ones spans multiple weeks, it is displayed outside of (below) the event strip for that week.

    to fix it in def event_row the first few lines should read
    num_events = 0
    event_counter = 0
    content.each do |strip|
    event_counter += 1
    if !strip.blank?
    num_events = event_counter
    end
    end

    Cheers for the cool plugin.

  • Anko Painting

    I can’t push to github through my work proxy, but i found a bug – If you have more than 4 events and one of the later ones spans multiple weeks, it is displayed outside of (below) the event strip for that week.

    to fix it in def event_row the first few lines should read

    
      num_events = 0
      event_counter = 0
      content.each do |strip|
        event_counter += 1
        if !strip.blank?
          num_events = event_counter
        end
      end

    Cheers for the cool plugin.

  • Jeff Schuil

    @Anko

    Good catch! I just updated the plugin with your fix. Thanks.

  • http://yourelevation.com Jeff Schuil

    @Anko

    Good catch! I just updated the plugin with your fix. Thanks.

  • Sean Dunn

    Hi Jeff,

    I was trying to use this in a existing project with several event like models. Is it possible to alias the start_at and end_at columns without refacoring the plugin or my whole project? i.e. the my existing models use start_date and end_date.

    I’m guessing that the answer is currently no, :( but here’s hoping. :)

    Sean.

  • Sean Dunn

    Hi Jeff,

    I was trying to use this in a existing project with several event like models. Is it possible to alias the start_at and end_at columns without refacoring the plugin or my whole project? i.e. the my existing models use start_date and end_date.

    I’m guessing that the answer is currently no, :( but here’s hoping. :)

    Sean.

  • Jeff Schuil

    @Sean

    You could try using the rails helper method ‘alias_attribute’ in your model.alias_attribute :start_at, :start_date
    alias_attribute :end_at, :end_date

    If nothing else, you could create start_at/end_at getters and setters to wrap your attributes.

  • http://yourelevation.com Jeff Schuil

    @Sean

    You could try using the rails helper method ‘alias_attribute’ in your model.

    alias_attribute :start_at, :start_date
    alias_attribute :end_at, :end_date

    If nothing else, you could create start_at/end_at getters and setters to wrap your attributes.