You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ode.apache.org by as...@apache.org on 2008/05/16 10:38:03 UTC

svn commit: r656965 - in /ode/sandbox/singleshot: app/views/activities/show.ics.erb app/views/activities/show.ics.ical lib/extensions.rb lib/extensions/ical_template.rb

Author: assaf
Date: Fri May 16 01:38:02 2008
New Revision: 656965

URL: http://svn.apache.org/viewvc?rev=656965&view=rev
Log:
Added iCal template so we can do fancy things around escaping and properly creating conformant documents.

Added:
    ode/sandbox/singleshot/app/views/activities/show.ics.ical
    ode/sandbox/singleshot/lib/extensions/ical_template.rb
Removed:
    ode/sandbox/singleshot/app/views/activities/show.ics.erb
Modified:
    ode/sandbox/singleshot/lib/extensions.rb

Added: ode/sandbox/singleshot/app/views/activities/show.ics.ical
URL: http://svn.apache.org/viewvc/ode/sandbox/singleshot/app/views/activities/show.ics.ical?rev=656965&view=auto
==============================================================================
--- ode/sandbox/singleshot/app/views/activities/show.ics.ical (added)
+++ ode/sandbox/singleshot/app/views/activities/show.ics.ical Fri May 16 01:38:02 2008
@@ -0,0 +1,10 @@
+calendar.prodid = 'Apache Singleshot'
+for activity in @activities
+  calendar.event do |event|
+    event.dtstamp activity.created_at
+    event.dtstart activity.created_at.to_date
+    event.summary "#{activity.person.fullname} #{activity.action} #{activity.task.title}"
+    event.description link_to(activity.person.fullname, activity.person.identity)
+    event.url task_url(activity.task)
+  end
+end

Modified: ode/sandbox/singleshot/lib/extensions.rb
URL: http://svn.apache.org/viewvc/ode/sandbox/singleshot/lib/extensions.rb?rev=656965&r1=656964&r2=656965&view=diff
==============================================================================
--- ode/sandbox/singleshot/lib/extensions.rb (original)
+++ ode/sandbox/singleshot/lib/extensions.rb Fri May 16 01:38:02 2008
@@ -12,3 +12,6 @@
   include ActiveRecord::Validators::Url
   include ActiveRecord::Validators::Email
 end
+
+require File.expand_path('extensions/ical_template', File.dirname(__FILE__))
+ActionView::Template.register_template_handler(:ical, ActionView::TemplateHandlers::ICalTemplate)

Added: ode/sandbox/singleshot/lib/extensions/ical_template.rb
URL: http://svn.apache.org/viewvc/ode/sandbox/singleshot/lib/extensions/ical_template.rb?rev=656965&view=auto
==============================================================================
--- ode/sandbox/singleshot/lib/extensions/ical_template.rb (added)
+++ ode/sandbox/singleshot/lib/extensions/ical_template.rb Fri May 16 01:38:02 2008
@@ -0,0 +1,78 @@
+module ActionView
+  class ICalBuilder
+
+    class Event
+
+      def initialize
+        @properties = {}
+      end
+
+      attr_accessor :properties
+
+      def to_ical
+        properties = @properties.map { |name, value| "#{name.to_s.upcase}:#{stringify(value)}" }
+        "BEGIN:VEVENT\n#{properties.join("\n")}\nEND:VEVENT"
+      end
+
+    private
+
+      def stringify(value)
+        case value
+        when String then value
+        when Date then value.strftime('%Y%m%d')
+        when Time then value.strftime('%Y%m%dT%H%M%S')
+        else value.to_s
+        end
+      end
+
+      def method_missing(name, arg)
+        @properties[name] = arg
+      end
+
+    end
+
+    def initialize(prodid = nil)
+      @prodid = prodid
+      @events = []
+    end
+
+    attr_accessor :prodid
+    attr_reader :events
+
+    def event
+      returning Event.new do |event|
+        yield event if block_given?
+        @events << event
+      end
+    end
+
+    def to_ical
+      "BEGIN:VCALENDAR\nVERSION:2.0\nPRODID:#{prodid}\n#{events.map(&:to_ical).join("\n")}\nEND:VCALENDAR\n"
+    end
+  end
+
+
+  module TemplateHandlers
+    class ICalTemplate < TemplateHandler
+      include Compilable
+
+      def self.line_offset
+        2
+      end
+
+      def compile(template)
+        content_type_handler = (@view.send!(:controller).respond_to?(:response) ? "controller.response" : "controller")
+        "#{content_type_handler}.content_type ||= Mime::ICS\n" +
+        "calendar = ::ActionView::ICalBuilder.new\n" +
+        template.source +
+        "\ncalendar.to_ical\n"
+      end
+
+      def cache_fragment(block, name = {}, options = nil)
+        @view.fragment_for(block, name, options) do
+          eval('calendar.to_ical', block.binding)
+        end
+      end
+    end
+  end
+end