Rails Calendar Generation Method
I agree that it is too long. I find a good place to start is to try breaking it up into methods that are short and each do a single thing. While I'm doing this I usually see opportunities to make things DRY. 

My code might look like:

content_tag(:table, ...) do 
  header_row + week_rows      
end

def week_rows
  num_rows = (first_day + last_day) % 7
  rows = num_rows.times.map { |i| week_row(i) }
  content_tag(:tbody) do rows.join('')
end


A couple of things I noticed:


Instead of hard-coding html like "&lt;table class='table-condensed table-bordered table-striped'&gt;&lt;thead&gt;&lt;tr&gt;" use the Rails content_tag helper
Instead of date.at_end_of_month.strftime('%w').to_i use the wday method. There is also a day method that you can use instead of %d.
Have your method return a string that is already html_safe