Previous/Next Links with calendar_helper plugin
Posted by admin Sun, 10 Feb 2008 03:35:00 GMT
You may have to hack this a little to get it to do exactly what you want ..The following will go forward to future years but not will go back beyond the current year.
It is not the prettiest thing, but this is what I was required to do and am just offering up the code as a help.
## put this in your controller
def setvars
# You could store this in a Time/Date object instead of two separate integers
session[:year] ||= Time.now.year
session[:month] ||= Time.now.month
$user = User
end
def nnext
session[:month] += 1
render(:layout => false)
end
def nprev
session[:month] -= 1
render(:layout => false)
end
# I put the following in application.rb to use globally
$year = Time.now.year
$month = Time.now.month
## this can go in your view ..
<div id="mini-calendar">
<%= calendar(:year => $year, :month => $month, :table_class => "calendar_helper") do |d|
"<a href='/calendars/#{d.year}/#{d.mon}/#{d.mday}'>#{d.mday}</a>"
end
%>
</div>
<%= link_to_remote("Next"), :update => 'mini-calendar', :url => { :controller => "your_controller", :action => :nprev}) %>
<%= link_to_remote("Previous"), :update => 'mini-calendar', :url => { :controller => "your_controller", :action => :nnext}) %> ## you will then have to create two new views name nnext and nprev
# nnext
<% case -%>
<% when session[:month] === 0 %>
<%= calendar(:year => $year, :month => session[:month]+1, :table_class => "calendar_helper") do |d|
"<a href='/calendars/#{d.year}/#{d.mon}/#{d.mday}'>#{d.mday}</a>"
end
%>
<% when session[:month] >= 13 %>
<%= calendar(:year => $year+1, :month => session[:month]-12, :table_class => "calendar_helper") do |d|
"<a href='/calendars/#{d.year}/#{d.mon}/#{d.mday}'>#{d.mday}</a>"
end
%>
<% else -%>
<%= calendar(:year => $year, :month => session[:month], :table_class => "calendar_helper") do |d|
"<a href='/calendars/#{d.year}/#{d.mon}/#{d.mday}'>#{d.mday}</a>"
end
%>
<% end -%>
#nprev
<% case -%>
<% when session[:month] === 0 %>
<%= calendar(:year => $year, :month => session[:month]+1, :table_class => "calendar_helper") do |d|
"<a href='/calendars/#{d.year}/#{d.mon}/#{d.mday}'>#{d.mday}</a>"
end
%>
<% when session[:month] >= 13 %>
<%= calendar(:year => $year+1, :month => session[:month]-12, :table_class => "calendar_helper") do |d|
"<a href='/calendars/#{d.year}/#{d.mon}/#{d.mday}'>#{d.mday}</a>"
end
%>
<% else -%>
<%= calendar(:year => $year, :month => session[:month], :table_class => "calendar_helper") do |d|
"<a href='/calendars/#{d.year}/#{d.mon}/#{d.mday}'>#{d.mday}</a>"
end
%>
<% end -%>
I would love to know how to do this without putting the logic inside the view like this.
As always, if anyone can suggest a better way.. I am game.
Please leave a comment.


