Skip to content

Commit

Permalink
Adding respond to JSON support for Opportunities
Browse files Browse the repository at this point in the history
  • Loading branch information
sebicas committed Nov 30, 2011
1 parent 5d042b1 commit 2e3602f
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions app/controllers/opportunities_controller.rb
Expand Up @@ -24,6 +24,7 @@ class OpportunitiesController < ApplicationController
after_filter :update_recently_viewed, :only => :show

# GET /opportunities
# GET /opportunities.json
# GET /opportunities.xml
#----------------------------------------------------------------------------
def index
Expand All @@ -32,6 +33,7 @@ def index
respond_to do |format|
format.html # index.html.haml
format.js # index.js.rjs
format.json { render :json => @opportunities }
format.xml { render :xml => @opportunities }
format.xls { send_data @opportunities.to_xls, :type => :xls }
format.csv { send_data @opportunities.to_csv, :type => :csv }
Expand All @@ -41,6 +43,7 @@ def index
end

# GET /opportunities/1
# GET /opportunities/1.json
# GET /opportunities/1.xml HTML
#----------------------------------------------------------------------------
def show
Expand All @@ -51,14 +54,16 @@ def show

respond_to do |format|
format.html # show.html.erb
format.json { render :json => @opportunity }
format.xml { render :xml => @opportunity }
end

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

# GET /opportunities/new
# GET /opportunities/new.json
# GET /opportunities/new.xml AJAX
#----------------------------------------------------------------------------
def new
Expand All @@ -73,6 +78,7 @@ def new

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

Expand All @@ -97,6 +103,7 @@ def edit
end

# POST /opportunities
# POST /opportunities.json
# POST /opportunities.xml AJAX
#----------------------------------------------------------------------------
def create
Expand All @@ -113,6 +120,7 @@ def create
get_data_for_sidebar(:campaign)
end
format.js # create.js.rjs
format.json { render :json => @opportunity, :status => :created, :location => @opportunity }
format.xml { render :xml => @opportunity, :status => :created, :location => @opportunity }
else
@users = User.except(@current_user)
Expand All @@ -129,12 +137,14 @@ def create
@contact = Contact.find(params[:contact]) unless params[:contact].blank?
@campaign = Campaign.find(params[:campaign]) unless params[:campaign].blank?
format.js # create.js.rjs
format.json { render :json => @opportunity.errors, :status => :unprocessable_entity }
format.xml { render :xml => @opportunity.errors, :status => :unprocessable_entity }
end
end
end

# PUT /opportunities/1
# PUT /opportunities/1.json
# PUT /opportunities/1.xml AJAX
#----------------------------------------------------------------------------
def update
Expand All @@ -150,6 +160,7 @@ def update
get_data_for_sidebar(:campaign)
end
format.js
format.json { head :ok }
format.xml { head :ok }
else
@users = User.except(@current_user)
Expand All @@ -160,15 +171,17 @@ def update
@account = Account.new(:user => @current_user)
end
format.js
format.json { render :json => @opportunity.errors, :status => :unprocessable_entity }
format.xml { render :xml => @opportunity.errors, :status => :unprocessable_entity }
end
end

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

# DELETE /opportunities/1
# DELETE /opportunities/1.json
# DELETE /opportunities/1.xml HTML and AJAX
#----------------------------------------------------------------------------
def destroy
Expand All @@ -183,11 +196,12 @@ def destroy
respond_to do |format|
format.html { respond_to_destroy(:html) }
format.js { respond_to_destroy(:ajax) }
format.json { head :ok }
format.xml { head :ok }
end

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

# PUT /opportunities/1/attach
Expand All @@ -211,6 +225,7 @@ def search

respond_to do |format|
format.js { render :index }
format.json { render :json => @opportunities.as_json }
format.xml { render :xml => @opportunities.to_xml }
end
end
Expand Down

0 comments on commit 2e3602f

Please sign in to comment.