Skip to content

Instantly share code, notes, and snippets.

@ghostandthemachine
Created November 2, 2012 23:44
Show Gist options
  • Save ghostandthemachine/4005056 to your computer and use it in GitHub Desktop.
Save ghostandthemachine/4005056 to your computer and use it in GitHub Desktop.
## I want to add a column in the event table that will store the google calendar event id. So far I found the event.rb and I added the correct column.
## i'm assuming something like this
## event.rb
class Event
property :google_calendar_event_id, String
end
## What I need to figure out is how to query the event table for a given event id and then once I return a valid event, I need to be able to add the google calendar event id.
## I take it you are getting the event id from the ajax post. If thats the case it should be something like
## routes/api.rb
## this ajax post route
post '/api/sync/event' do
event_id = params[:event_id] ## assuming your ajax post contains the key/value {event_id: the_id}
@event = Event.all(id: event_id).first ## Model.all returns a collection so you need to call first to get an instance (and the collection should only have one value in this case)
if @event
google_event = get_google_calendar_event(params) ## whatever function(s) you use to get the calendar info (only if the event exists in our db)
@event.google_calendar_event_id = google_event[:event_uuid] ## assuming you have a hash from the google response
if @event.save
return "{\"success\": \"success\", \"message\": \"it worked\"}" ## return a useful string back to the ajax done function by returning an informed string or JSON string if there is more info than a string can usefully store
else
return "{\"success\": \"failed\", \"message\": \"google_sync_error_#{maybe_error_info_from_google}\"}"
end
else
return "{\"success\": \"failed\", \"message\": \"event_does_not_exist\"}"
end
end
## This way you can have something in your ajax done function like
## .done(function(response_msg) {
## var obj = jQuery.parseJSON(response_msg);
## var success_msg = obj.success
## if success_msg == "success"
## success html updates...
## } elseif success_msg == "failed" {
## html error info...
## }
## });
## Once you have it working in the routes file you can clean it up and move it to a controller method in api_controller.rb. You will probably need to just recreate your db to migrate the added column. I think you can also just automigrate with
## model/event.rb
Event.auto_upgrade! # at end of file I think
## But I haven't tried this migration type before. I should.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment