Abc6f3fa22bd75a5cd3773d50a7e91f9

We have a database that contains all the appointments for the employees. We all work with iCal, so I would like to export the database entries to a iCal subscription.
This is the code I came up with, but I don't think is really good code! Keep in mind that I just started programming Ruby on Rails.
All the tips you can give me are more than welkom!

class IcalController < ApplicationController
  
  ##
  # Build the iCal subscription fom the given first_letters for the user
  # If none first_letters given than build the complete subscription for all users.
  ##
  def subscribe

    require "icalendar"

    first_letters = params[:id]

    #build time frame
    start_at = Time.now.beginning_of_day - 14.days # Kijk 14 dagen in het verleden
    end_at   = Time.now.end_of_day + 13.months     # Kijk 13 maanden in de toekomst

    if !first_letters.nil?
      user      = User.find_by_jobfunction(first_letters, :select => ["Username, id"]) # Initials saved in jobfunction database field!!
      plannings = Planning.find_active_plannings_for_user_between(user.id,start_at, end_at)
    else
      plannings = Planning.find_active_plannings_between(start_at, end_at)
    end

    #pp plannings

    calendar = Icalendar::Calendar.new
    calendar.custom_property("METHOD", "PUBLISH")
    plannings.each do |planning|
      begin
        company = Company.find(planning.company_id, :select => ["id, strCompanyName, strPhone, strOfficeStreet, strOfficeZip, strOfficeCity"])
        planning.description = "" if planning.description.nil?

        body = ""
        body << company.strCompanyName
        body << " "
        body << company.strPhone if not company.strPhone.nil?
        body << "tel onbekend" if company.strPhone.nil?
        body << " "
        body << User.find(planning.user_id).Username if user.nil?
        body << user.Username if not user.nil?
        body << ""

        event = Icalendar::Event.new
        event.klass       "Public"
        event.dtstart     icaldate(planning.start_at)
        event.dtend       icaldate(planning.end_at)
        event.summary     "" << body
        event.description "" << planning.description
        event.location    "" + company.strOfficeStreet + " - " + company.strOfficeZip + " - " + company.strOfficeCity
        event.dtstamp     DateTime.now.to_datetime
        event.uid         "event-#{planning.id.to_s}"
        event.priority    3
        event.sequence    0

        calendar.add_event(event)

      rescue Exception => error
        puts "Dat ging fout: #{error.message}"
      end
    end

    respond_to do |format|
      format.ics {render :text => calendar.to_ical}
      format.html {render :text => calendar.to_ical}
    end

  end
end

class Planning < ActiveRecord::Base

  belongs_to :user, :select => ["Username, id, jobfunction"]
  belongs_to :company, :select => ["id, strCompanyName, strPhone, strOfficeStreet, strOfficeZip, strOfficeCity"]

  def self.find_active_plannings_for_user_between(user, start_date, end_date)
    plannings = self.find(:all, :conditions => ["bitDeleted = 0 AND user_id = ? AND start_at BETWEEN ? AND ?", user, start_date, end_date],
                          :select => ["start_at, end_at, description, id"], :include => [:user, :company])
    return plannings
  end

  def self.find_active_plannings_between(start_date, end_date)
    plannings = self.find(:all, :conditions => ["bitDeleted = 0 AND start_at BETWEEN ? AND ?", start_date, end_date],
                          :select => ["start_at, end_at, description, id"])
    return plannings
  end

end

class Company < ActiveRecord::Base

  has_many :contacts,
           :select => ["id, strContactName, strLastName, strMiddleName, strFirstName, strPhone, strEmail, strMobile"],
           :conditions => ["bitDeleted < 1"]

end

class User < ActiveRecord::Base

  has_many :plannings

end

Refactorings

No refactoring yet !

Your refactoring





Format Copy from initial code

or Cancel