Skip to content

Commit

Permalink
Adding respond to JSON support for Tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
sebicas committed Nov 30, 2011
1 parent 2e3602f commit 0bcddee
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions app/controllers/tasks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class TasksController < ApplicationController
before_filter :set_current_tab, :only => [ :index, :show ]

# GET /tasks
# GET /tasks.json
# GET /tasks.xml
#----------------------------------------------------------------------------
def index
Expand All @@ -29,6 +30,7 @@ def index

respond_to do |format|
format.html # index.html.haml
format.json { render :json => @tasks }
format.xml { render :xml => @tasks }
format.xls { send_data @tasks.values.flatten.to_xls, :type => :xls }
format.csv { send_data @tasks.values.flatten.to_csv, :type => :csv }
Expand All @@ -38,16 +40,22 @@ def index
end

# GET /tasks/1
# GET /tasks/1.json
# GET /tasks/1.xml HTML
#----------------------------------------------------------------------------
def show
respond_to do |format|
format.html { render :index }
format.json { @task = Task.tracked_by(@current_user).find(params[:id]); render :json => @task }
format.xml { @task = Task.tracked_by(@current_user).find(params[:id]); render :xml => @task }
end

rescue ActiveRecord::RecordNotFound
respond_to_not_found(:js, :json, :xml)
end

# GET /tasks/new
# GET /tasks/new.json
# GET /tasks/new.xml AJAX
#----------------------------------------------------------------------------
def new
Expand All @@ -63,6 +71,7 @@ def new

respond_to do |format|
format.js # new.js.rjs
format.json { render :json => @task }
format.xml { render :xml => @task }
end

Expand All @@ -89,6 +98,7 @@ def edit
end

# POST /tasks
# POST /tasks.json
# POST /tasks.xml AJAX
#----------------------------------------------------------------------------
def create
Expand All @@ -99,15 +109,18 @@ def create
if @task.save
update_sidebar if called_from_index_page?
format.js # create.js.rjs
format.json { render :json => @task, :status => :created, :location => @task }
format.xml { render :xml => @task, :status => :created, :location => @task }
else
format.js # create.js.rjs
format.json { render :json => @task.errors, :status => :unprocessable_entity }
format.xml { render :xml => @task.errors, :status => :unprocessable_entity }
end
end
end

# PUT /tasks/1
# PUT /tasks/1.json
# PUT /tasks/1.xml AJAX
#----------------------------------------------------------------------------
def update
Expand All @@ -131,18 +144,21 @@ def update
update_sidebar
end
format.js # update.js.rjs
format.json { head :ok }
format.xml { head :ok }
else
format.js # update.js.rjs
format.json { render :json => @task.errors, :status => :unprocessable_entity }
format.xml { render :xml => @task.errors, :status => :unprocessable_entity }
end
end

rescue ActiveRecord::RecordNotFound
respond_to_not_found(:js, :xml)
respond_to_not_found(:js, :json, :xml)
end

# DELETE /tasks/1
# DELETE /tasks/1.json
# DELETE /tasks/1.xml AJAX
#----------------------------------------------------------------------------
def destroy
Expand All @@ -158,14 +174,16 @@ def destroy
update_sidebar if called_from_index_page?
respond_to do |format|
format.js # destroy.js.rjs
format.json { head :ok }
format.xml { head :ok }
end

rescue ActiveRecord::RecordNotFound
respond_to_not_found(:js, :xml)
respond_to_not_found(:js, :json, :xml)
end

# PUT /tasks/1/complete
# PUT /tasks/1/complete.json
# PUT /leads/1/complete.xml AJAX
#----------------------------------------------------------------------------
def complete
Expand All @@ -180,11 +198,12 @@ def complete
update_sidebar unless params[:bucket].blank?
respond_to do |format|
format.js # complete.js.rjs
format.json { head :ok }
format.xml { head :ok }
end

rescue ActiveRecord::RecordNotFound
respond_to_not_found(:js, :xml)
respond_to_not_found(:js, :json, :xml)
end

# POST /tasks/auto_complete/query AJAX
Expand Down

0 comments on commit 0bcddee

Please sign in to comment.