Skip to content

Instantly share code, notes, and snippets.

@tbbooher
Created September 12, 2011 03:06
Show Gist options
  • Save tbbooher/1210498 to your computer and use it in GitHub Desktop.
Save tbbooher/1210498 to your computer and use it in GitHub Desktop.
<%= form_for(@event) do |f| %>
<% if @event.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@event.errors.count, "error") %> prohibited this event from being saved:</h2>
<ul>
<% @event.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :starts_at %><br />
<%= f.datetime_select :starts_at %>
</div>
<div class="field">
<%= f.label :ends_at %><br />
<%= f.datetime_select :ends_at %>
</div>
<div class="field">
<%= f.label :all_day %><br />
<%= f.check_box :all_day %>
</div>
<div class="field">
<%= f.label :description %><br />
<%= f.text_field :description %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
class Event
include Mongoid::Document
include Mongoid::Timestamps
field :title, :type => String
field :starts_at, :type => DateTime
field :ends_at, :type => DateTime
field :all_day, :type => Boolean
field :description, :type => String
#scope :before2, lambda {|end_time| {:conditions => ["ends_at < ?", Event.format_date(end_time)] }}
#scope :after2, lambda {|start_time| {:conditions => ["starts_at > ?", Event.format_date(start_time)] }}
scope :before, lambda {|end_time| where(:ends_at.lt => Event.format_date(end_time))}
scope :after, lambda {|start_time| where(:ends_at.gt => Event.format_date(start_time))}
# need to override the json view to return what full_calendar is expecting.
# http://arshaw.com/fullcalendar/docs/event_data/Event_Object/
def as_json(options = {})
{
:id => self.id,
:title => self.title,
:description => self.description || "",
:start => starts_at.rfc822,
:end => ends_at.rfc822,
:allDay => self.all_day,
:recurring => false,
:url => Rails.application.routes.url_helpers.event_path(id)
}
end
def self.format_date(date_time)
Time.at(date_time.to_i).to_formatted_s(:db)
end
end
class EventsController < ApplicationController
# GET /events
# GET /events.xml
def index
# full_calendar will hit the index method with query parameters
# 'start' and 'end' in order to filter the results for the
# appropriate month/week/day. It should be possiblt to change
# this to be starts_at and ends_at to match rails conventions.
# I'll eventually do that to make the demo a little cleaner.
@events = Event.scoped
@events = @events.after(params['start']) if (params['start'])
@events = @events.before(params['end']) if (params['end'])
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @events }
format.js { render :json => @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 }
format.js { render :json => @event.to_json }
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
format.html { redirect_to(@event, :notice => "Event was successfully created. #{params[:event].to_s}") }
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
# PUT /events/1.js
# when we drag an event on the calendar (from day to day on the month view, or stretching
# it on the week or day view), this method will be called to update the values.
# viv la REST!
def update
@event = Event.find(params[:id])
respond_to do |format|
if @event.update_attributes(params[:event])
format.html { redirect_to(@event, :notice => 'Event was successfully updated.') }
format.xml { head :ok }
format.js { head :ok}
else
format.html { render :action => "edit" }
format.xml { render :xml => @event.errors, :status => :unprocessable_entity }
format.js { render :js => @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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment