Dagi3d v4

Calendario en Ruby On Rails

Como hacía tiempo que no programaba nada para Ruby On Rails, me puse a hacer un sencillo helper para generar calendarios que tuviesen las fechas pinchables:

calendar_helper.rb

module CalendarHelper

  def calendar(year, month, data = {})

    month_time = Time.local(year, month)
    month_name = month_time.strftime("%B")
    first_week_day = month_time.wday
    first_week_day = (first_week_day == 0)? 7 : first_week_day;

    if (month == 12)
      last_month_day = 31
    else
      last_month_day = Time.gm(year, month + 1)
      last_month_day -= 60 * 60 * 24
      last_month_day = last_month_day.day
    end

    table =  "<table class=\"calendar\">" +
      "<thead>" +
      "<tr><th colspan=\"7\">#{month_name}</th></tr>" +
      "<tr><th>L</th><th>M</th><th>X</th><th>J</th>" + 
      "<th>V</th><th>S</th><th>D</th></tr>" +
      "</thead>" +
      "<tbody>" 

    sc = ((first_week_day - 1) * -1) + 1
    week_day = 1

    for i in sc..last_month_day

      if (week_day == 1)
        table << "<tr>" 
      end

      if (i > 0)
        current_date = Time.gm(year, month, i).strftime("%Y-%m-%d")

        if (data.has_key?(current_date))
          table << "<td><a href=\"#{data[current_date]}\">#{i}</a></td>" 
        else
          table << "<td>#{i}</td>" 
        end       

      else
        table << "<td></td>" 
      end

      if (week_day == 7)
        table << "</tr>" 
      end

      week_day = (week_day >= 7)? 1 : week_day + 1

    end

    table << "</tbody></table>" 

    return table  
  end

end

Para usarlo basta con indicar el año y mes del calendario a generar. Opcionalmente se puede pasar un hash con las distintas fechas que queremos que sean pinchables junto a su correspondiente enlace:

<%=calendar(2006, 9, {'2006-09-05' => '/blog/2006/09/05'})%>

javier
02/07/2007 03:04

hola quiero que me ayudes.. ya que necesito .. controlar los horarios de entrada y salida del trabajo a muchas personas. y quiero usar un calendario que registre las actividades de H_ingreso y H_salida , H_extras por cada empleado. y quiero tener definido un camino a seguir gracias por tu consejo. uso.. mysql

Deja un comentario
*: campos obligatorios. La dirección de correo no será publicada