Skip to content

Commit

Permalink
Adding respond to JSON support for Leads
Browse files Browse the repository at this point in the history
  • Loading branch information
sebicas committed Nov 30, 2011
1 parent 9ec9821 commit 5d042b1
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions app/controllers/leads_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class LeadsController < ApplicationController
after_filter :update_recently_viewed, :only => :show

# GET /leads
# GET /leads.json
# GET /leads.xml AJAX and HTML
#----------------------------------------------------------------------------
def index
Expand All @@ -30,6 +31,7 @@ def index
respond_to do |format|
format.html # index.html.erb
format.js # index.js.rjs
format.json { render :json => @leads }
format.xml { render :xml => @leads }
format.xls { send_data @leads.to_xls, :type => :xls }
format.csv { send_data @leads.to_csv, :type => :csv }
Expand All @@ -39,6 +41,7 @@ def index
end

# GET /leads/1
# GET /leads/1.json
# GET /leads/1.xml HTML
#----------------------------------------------------------------------------
def show
Expand All @@ -49,14 +52,16 @@ def show

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

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

# GET /leads/new
# GET /leads/new.json
# GET /leads/new.xml AJAX
#----------------------------------------------------------------------------
def new
Expand All @@ -70,6 +75,7 @@ def new

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

Expand All @@ -93,6 +99,7 @@ def edit
end

# POST /leads
# POST /leads.json
# POST /leads.xml AJAX
#----------------------------------------------------------------------------
def create
Expand All @@ -109,15 +116,18 @@ def create
get_data_for_sidebar(:campaign)
end
format.js # create.js.rjs
format.json { render :json => @lead, :status => :created, :location => @lead }
format.xml { render :xml => @lead, :status => :created, :location => @lead }
else
format.js # create.js.rjs
format.json { render :json => @lead.errors, :status => :unprocessable_entity }
format.xml { render :xml => @lead.errors, :status => :unprocessable_entity }
end
end
end

# PUT /leads/1
# PUT /leads/1.json
# PUT /leads/1.xml
#----------------------------------------------------------------------------
def update
Expand All @@ -127,20 +137,23 @@ def update
if @lead.update_with_permissions(params[:lead], params[:users])
update_sidebar
format.js
format.json { head :ok }
format.xml { head :ok }
else
@users = User.except(@current_user)
@campaigns = Campaign.my.order("name")
format.js
format.json { render :json => @lead.errors, :status => :unprocessable_entity }
format.xml { render :xml => @lead.errors, :status => :unprocessable_entity }
end
end

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

# DELETE /leads/1
# DELETE /leads/1.json
# DELETE /leads/1.xml HTML and AJAX
#----------------------------------------------------------------------------
def destroy
Expand All @@ -150,11 +163,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

# GET /leads/1/convert
Expand All @@ -172,10 +186,11 @@ def convert

rescue ActiveRecord::RecordNotFound
@previous ||= $1.to_i
respond_to_not_found(:js, :xml) unless @lead
respond_to_not_found(:js, :json, :xml) unless @lead
end

# PUT /leads/1/promote
# PUT /leads/1/promote.json
# PUT /leads/1/promote.xml AJAX
#----------------------------------------------------------------------------
def promote
Expand All @@ -190,18 +205,21 @@ def promote
@lead.convert
update_sidebar
format.js # promote.js.rjs
format.json { head :ok }
format.xml { head :ok }
else
format.js # promote.js.rjs
format.json { render :json => @account.errors + @opportunity.errors + @contact.errors, :status => :unprocessable_entity }
format.xml { render :xml => @account.errors + @opportunity.errors + @contact.errors, :status => :unprocessable_entity }
end
end

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

# PUT /leads/1/reject
# PUT /leads/1/reject.json
# PUT /leads/1/reject.xml AJAX and HTML
#----------------------------------------------------------------------------
def reject
Expand All @@ -212,11 +230,12 @@ def reject
respond_to do |format|
format.html { flash[:notice] = t(:msg_asset_rejected, @lead.full_name); redirect_to leads_path }
format.js # reject.js.rjs
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

# GET /leads/search/query AJAX
Expand All @@ -226,6 +245,7 @@ def search

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

0 comments on commit 5d042b1

Please sign in to comment.