diff options
| author | erdgeist <erdgeist@bauklotz.local> | 2009-03-10 21:03:52 +0100 |
|---|---|---|
| committer | erdgeist <erdgeist@bauklotz.local> | 2009-03-10 21:03:52 +0100 |
| commit | 26687eaf7400aea5d569f99e880ac3949982f303 (patch) | |
| tree | 7bd22978d1022de16a41428d65398edab628bf3c | |
| parent | d6049aeffc7de43393a9a7a1d2f95f26422a046f (diff) | |
added calendar backend. more features and integration to come
| -rw-r--r-- | app/controllers/events_controller.rb | 85 | ||||
| -rw-r--r-- | app/helpers/events_helper.rb | 2 | ||||
| -rw-r--r-- | app/models/event.rb | 2 | ||||
| -rw-r--r-- | app/views/events/edit.html.erb | 16 | ||||
| -rw-r--r-- | app/views/events/index.html.erb | 20 | ||||
| -rw-r--r-- | app/views/events/new.html.erb | 15 | ||||
| -rw-r--r-- | app/views/events/show.html.erb | 8 | ||||
| -rw-r--r-- | app/views/layouts/events.html.erb | 17 | ||||
| -rw-r--r-- | config/routes.rb | 1 | ||||
| -rw-r--r-- | lib/chaos_calendar.rb | 1 | ||||
| -rw-r--r-- | test/fixtures/events.yml | 7 | ||||
| -rw-r--r-- | test/functional/events_controller_test.rb | 45 | ||||
| -rw-r--r-- | test/unit/event_test.rb | 77 | ||||
| -rw-r--r-- | test/unit/helpers/events_helper_test.rb | 4 |
14 files changed, 300 insertions, 0 deletions
diff --git a/app/controllers/events_controller.rb b/app/controllers/events_controller.rb new file mode 100644 index 0000000..063b7a1 --- /dev/null +++ b/app/controllers/events_controller.rb | |||
| @@ -0,0 +1,85 @@ | |||
| 1 | class EventsController < ApplicationController | ||
| 2 | # GET /events | ||
| 3 | # GET /events.xml | ||
| 4 | def index | ||
| 5 | @events = Event.find(:all) | ||
| 6 | |||
| 7 | respond_to do |format| | ||
| 8 | format.html # index.html.erb | ||
| 9 | format.xml { render :xml => @events } | ||
| 10 | end | ||
| 11 | end | ||
| 12 | |||
| 13 | # GET /events/1 | ||
| 14 | # GET /events/1.xml | ||
| 15 | def show | ||
| 16 | @event = Event.find(params[:id]) | ||
| 17 | |||
| 18 | respond_to do |format| | ||
| 19 | format.html # show.html.erb | ||
| 20 | format.xml { render :xml => @event } | ||
| 21 | end | ||
| 22 | end | ||
| 23 | |||
| 24 | # GET /events/new | ||
| 25 | # GET /events/new.xml | ||
| 26 | def new | ||
| 27 | @event = Event.new | ||
| 28 | |||
| 29 | respond_to do |format| | ||
| 30 | format.html # new.html.erb | ||
| 31 | format.xml { render :xml => @event } | ||
| 32 | end | ||
| 33 | end | ||
| 34 | |||
| 35 | # GET /events/1/edit | ||
| 36 | def edit | ||
| 37 | @event = Event.find(params[:id]) | ||
| 38 | end | ||
| 39 | |||
| 40 | # POST /events | ||
| 41 | # POST /events.xml | ||
| 42 | def create | ||
| 43 | @event = Event.new(params[:event]) | ||
| 44 | |||
| 45 | respond_to do |format| | ||
| 46 | if @event.save | ||
| 47 | flash[:notice] = 'Event was successfully created.' | ||
| 48 | format.html { redirect_to(@event) } | ||
| 49 | format.xml { render :xml => @event, :status => :created, :location => @event } | ||
| 50 | else | ||
| 51 | format.html { render :action => "new" } | ||
| 52 | format.xml { render :xml => @event.errors, :status => :unprocessable_entity } | ||
| 53 | end | ||
| 54 | end | ||
| 55 | end | ||
| 56 | |||
| 57 | # PUT /events/1 | ||
| 58 | # PUT /events/1.xml | ||
| 59 | def update | ||
| 60 | @event = Event.find(params[:id]) | ||
| 61 | |||
| 62 | respond_to do |format| | ||
| 63 | if @event.update_attributes(params[:event]) | ||
| 64 | flash[:notice] = 'Event was successfully updated.' | ||
| 65 | format.html { redirect_to(@event) } | ||
| 66 | format.xml { head :ok } | ||
| 67 | else | ||
| 68 | format.html { render :action => "edit" } | ||
| 69 | format.xml { render :xml => @event.errors, :status => :unprocessable_entity } | ||
| 70 | end | ||
| 71 | end | ||
| 72 | end | ||
| 73 | |||
| 74 | # DELETE /events/1 | ||
| 75 | # DELETE /events/1.xml | ||
| 76 | def destroy | ||
| 77 | @event = Event.find(params[:id]) | ||
| 78 | @event.destroy | ||
| 79 | |||
| 80 | respond_to do |format| | ||
| 81 | format.html { redirect_to(events_url) } | ||
| 82 | format.xml { head :ok } | ||
| 83 | end | ||
| 84 | end | ||
| 85 | 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 @@ | |||
| 1 | module EventsHelper | ||
| 2 | end | ||
diff --git a/app/models/event.rb b/app/models/event.rb new file mode 100644 index 0000000..3a829fd --- /dev/null +++ b/app/models/event.rb | |||
| @@ -0,0 +1,2 @@ | |||
| 1 | class Event < ActiveRecord::Base | ||
| 2 | end | ||
diff --git a/app/views/events/edit.html.erb b/app/views/events/edit.html.erb new file mode 100644 index 0000000..244f35f --- /dev/null +++ b/app/views/events/edit.html.erb | |||
| @@ -0,0 +1,16 @@ | |||
| 1 | <h1>Editing event</h1> | ||
| 2 | |||
| 3 | <% form_for(@event) do |f| %> | ||
| 4 | <%= f.error_messages %> | ||
| 5 | |||
| 6 | <p> | ||
| 7 | <%= f.label :serialized_event %><br /> | ||
| 8 | <%= f.text_area :serialized_event %> | ||
| 9 | </p> | ||
| 10 | <p> | ||
| 11 | <%= f.submit "Update" %> | ||
| 12 | </p> | ||
| 13 | <% end %> | ||
| 14 | |||
| 15 | <%= link_to 'Show', @event %> | | ||
| 16 | <%= link_to 'Back', events_path %> | ||
diff --git a/app/views/events/index.html.erb b/app/views/events/index.html.erb new file mode 100644 index 0000000..ad0ca8d --- /dev/null +++ b/app/views/events/index.html.erb | |||
| @@ -0,0 +1,20 @@ | |||
| 1 | <h1>Listing events</h1> | ||
| 2 | |||
| 3 | <table> | ||
| 4 | <tr> | ||
| 5 | <th>Serialized event</th> | ||
| 6 | </tr> | ||
| 7 | |||
| 8 | <% for event in @events %> | ||
| 9 | <tr> | ||
| 10 | <td><%=h event.serialized_event %></td> | ||
| 11 | <td><%= link_to 'Show', event %></td> | ||
| 12 | <td><%= link_to 'Edit', edit_event_path(event) %></td> | ||
| 13 | <td><%= link_to 'Destroy', event, :confirm => 'Are you sure?', :method => :delete %></td> | ||
| 14 | </tr> | ||
| 15 | <% end %> | ||
| 16 | </table> | ||
| 17 | |||
| 18 | <br /> | ||
| 19 | |||
| 20 | <%= link_to 'New event', new_event_path %> | ||
diff --git a/app/views/events/new.html.erb b/app/views/events/new.html.erb new file mode 100644 index 0000000..44f4bb4 --- /dev/null +++ b/app/views/events/new.html.erb | |||
| @@ -0,0 +1,15 @@ | |||
| 1 | <h1>New event</h1> | ||
| 2 | |||
| 3 | <% form_for(@event) do |f| %> | ||
| 4 | <%= f.error_messages %> | ||
| 5 | |||
| 6 | <p> | ||
| 7 | <%= f.label :serialized_event %><br /> | ||
| 8 | <%= f.text_area :serialized_event %> | ||
| 9 | </p> | ||
| 10 | <p> | ||
| 11 | <%= f.submit "Create" %> | ||
| 12 | </p> | ||
| 13 | <% end %> | ||
| 14 | |||
| 15 | <%= link_to 'Back', events_path %> | ||
diff --git a/app/views/events/show.html.erb b/app/views/events/show.html.erb new file mode 100644 index 0000000..ad9c582 --- /dev/null +++ b/app/views/events/show.html.erb | |||
| @@ -0,0 +1,8 @@ | |||
| 1 | <p> | ||
| 2 | <b>Serialized event:</b> | ||
| 3 | <%=h @event.serialized_event %> | ||
| 4 | </p> | ||
| 5 | |||
| 6 | |||
| 7 | <%= link_to 'Edit', edit_event_path(@event) %> | | ||
| 8 | <%= link_to 'Back', events_path %> | ||
diff --git a/app/views/layouts/events.html.erb b/app/views/layouts/events.html.erb new file mode 100644 index 0000000..6201c57 --- /dev/null +++ b/app/views/layouts/events.html.erb | |||
| @@ -0,0 +1,17 @@ | |||
| 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" | ||
| 2 | "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> | ||
| 3 | |||
| 4 | <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> | ||
| 5 | <head> | ||
| 6 | <meta http-equiv="content-type" content="text/html;charset=UTF-8" /> | ||
| 7 | <title>Events: <%= controller.action_name %></title> | ||
| 8 | <%= stylesheet_link_tag 'scaffold' %> | ||
| 9 | </head> | ||
| 10 | <body> | ||
| 11 | |||
| 12 | <p style="color: green"><%= flash[:notice] %></p> | ||
| 13 | |||
| 14 | <%= yield %> | ||
| 15 | |||
| 16 | </body> | ||
| 17 | </html> | ||
diff --git a/config/routes.rb b/config/routes.rb index 4305a21..33216b3 100644 --- a/config/routes.rb +++ b/config/routes.rb | |||
| @@ -10,6 +10,7 @@ ActionController::Routing::Routes.draw do |map| | |||
| 10 | 10 | ||
| 11 | map.resources :pages | 11 | map.resources :pages |
| 12 | map.resources :nodes, :member => {:publish => :put, :unlock => :put} | 12 | map.resources :nodes, :member => {:publish => :put, :unlock => :put} |
| 13 | map.resources :events | ||
| 13 | 14 | ||
| 14 | map.logout '/logout', :controller => 'sessions', :action => 'destroy' | 15 | map.logout '/logout', :controller => 'sessions', :action => 'destroy' |
| 15 | map.login '/login', :controller => 'sessions', :action => 'new' | 16 | map.login '/login', :controller => 'sessions', :action => 'new' |
diff --git a/lib/chaos_calendar.rb b/lib/chaos_calendar.rb index 22ad934..94ad258 100644 --- a/lib/chaos_calendar.rb +++ b/lib/chaos_calendar.rb | |||
| @@ -10,6 +10,7 @@ class Occurrence | |||
| 10 | end | 10 | end |
| 11 | 11 | ||
| 12 | class ChaosCalendar | 12 | class ChaosCalendar |
| 13 | attr_reader :calendar | ||
| 13 | def initialize | 14 | def initialize |
| 14 | @calendar = {} | 15 | @calendar = {} |
| 15 | end | 16 | end |
diff --git a/test/fixtures/events.yml b/test/fixtures/events.yml new file mode 100644 index 0000000..6d43f1d --- /dev/null +++ b/test/fixtures/events.yml | |||
| @@ -0,0 +1,7 @@ | |||
| 1 | # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html | ||
| 2 | |||
| 3 | one: | ||
| 4 | serialized_event: MyText | ||
| 5 | |||
| 6 | two: | ||
| 7 | serialized_event: MyText | ||
diff --git a/test/functional/events_controller_test.rb b/test/functional/events_controller_test.rb new file mode 100644 index 0000000..24a6f94 --- /dev/null +++ b/test/functional/events_controller_test.rb | |||
| @@ -0,0 +1,45 @@ | |||
| 1 | require 'test_helper' | ||
| 2 | |||
| 3 | class EventsControllerTest < ActionController::TestCase | ||
| 4 | test "should get index" do | ||
| 5 | get :index | ||
| 6 | assert_response :success | ||
| 7 | assert_not_nil assigns(:events) | ||
| 8 | end | ||
| 9 | |||
| 10 | test "should get new" do | ||
| 11 | get :new | ||
| 12 | assert_response :success | ||
| 13 | end | ||
| 14 | |||
| 15 | test "should create event" do | ||
| 16 | assert_difference('Event.count') do | ||
| 17 | post :create, :event => { } | ||
| 18 | end | ||
| 19 | |||
| 20 | assert_redirected_to event_path(assigns(:event)) | ||
| 21 | end | ||
| 22 | |||
| 23 | test "should show event" do | ||
| 24 | get :show, :id => events(:one).id | ||
| 25 | assert_response :success | ||
| 26 | end | ||
| 27 | |||
| 28 | test "should get edit" do | ||
| 29 | get :edit, :id => events(:one).id | ||
| 30 | assert_response :success | ||
| 31 | end | ||
| 32 | |||
| 33 | test "should update event" do | ||
| 34 | put :update, :id => events(:one).id, :event => { } | ||
| 35 | assert_redirected_to event_path(assigns(:event)) | ||
| 36 | end | ||
| 37 | |||
| 38 | test "should destroy event" do | ||
| 39 | assert_difference('Event.count', -1) do | ||
| 40 | delete :destroy, :id => events(:one).id | ||
| 41 | end | ||
| 42 | |||
| 43 | assert_redirected_to events_path | ||
| 44 | end | ||
| 45 | end | ||
diff --git a/test/unit/event_test.rb b/test/unit/event_test.rb new file mode 100644 index 0000000..c8ee335 --- /dev/null +++ b/test/unit/event_test.rb | |||
| @@ -0,0 +1,77 @@ | |||
| 1 | require 'test_helper' | ||
| 2 | |||
| 3 | class EventTest < ActiveSupport::TestCase | ||
| 4 | |||
| 5 | def setup | ||
| 6 | @cal = <<-EOT | ||
| 7 | BEGIN:VCALENDAR | ||
| 8 | VERSION:2.0 | ||
| 9 | PRODID:-//Ensemble Independent//vPim 0.658//EN | ||
| 10 | CALSCALE:Gregorian | ||
| 11 | BEGIN:VEVENT | ||
| 12 | SUMMARY:Chaosradio | ||
| 13 | DTSTART:20080505T220000 | ||
| 14 | LOCATION:FM 102,60 MHz | ||
| 15 | URL:http://www.fritz.de/frequenzen | ||
| 16 | RRULE:FREQ=MONTHLY;BYMONTH=1,2,3,4,5,6,7,8,9,10,11;BYDAY=-1WE;UNTIL=20091105T220000 | ||
| 17 | CREATED:20021020T220000 | ||
| 18 | DURATION:PT3H | ||
| 19 | END:VEVENT | ||
| 20 | END:VCALENDAR | ||
| 21 | EOT | ||
| 22 | |||
| 23 | @first_child = Node.find(2) | ||
| 24 | end | ||
| 25 | |||
| 26 | test 'saving serialized event' do | ||
| 27 | assert event = Event.create!( :serialized_event => @cal ) | ||
| 28 | end | ||
| 29 | |||
| 30 | test 'parsing calendar data' do | ||
| 31 | event = Event.create!( :serialized_event => @cal ) | ||
| 32 | assert calendar = ChaosCalendar.new | ||
| 33 | assert calendar.push( event.serialized_event, @first_child.id ) | ||
| 34 | assert_equal 1, calendar.calendar.length | ||
| 35 | assert_equal "Chaosradio", calendar.calendar.to_a.first.first.summary | ||
| 36 | end | ||
| 37 | |||
| 38 | test 'checking occurrences logic' do | ||
| 39 | event = Event.create!( :serialized_event => @cal ) | ||
| 40 | calendar = ChaosCalendar.new | ||
| 41 | calendar.push( event.serialized_event, @first_child.id ) | ||
| 42 | |||
| 43 | all_occurrences = calendar.occurrences( "2009-03-22".to_datetime, "2010-01-10".to_datetime ) | ||
| 44 | assert_equal 8, all_occurrences.length | ||
| 45 | |||
| 46 | assert_equal "2009-03-25".to_date, all_occurrences.first.dtstart.to_date | ||
| 47 | assert_equal "2009-10-28".to_date, all_occurrences.last.dtstart.to_date | ||
| 48 | end | ||
| 49 | |||
| 50 | end | ||
| 51 | |||
| 52 | __END__ | ||
| 53 | |||
| 54 | opt_days = 7 | ||
| 55 | |||
| 56 | calendar = ChaosCalendar.new | ||
| 57 | |||
| 58 | |||
| 59 | calendar.push( cal, 25 ) | ||
| 60 | t0 = Time.today | ||
| 61 | t1 = t0 + opt_days.days | ||
| 62 | |||
| 63 | all_occurrences = calendar.occurrences( t0, t1 ) | ||
| 64 | |||
| 65 | all_occurrences.each do |o| | ||
| 66 | e = o.event | ||
| 67 | puts o.dtstart | ||
| 68 | puts "#{e.summary}:" | ||
| 69 | |||
| 70 | puts " description=#{e.description}" if e.description | ||
| 71 | puts " comment=#{e.comments.first}" if e.comments | ||
| 72 | puts " location=#{e.location}" if e.location | ||
| 73 | puts " status=#{e.status}" if e.status | ||
| 74 | puts " dtstart=#{e.dtstart}" if e.dtstart | ||
| 75 | puts " duration=#{Vpim::Duration.new(e.duration).to_s}" if e.duration | ||
| 76 | end | ||
| 77 | |||
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 @@ | |||
| 1 | require 'test_helper' | ||
| 2 | |||
| 3 | class EventsHelperTest < ActionView::TestCase | ||
| 4 | end | ||
