js111
js111

Reputation: 1314

Calendar in rails app

Im working on implementing a calendar into my rails 3.2 app.I found this great railscast that explains a plugin which does exactly what i need. However, i need to have data pulled in from multiple models, not just one.. Anyone know of a solution for this?

i.e - I have separate birthdays, anniversary, and holidays tables and would like to display them all on the same calendar...The railscast only deals with an article model..

Thanks!

http://railscasts.com/episodes/213-calendars

https://github.com/watu/table_builder

users/show.html.erb

<div id="calendar">
    <h2 id="month">
    <%= link_to "<", :month => (@date.beginning_of_month-1).strftime("%Y-%m") %>
    <%=h @date.strftime("%B %Y") %>
    <%= link_to ">", :month => (@date.end_of_month+1).strftime("%Y-%m") %>
  </h2>
                <%= calendar_for @friends, :year => @date.year, :month => @date.month do |calendar| %>
                 <%= calendar.head('mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun') %>
                  <%= calendar.day(:day_method => :dob) do |date, friends| %>
                   <%= date.day %>
                    <ul>
                        <% friends.each do |friend| %>
                        <li> <%= h(friend.name) %><%= "\'s birthday"%></li>
                        <% %>
                     <% end %>
                    </ul>
                <% end %>
            <% end %>
</div>

Upvotes: 0

Views: 500

Answers (1)

DVG
DVG

Reputation: 17480

The calendar table is just building off an array (which is to say, the results set from an active record query). If you need it to work off different models, you would just do the following:

def calendar_action
  #assumes a scope of this_month on each model
  @events = []
  @events << Birthday.this_month
  @events << Anniversary.this_month
  @events << Holiday.this_month
end

Of course, it doesn't sound like birthdays, anniversaries and holidays are all that different, why not just refactor them to an event model with an event category association? Regardless, how you implement is up to you, just a suggestion!

Upvotes: 1

Related Questions