From d2bfbfd2810fbee673e43b2515db8bac527b3441 Mon Sep 17 00:00:00 2001 From: hukl Date: Mon, 16 Mar 2009 20:58:49 +0100 Subject: Refactored Chaos Calendar by wrapping libical and introducing event and occurrence model. More improvements to come. Enables us to create events with reoccurrence and intervals etc. --- .gitignore | 5 + app/controllers/events_controller.rb | 85 +++++++++++++++++ app/controllers/occurrences_controller.rb | 85 +++++++++++++++++ app/helpers/events_helper.rb | 2 + app/helpers/occurrences_helper.rb | 2 + app/models/event.rb | 27 ++++++ app/models/node.rb | 1 + app/models/occurrence.rb | 60 ++++++++++++ app/views/events/edit.html.erb | 48 ++++++++++ app/views/events/index.html.erb | 36 +++++++ app/views/events/new.html.erb | 47 ++++++++++ app/views/events/show.html.erb | 48 ++++++++++ app/views/layouts/events.html.erb | 17 ++++ app/views/layouts/occurrences.html.erb | 17 ++++ app/views/occurrences/edit.html.erb | 32 +++++++ app/views/occurrences/index.html.erb | 28 ++++++ app/views/occurrences/new.html.erb | 31 ++++++ app/views/occurrences/show.html.erb | 28 ++++++ config/routes.rb | 4 + db/migrate/20090316194152_create_events.rb | 21 +++++ db/migrate/20090316195143_create_occurrences.rb | 17 ++++ doc/cccms_model.graffle | Bin 530900 -> 671940 bytes lib/chaos_calendar.rb | 35 ------- lib/chaos_calendar/gen.sh | 0 public/stylesheets/scaffold.css | 54 +++++++++++ test/fixtures/events.yml | 3 + test/fixtures/occurrences.yml | 1 + test/functional/events_controller_test.rb | 45 +++++++++ test/functional/occurrences_controller_test.rb | 45 +++++++++ test/unit/event_test.rb | 119 ++++++++++++++++++++++++ test/unit/helpers/events_helper_test.rb | 4 + test/unit/helpers/occurrences_helper_test.rb | 4 + test/unit/occurrence_test.rb | 8 ++ vendor/rails | 2 +- 34 files changed, 925 insertions(+), 36 deletions(-) create mode 100644 app/controllers/events_controller.rb create mode 100644 app/controllers/occurrences_controller.rb create mode 100644 app/helpers/events_helper.rb create mode 100644 app/helpers/occurrences_helper.rb create mode 100644 app/models/event.rb create mode 100644 app/models/occurrence.rb create mode 100644 app/views/events/edit.html.erb create mode 100644 app/views/events/index.html.erb create mode 100644 app/views/events/new.html.erb create mode 100644 app/views/events/show.html.erb create mode 100644 app/views/layouts/events.html.erb create mode 100644 app/views/layouts/occurrences.html.erb create mode 100644 app/views/occurrences/edit.html.erb create mode 100644 app/views/occurrences/index.html.erb create mode 100644 app/views/occurrences/new.html.erb create mode 100644 app/views/occurrences/show.html.erb create mode 100644 db/migrate/20090316194152_create_events.rb create mode 100644 db/migrate/20090316195143_create_occurrences.rb delete mode 100644 lib/chaos_calendar.rb mode change 100644 => 100755 lib/chaos_calendar/gen.sh create mode 100644 public/stylesheets/scaffold.css create mode 100644 test/fixtures/events.yml create mode 100644 test/fixtures/occurrences.yml create mode 100644 test/functional/events_controller_test.rb create mode 100644 test/functional/occurrences_controller_test.rb create mode 100644 test/unit/event_test.rb create mode 100644 test/unit/helpers/events_helper_test.rb create mode 100644 test/unit/helpers/occurrences_helper_test.rb create mode 100644 test/unit/occurrence_test.rb diff --git a/.gitignore b/.gitignore index 0b05835..b8d3ec2 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,8 @@ coverage coverage.data .project db/updates/ +lib/chaos_calendar/Makefile +lib/chaos_calendar/ical_occurrences.bundle +lib/chaos_calendar/ical_occurrences.o +lib/chaos_calendar/ical_occurrences_wrap.c +lib/chaos_calendar/ical_occurrences_wrap.o \ No newline at end of file diff --git a/app/controllers/events_controller.rb b/app/controllers/events_controller.rb new file mode 100644 index 0000000..c25c73f --- /dev/null +++ b/app/controllers/events_controller.rb @@ -0,0 +1,85 @@ +class EventsController < ApplicationController + # GET /events + # GET /events.xml + def index + @events = Event.all + + respond_to do |format| + format.html # index.html.erb + format.xml { render :xml => @events } + end + end + + # GET /events/1 + # GET /events/1.xml + def show + @event = Event.find(params[:id]) + + respond_to do |format| + format.html # show.html.erb + format.xml { render :xml => @event } + end + end + + # GET /events/new + # GET /events/new.xml + def new + @event = Event.new + + respond_to do |format| + format.html # new.html.erb + format.xml { render :xml => @event } + end + end + + # GET /events/1/edit + def edit + @event = Event.find(params[:id]) + end + + # POST /events + # POST /events.xml + def create + @event = Event.new(params[:event]) + + respond_to do |format| + if @event.save + flash[:notice] = 'Event was successfully created.' + format.html { redirect_to(@event) } + format.xml { render :xml => @event, :status => :created, :location => @event } + else + format.html { render :action => "new" } + format.xml { render :xml => @event.errors, :status => :unprocessable_entity } + end + end + end + + # PUT /events/1 + # PUT /events/1.xml + def update + @event = Event.find(params[:id]) + + respond_to do |format| + if @event.update_attributes(params[:event]) + flash[:notice] = 'Event was successfully updated.' + format.html { redirect_to(@event) } + format.xml { head :ok } + else + format.html { render :action => "edit" } + format.xml { render :xml => @event.errors, :status => :unprocessable_entity } + end + end + end + + # DELETE /events/1 + # DELETE /events/1.xml + def destroy + @event = Event.find(params[:id]) + @event.destroy + + respond_to do |format| + format.html { redirect_to(events_url) } + format.xml { head :ok } + end + end +end diff --git a/app/controllers/occurrences_controller.rb b/app/controllers/occurrences_controller.rb new file mode 100644 index 0000000..e3f1cdd --- /dev/null +++ b/app/controllers/occurrences_controller.rb @@ -0,0 +1,85 @@ +class OccurrencesController < ApplicationController + # GET /occurrences + # GET /occurrences.xml + def index + @occurrences = Occurrence.all + + respond_to do |format| + format.html # index.html.erb + format.xml { render :xml => @occurrences } + end + end + + # GET /occurrences/1 + # GET /occurrences/1.xml + def show + @occurrence = Occurrence.find(params[:id]) + + respond_to do |format| + format.html # show.html.erb + format.xml { render :xml => @occurrence } + end + end + + # GET /occurrences/new + # GET /occurrences/new.xml + def new + @occurrence = Occurrence.new + + respond_to do |format| + format.html # new.html.erb + format.xml { render :xml => @occurrence } + end + end + + # GET /occurrences/1/edit + def edit + @occurrence = Occurrence.find(params[:id]) + end + + # POST /occurrences + # POST /occurrences.xml + def create + @occurrence = Occurrence.new(params[:occurrence]) + + respond_to do |format| + if @occurrence.save + flash[:notice] = 'Occurrence was successfully created.' + format.html { redirect_to(@occurrence) } + format.xml { render :xml => @occurrence, :status => :created, :location => @occurrence } + else + format.html { render :action => "new" } + format.xml { render :xml => @occurrence.errors, :status => :unprocessable_entity } + end + end + end + + # PUT /occurrences/1 + # PUT /occurrences/1.xml + def update + @occurrence = Occurrence.find(params[:id]) + + respond_to do |format| + if @occurrence.update_attributes(params[:occurrence]) + flash[:notice] = 'Occurrence was successfully updated.' + format.html { redirect_to(@occurrence) } + format.xml { head :ok } + else + format.html { render :action => "edit" } + format.xml { render :xml => @occurrence.errors, :status => :unprocessable_entity } + end + end + end + + # DELETE /occurrences/1 + # DELETE /occurrences/1.xml + def destroy + @occurrence = Occurrence.find(params[:id]) + @occurrence.destroy + + respond_to do |format| + format.html { redirect_to(occurrences_url) } + format.xml { head :ok } + end + end +end diff --git a/app/helpers/events_helper.rb b/app/helpers/events_helper.rb new file mode 100644 index 0000000..8a9a878 --- /dev/null +++ b/app/helpers/events_helper.rb @@ -0,0 +1,2 @@ +module EventsHelper +end diff --git a/app/helpers/occurrences_helper.rb b/app/helpers/occurrences_helper.rb new file mode 100644 index 0000000..8cba881 --- /dev/null +++ b/app/helpers/occurrences_helper.rb @@ -0,0 +1,2 @@ +module OccurrencesHelper +end diff --git a/app/models/event.rb b/app/models/event.rb new file mode 100644 index 0000000..60b8521 --- /dev/null +++ b/app/models/event.rb @@ -0,0 +1,27 @@ +class Event < ActiveRecord::Base + + # Associations + + has_many :occurrences + belongs_to :node + + # Callbacks + + after_save :generate_occurences + + # Instance Methods + + def occurrences_in_range start_time, end_time + self.occurrences.find( + :all, :conditions => [ + "start_time > ? AND end_time < ?", + start_time, end_time + ] + ) + end + + private + def generate_occurences + Occurrence.generate self + end +end diff --git a/app/models/node.rb b/app/models/node.rb index d2db4ba..da26164 100644 --- a/app/models/node.rb +++ b/app/models/node.rb @@ -7,6 +7,7 @@ class Node < ActiveRecord::Base belongs_to :head, :class_name => "Page", :foreign_key => :head_id belongs_to :draft, :class_name => "Page", :foreign_key => :draft_id has_many :permissions + has_one :event # Callbacks after_create :initialize_empty_page diff --git a/app/models/occurrence.rb b/app/models/occurrence.rb new file mode 100644 index 0000000..6205618 --- /dev/null +++ b/app/models/occurrence.rb @@ -0,0 +1,60 @@ +# TODO Make a gem out of the c wrapper +require 'lib/chaos_calendar/ical_occurrences' + +class Occurrence < ActiveRecord::Base + + # Associations + + belongs_to :node + belongs_to :event + + # Class Methods + + def self.find_in_range start_time, end_time + find( + :all, :conditions => [ + "start_time > ? AND end_time < ?", start_time, end_time + ] + ) + end + + # Deletes all Occurrences which belong to the given event. Afterwards a few + # variables are set to save repetitive queries. The occurrences of the given + # event are then calculated and created. + def self.generate event + self.delete_all(:event_id => event.id) + + node = event.node + summary = node.head.title + duration = (event.end_time - event.start_time) + occurrences = self.generate_dates(event) + + occurrences.each do |occurrence| + self.create( + :summary => summary, + :start_time => occurrence, + :end_time => (occurrence + duration), + :node_id => node.id, + :event_id => event.id + ) + end + end + + # Calculates the start_time of all occurrences for a given event if a proper + # RRule is provided. An ArgumentError is thrown from within the libical + # wrapper if the RRule is malformed. If the rrule attribute of an event is + # nil, it simply returns the event start_time as only occurrence. + # Return value is always an array of Time objects. + def self.generate_dates event + if event.rrule + Ical_occurrences::occurrences( + event.start_time.utc.iso8601, + (Time.now + 5.years).utc.iso8601, + event.rrule + ) + else + [event.start_time] + end + + end +end diff --git a/app/views/events/edit.html.erb b/app/views/events/edit.html.erb new file mode 100644 index 0000000..e40ac55 --- /dev/null +++ b/app/views/events/edit.html.erb @@ -0,0 +1,48 @@ +

Editing event

+ +<% form_for(@event) do |f| %> + <%= f.error_messages %> + +

+ <%= f.label :start_time %>
+ <%= f.datetime_select :start_time %> +

+

+ <%= f.label :end_time %>
+ <%= f.datetime_select :end_time %> +

+

+ <%= f.label :rrule %>
+ <%= f.text_field :rrule %> +

+

+ <%= f.label :custom_rrule %>
+ <%= f.check_box :custom_rrule %> +

+

+ <%= f.label :allday %>
+ <%= f.check_box :allday %> +

+

+ <%= f.label :url %>
+ <%= f.text_field :url %> +

+

+ <%= f.label :latitude %>
+ <%= f.text_field :latitude %> +

+

+ <%= f.label :longitude %>
+ <%= f.text_field :longitude %> +

+

+ <%= f.label :node_id %>
+ <%= f.text_field :node_id %> +

+

+ <%= f.submit 'Update' %> +

+<% end %> + +<%= link_to 'Show', @event %> | +<%= link_to 'Back', events_path %> \ No newline at end of file diff --git a/app/views/events/index.html.erb b/app/views/events/index.html.erb new file mode 100644 index 0000000..c1b5b48 --- /dev/null +++ b/app/views/events/index.html.erb @@ -0,0 +1,36 @@ +

Listing events

+ + + + + + + + + + + + + + +<% @events.each do |event| %> + + + + + + + + + + + + + + +<% end %> +
Start timeEnd timeRruleCustom rruleAlldayUrlLatitudeLongitudeNode
<%=h event.start_time %><%=h event.end_time %><%=h event.rrule %><%=h event.custom_rrule %><%=h event.allday %><%=h event.url %><%=h event.latitude %><%=h event.longitude %><%=h event.node_id %><%= link_to 'Show', event %><%= link_to 'Edit', edit_event_path(event) %><%= link_to 'Destroy', event, :confirm => 'Are you sure?', :method => :delete %>
+ +
+ +<%= link_to 'New event', new_event_path %> \ No newline at end of file diff --git a/app/views/events/new.html.erb b/app/views/events/new.html.erb new file mode 100644 index 0000000..0182b07 --- /dev/null +++ b/app/views/events/new.html.erb @@ -0,0 +1,47 @@ +

New event

+ +<% form_for(@event) do |f| %> + <%= f.error_messages %> + +

+ <%= f.label :start_time %>
+ <%= f.datetime_select :start_time %> +

+

+ <%= f.label :end_time %>
+ <%= f.datetime_select :end_time %> +

+

+ <%= f.label :rrule %>
+ <%= f.text_field :rrule %> +

+

+ <%= f.label :custom_rrule %>
+ <%= f.check_box :custom_rrule %> +

+

+ <%= f.label :allday %>
+ <%= f.check_box :allday %> +

+

+ <%= f.label :url %>
+ <%= f.text_field :url %> +

+

+ <%= f.label :latitude %>
+ <%= f.text_field :latitude %> +

+

+ <%= f.label :longitude %>
+ <%= f.text_field :longitude %> +

+

+ <%= f.label :node_id %>
+ <%= f.text_field :node_id %> +

+

+ <%= f.submit 'Create' %> +

+<% end %> + +<%= link_to 'Back', events_path %> \ No newline at end of file diff --git a/app/views/events/show.html.erb b/app/views/events/show.html.erb new file mode 100644 index 0000000..699765d --- /dev/null +++ b/app/views/events/show.html.erb @@ -0,0 +1,48 @@ +

+ Start time: + <%=h @event.start_time %> +

+ +

+ End time: + <%=h @event.end_time %> +

+ +

+ Rrule: + <%=h @event.rrule %> +

+ +

+ Custom rrule: + <%=h @event.custom_rrule %> +

+ +

+ Allday: + <%=h @event.allday %> +

+ +

+ Url: + <%=h @event.url %> +

+ +

+ Latitude: + <%=h @event.latitude %> +

+ +

+ Longitude: + <%=h @event.longitude %> +

+ +

+ Node: + <%=h @event.node_id %> +

+ + +<%= link_to 'Edit', edit_event_path(@event) %> | +<%= link_to 'Back', events_path %> \ No newline at end of file diff --git a/app/views/layouts/events.html.erb b/app/views/layouts/events.html.erb new file mode 100644 index 0000000..93a6c0c --- /dev/null +++ b/app/views/layouts/events.html.erb @@ -0,0 +1,17 @@ + + + + + + Events: <%= controller.action_name %> + <%= stylesheet_link_tag 'scaffold' %> + + + +

<%= flash[:notice] %>

+ +<%= yield %> + + + diff --git a/app/views/layouts/occurrences.html.erb b/app/views/layouts/occurrences.html.erb new file mode 100644 index 0000000..ab4aca6 --- /dev/null +++ b/app/views/layouts/occurrences.html.erb @@ -0,0 +1,17 @@ + + + + + + Occurrences: <%= controller.action_name %> + <%= stylesheet_link_tag 'scaffold' %> + + + +

<%= flash[:notice] %>

+ +<%= yield %> + + + diff --git a/app/views/occurrences/edit.html.erb b/app/views/occurrences/edit.html.erb new file mode 100644 index 0000000..6a81188 --- /dev/null +++ b/app/views/occurrences/edit.html.erb @@ -0,0 +1,32 @@ +

Editing occurrence

+ +<% form_for(@occurrence) do |f| %> + <%= f.error_messages %> + +

+ <%= f.label :summary %>
+ <%= f.text_field :summary %> +

+

+ <%= f.label :start_time %>
+ <%= f.datetime_select :start_time %> +

+

+ <%= f.label :end_time %>
+ <%= f.datetime_select :end_time %> +

+

+ <%= f.label :node_id %>
+ <%= f.text_field :node_id %> +

+

+ <%= f.label :event_id %>
+ <%= f.text_field :event_id %> +

+

+ <%= f.submit 'Update' %> +

+<% end %> + +<%= link_to 'Show', @occurrence %> | +<%= link_to 'Back', occurrences_path %> \ No newline at end of file diff --git a/app/views/occurrences/index.html.erb b/app/views/occurrences/index.html.erb new file mode 100644 index 0000000..06dbf07 --- /dev/null +++ b/app/views/occurrences/index.html.erb @@ -0,0 +1,28 @@ +

Listing occurrences

+ + + + + + + + + + +<% @occurrences.each do |occurrence| %> + + + + + + + + + + +<% end %> +
SummaryStart timeEnd timeNodeEvent
<%=h occurrence.summary %><%=h occurrence.start_time %><%=h occurrence.end_time %><%=h occurrence.node_id %><%=h occurrence.event_id %><%= link_to 'Show', occurrence %><%= link_to 'Edit', edit_occurrence_path(occurrence) %><%= link_to 'Destroy', occurrence, :confirm => 'Are you sure?', :method => :delete %>
+ +
+ +<%= link_to 'New occurrence', new_occurrence_path %> \ No newline at end of file diff --git a/app/views/occurrences/new.html.erb b/app/views/occurrences/new.html.erb new file mode 100644 index 0000000..0a77af1 --- /dev/null +++ b/app/views/occurrences/new.html.erb @@ -0,0 +1,31 @@ +

New occurrence

+ +<% form_for(@occurrence) do |f| %> + <%= f.error_messages %> + +

+ <%= f.label :summary %>
+ <%= f.text_field :summary %> +

+

+ <%= f.label :start_time %>
+ <%= f.datetime_select :start_time %> +

+

+ <%= f.label :end_time %>
+ <%= f.datetime_select :end_time %> +

+

+ <%= f.label :node_id %>
+ <%= f.text_field :node_id %> +

+

+ <%= f.label :event_id %>
+ <%= f.text_field :event_id %> +

+

+ <%= f.submit 'Create' %> +

+<% end %> + +<%= link_to 'Back', occurrences_path %> \ No newline at end of file diff --git a/app/views/occurrences/show.html.erb b/app/views/occurrences/show.html.erb new file mode 100644 index 0000000..9e831cf --- /dev/null +++ b/app/views/occurrences/show.html.erb @@ -0,0 +1,28 @@ +

+ Summary: + <%=h @occurrence.summary %> +

+ +

+ Start time: + <%=h @occurrence.start_time %> +

+ +

+ End time: + <%=h @occurrence.end_time %> +

+ +

+ Node: + <%=h @occurrence.node_id %> +

+ +

+ Event: + <%=h @occurrence.event_id %> +

+ + +<%= link_to 'Edit', edit_occurrence_path(@occurrence) %> | +<%= link_to 'Back', occurrences_path %> \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 24735b5..11ca34c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,8 @@ ActionController::Routing::Routes.draw do |map| + map.resources :occurrences + + map.resources :events + map.root( :locale => 'de', :controller => 'content', diff --git a/db/migrate/20090316194152_create_events.rb b/db/migrate/20090316194152_create_events.rb new file mode 100644 index 0000000..7f44fdf --- /dev/null +++ b/db/migrate/20090316194152_create_events.rb @@ -0,0 +1,21 @@ +class CreateEvents < ActiveRecord::Migration + def self.up + create_table :events do |t| + t.datetime :start_time + t.datetime :end_time + t.string :rrule + t.boolean :custom_rrule + t.boolean :allday + t.string :url + t.float :latitude + t.float :longitude + t.integer :node_id + + t.timestamps + end + end + + def self.down + drop_table :events + end +end diff --git a/db/migrate/20090316195143_create_occurrences.rb b/db/migrate/20090316195143_create_occurrences.rb new file mode 100644 index 0000000..e0df07b --- /dev/null +++ b/db/migrate/20090316195143_create_occurrences.rb @@ -0,0 +1,17 @@ +class CreateOccurrences < ActiveRecord::Migration + def self.up + create_table :occurrences do |t| + t.string :summary + t.datetime :start_time + t.datetime :end_time + t.integer :node_id + t.integer :event_id + + t.timestamps + end + end + + def self.down + drop_table :occurrences + end +end diff --git a/doc/cccms_model.graffle b/doc/cccms_model.graffle index 8303a0b..98ab2c7 100644 Binary files a/doc/cccms_model.graffle and b/doc/cccms_model.graffle differ diff --git a/lib/chaos_calendar.rb b/lib/chaos_calendar.rb deleted file mode 100644 index b363ccb..0000000 --- a/lib/chaos_calendar.rb +++ /dev/null @@ -1,35 +0,0 @@ -require 'vpim' - -class Occurrence - def initialize start, event, node - @dtstart = start - @event = event - @node = node - end - attr_reader :dtstart, :event, :node -end - -class ChaosCalendar - attr_reader :calendar - def initialize - @calendar = {} - end - - def push cal, node - Vpim::Icalendar.decode( cal ).each { |c| c.events.each { |e| @calendar[e] = node } } - end - - def occurrences start_time, end_time - occurr = [] - @calendar.each { |e, node| - if e.occurs_in?( start_time, end_time ) - e.occurences( end_time ) { |t| - occurr << Occurrence.new(t,e,node) if (t + (e.duration || 0)) >= start_time - } - end - } - - return occurr.sort { |lhs, rhs| lhs.dtstart <=> rhs.dtstart } - end - -end diff --git a/lib/chaos_calendar/gen.sh b/lib/chaos_calendar/gen.sh old mode 100644 new mode 100755 diff --git a/public/stylesheets/scaffold.css b/public/stylesheets/scaffold.css new file mode 100644 index 0000000..093c209 --- /dev/null +++ b/public/stylesheets/scaffold.css @@ -0,0 +1,54 @@ +body { background-color: #fff; color: #333; } + +body, p, ol, ul, td { + font-family: verdana, arial, helvetica, sans-serif; + font-size: 13px; + line-height: 18px; +} + +pre { + background-color: #eee; + padding: 10px; + font-size: 11px; +} + +a { color: #000; } +a:visited { color: #666; } +a:hover { color: #fff; background-color:#000; } + +.fieldWithErrors { + padding: 2px; + background-color: red; + display: table; +} + +#errorExplanation { + width: 400px; + border: 2px solid red; + padding: 7px; + padding-bottom: 12px; + margin-bottom: 20px; + background-color: #f0f0f0; +} + +#errorExplanation h2 { + text-align: left; + font-weight: bold; + padding: 5px 5px 5px 15px; + font-size: 12px; + margin: -7px; + background-color: #c00; + color: #fff; +} + +#errorExplanation p { + color: #333; + margin-bottom: 0; + padding: 5px; +} + +#errorExplanation ul li { + font-size: 12px; + list-style: square; +} + diff --git a/test/fixtures/events.yml b/test/fixtures/events.yml new file mode 100644 index 0000000..bd73671 --- /dev/null +++ b/test/fixtures/events.yml @@ -0,0 +1,3 @@ +# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html + + diff --git a/test/fixtures/occurrences.yml b/test/fixtures/occurrences.yml new file mode 100644 index 0000000..a56c164 --- /dev/null +++ b/test/fixtures/occurrences.yml @@ -0,0 +1 @@ +# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html diff --git a/test/functional/events_controller_test.rb b/test/functional/events_controller_test.rb new file mode 100644 index 0000000..5698c7b --- /dev/null +++ b/test/functional/events_controller_test.rb @@ -0,0 +1,45 @@ +require 'test_helper' + +class EventsControllerTest < ActionController::TestCase + # test "should get index" do + # get :index + # assert_response :success + # assert_not_nil assigns(:events) + # end + # + # test "should get new" do + # get :new + # assert_response :success + # end + # + # test "should create event" do + # assert_difference('Event.count') do + # post :create, :event => { } + # end + # + # assert_redirected_to event_path(assigns(:event)) + # end + # + # test "should show event" do + # get :show, :id => events(:one).to_param + # assert_response :success + # end + # + # test "should get edit" do + # get :edit, :id => events(:one).to_param + # assert_response :success + # end + # + # test "should update event" do + # put :update, :id => events(:one).to_param, :event => { } + # assert_redirected_to event_path(assigns(:event)) + # end + # + # test "should destroy event" do + # assert_difference('Event.count', -1) do + # delete :destroy, :id => events(:one).to_param + # end + # + # assert_redirected_to events_path + # end +end diff --git a/test/functional/occurrences_controller_test.rb b/test/functional/occurrences_controller_test.rb new file mode 100644 index 0000000..0b00e0e --- /dev/null +++ b/test/functional/occurrences_controller_test.rb @@ -0,0 +1,45 @@ +require 'test_helper' + +class OccurrencesControllerTest < ActionController::TestCase + # test "should get index" do + # get :index + # assert_response :success + # assert_not_nil assigns(:occurrences) + # end + # + # test "should get new" do + # get :new + # assert_response :success + # end + # + # test "should create occurrence" do + # assert_difference('Occurrence.count') do + # post :create, :occurrence => { } + # end + # + # assert_redirected_to occurrence_path(assigns(:occurrence)) + # end + # + # test "should show occurrence" do + # get :show, :id => occurrences(:one).to_param + # assert_response :success + # end + # + # test "should get edit" do + # get :edit, :id => occurrences(:one).to_param + # assert_response :success + # end + # + # test "should update occurrence" do + # put :update, :id => occurrences(:one).to_param, :occurrence => { } + # assert_redirected_to occurrence_path(assigns(:occurrence)) + # end + # + # test "should destroy occurrence" do + # assert_difference('Occurrence.count', -1) do + # delete :destroy, :id => occurrences(:one).to_param + # end + # + # assert_redirected_to occurrences_path + # end +end diff --git a/test/unit/event_test.rb b/test/unit/event_test.rb new file mode 100644 index 0000000..8d51b1a --- /dev/null +++ b/test/unit/event_test.rb @@ -0,0 +1,119 @@ +require 'test_helper' + +class EventTest < ActiveSupport::TestCase + + def setup + Page.delete_all + @cal_node = Node.create :slug => "calendar" + @cal_node.move_to_child_of Node.root + @draft = @cal_node.find_or_create_draft User.first + @draft.title = "99C3" + @draft.abstract = "The 99th Chaos Comunication Congress" + @draft.body = "Its totally freakin awesome" + @draft.save + @cal_node.publish_draft! + @cal_node.head.reload + end + + test 'verfy setup data' do + assert_not_nil @cal_node + assert_not_nil @cal_node.head + end + + test 'creating an event with malformed rrule raises exception' do + assert_raise(ArgumentError) do + Event.create!( + :start_time => "2009-01-01T15:23:42".to_time, + :end_time => "2009-01-01T20:05:23".to_time, + :url => "http://events.ccc.de/congress/2082", + :latitude => 52.525308, + :longitude => 13.378944, + :rrule => "FOOBAR", + :allday => false, + :custom_rrule => false, + :node_id => @cal_node.id + ) + end + end + + test 'create day event for node with one occurrence' do + assert_not_nil event = Event.create!( + :start_time => "2009-01-01T15:23:42".to_time, + :end_time => "2009-01-01T20:05:23".to_time, + :url => "http://events.ccc.de/congress/2082", + :latitude => 52.525308, + :longitude => 13.378944, + :rrule => nil, + :allday => false, + :custom_rrule => false, + :node_id => @cal_node.id + ) + + assert_equal 1, Occurrence.count + assert_equal event.start_time, Occurrence.first.start_time + assert_equal event.end_time, Occurrence.first.end_time + assert_equal @cal_node.head.title, Occurrence.first.summary + end + + test 'create day event with weekly reoccurrence and checking data' do + assert_not_nil event = Event.create!( + :start_time => "2009-01-01T15:23:42".to_time, + :end_time => "2009-01-01T20:05:23".to_time, + :url => "http://events.ccc.de/congress/2082", + :latitude => 52.525308, + :longitude => 13.378944, + :rrule => "FREQ=WEEKLY;INTERVAL=1", + :allday => false, + :custom_rrule => false, + :node_id => @cal_node.id + ) + + assert_not_nil scoped_occurrences = event.occurrences_in_range( + "2009-01-01".to_time, "2009-12-31".to_time + ) + + assert_equal 52, scoped_occurrences.length + + assert_equal "2009-12-24T15:23:42".to_time, scoped_occurrences[51].start_time + assert_equal "2009-12-24T20:05:23".to_time, scoped_occurrences[51].end_time + assert_equal "99C3", scoped_occurrences[51].summary + assert_equal @cal_node.event, scoped_occurrences[51].event + assert_equal @cal_node, scoped_occurrences[51].node + + assert_equal "2009-03-19T15:23:42".to_time, scoped_occurrences[11].start_time + assert_equal "2009-03-19T20:05:23".to_time, scoped_occurrences[11].end_time + assert_equal "99C3", scoped_occurrences[11].summary + assert_equal @cal_node.event, scoped_occurrences[11].event + assert_equal @cal_node, scoped_occurrences[11].node + + assert_equal "2009-01-01T15:23:42".to_time, scoped_occurrences[0].start_time + assert_equal "2009-01-01T20:05:23".to_time, scoped_occurrences[0].end_time + assert_equal "99C3", scoped_occurrences[0].summary + assert_equal @cal_node.event, scoped_occurrences[11].event + assert_equal @cal_node, scoped_occurrences[11].node + end + + test 'create chaosradio event with custom rrule and interval' do + assert_not_nil event = Event.create!( + :start_time => "2009-01-28T21:00:00".to_time, + :end_time => "2009-01-28T23:00:00".to_time, + :url => "http://chaosradio.ccc.de", + :latitude => 52.525308, + :longitude => 13.378944, + :rrule => "FREQ=MONTHLY;INTERVAL=1;BYDAY=-1WE", + :allday => false, + :custom_rrule => true, + :node_id => @cal_node.id + ) + + assert_not_nil scoped_occurrences = event.occurrences_in_range( + "2009-01-01".to_time, "2009-12-31".to_time + ) + + assert_equal 12, scoped_occurrences.length + + expected_days = [28, 25, 25, 29, 27, 24, 29, 26, 30, 28, 25, 30] + chaosradio_days = scoped_occurrences.map {|x| x.start_time.day} + assert_equal expected_days, chaosradio_days + end +end \ No newline at end of file diff --git a/test/unit/helpers/events_helper_test.rb b/test/unit/helpers/events_helper_test.rb new file mode 100644 index 0000000..2e7567e --- /dev/null +++ b/test/unit/helpers/events_helper_test.rb @@ -0,0 +1,4 @@ +require 'test_helper' + +class EventsHelperTest < ActionView::TestCase +end diff --git a/test/unit/helpers/occurrences_helper_test.rb b/test/unit/helpers/occurrences_helper_test.rb new file mode 100644 index 0000000..0692926 --- /dev/null +++ b/test/unit/helpers/occurrences_helper_test.rb @@ -0,0 +1,4 @@ +require 'test_helper' + +class OccurrencesHelperTest < ActionView::TestCase +end diff --git a/test/unit/occurrence_test.rb b/test/unit/occurrence_test.rb new file mode 100644 index 0000000..91a78ec --- /dev/null +++ b/test/unit/occurrence_test.rb @@ -0,0 +1,8 @@ +require 'test_helper' + +class OccurrenceTest < ActiveSupport::TestCase + # Replace this with your real tests. + test "the truth" do + assert true + end +end diff --git a/vendor/rails b/vendor/rails index bc5c984..40e813c 160000 --- a/vendor/rails +++ b/vendor/rails @@ -1 +1 @@ -Subproject commit bc5c984c3e2dd3ae63cb89216e581a24cd76bd0e +Subproject commit 40e813cbf83ff0258c7549211aa7a0e1c7ddb4c8 -- cgit v1.3