Skip to content

Commit

Permalink
Merge pull request #92 from sebicas/respond_to_json
Browse files Browse the repository at this point in the history
Respond to JSON
  • Loading branch information
ndbroadbent committed Nov 30, 2011
2 parents 5884807 + 36e3463 commit b7fa3a6
Show file tree
Hide file tree
Showing 11 changed files with 159 additions and 30 deletions.
20 changes: 17 additions & 3 deletions app/controllers/accounts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class AccountsController < ApplicationController
after_filter :update_recently_viewed, :only => :show

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

# GET /accounts/1
# GET /accounts/1.json
# GET /accounts/1.xml HTML
#----------------------------------------------------------------------------
def show
Expand All @@ -50,14 +53,16 @@ def show

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

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

# GET /accounts/new
# GET /accounts/new.json
# GET /accounts/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 => @account }
format.xml { render :xml => @account }
end
end
Expand All @@ -89,6 +95,7 @@ def edit
end

# POST /accounts
# POST /accounts.json
# POST /accounts.xml AJAX
#----------------------------------------------------------------------------
def create
Expand All @@ -102,15 +109,18 @@ def create
@accounts = get_accounts
get_data_for_sidebar
format.js # create.js.rjs
format.json { render :json => @account, :status => :created, :location => @account }
format.xml { render :xml => @account, :status => :created, :location => @account }
else
format.js # create.js.rjs
format.json { render :json => @account.errors, :status => :unprocessable_entity }
format.xml { render :xml => @account.errors, :status => :unprocessable_entity }
end
end
end

# PUT /accounts/1
# PUT /accounts/1.json
# PUT /accounts/1.xml AJAX
#----------------------------------------------------------------------------
def update
Expand All @@ -120,16 +130,18 @@ def update
if @account.update_with_permissions(params[:account], params[:users])
get_data_for_sidebar
format.js
format.json { head :ok }
format.xml { head :ok }
else
@users = User.except(@current_user) # Need it to redraw [Edit Account] form.
format.js
format.json { render :json => @account.errors, :status => :unprocessable_entity }
format.xml { render :xml => @account.errors, :status => :unprocessable_entity }
end
end

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

# DELETE /accounts/1
Expand All @@ -142,11 +154,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 /accounts/search/query AJAX
Expand All @@ -156,6 +169,7 @@ def search

respond_to do |format|
format.js { render :index }
format.json { render :json => @accounts.as_json }
format.xml { render :xml => @accounts.to_xml }
end
end
Expand Down
12 changes: 8 additions & 4 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ def attach
@campaign = model.reload if model.is_a?(Campaign)

respond_to do |format|
format.js { render "shared/attach" }
format.xml { render :xml => model.reload.to_xml }
format.js { render "shared/attach" }
format.json { render :json => model.reload.as_json }
format.xml { render :xml => model.reload.to_xml }
end

rescue ActiveRecord::RecordNotFound
Expand All @@ -71,8 +72,9 @@ def discard
@campaign = model.reload if model.is_a?(Campaign)

respond_to do |format|
format.js { render "shared/discard" }
format.xml { render :xml => model.reload.to_xml }
format.js { render "shared/discard" }
format.json { render :json => model.reload.as_json }
format.xml { render :xml => model.reload.to_xml }
end

rescue ActiveRecord::RecordNotFound
Expand Down Expand Up @@ -226,6 +228,7 @@ def respond_to_not_found(*types)
respond_to do |format|
format.html { redirect_to :action => :index } if types.include?(:html)
format.js { render(:update) { |page| page.reload } } if types.include?(:js)
format.json { render :text => flash[:warning], :status => :not_found } if types.include?(:json)
format.xml { render :text => flash[:warning], :status => :not_found } if types.include?(:xml)
end
end
Expand All @@ -239,6 +242,7 @@ def respond_to_related_not_found(related, *types)
respond_to do |format|
format.html { redirect_to url } if types.include?(:html)
format.js { render(:update) { |page| page.redirect_to url } } if types.include?(:js)
format.json { render :text => flash[:warning], :status => :not_found } if types.include?(:json)
format.xml { render :text => flash[:warning], :status => :not_found } if types.include?(:xml)
end
end
Expand Down
21 changes: 18 additions & 3 deletions app/controllers/campaigns_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class CampaignsController < ApplicationController
after_filter :update_recently_viewed, :only => :show

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

# GET /campaigns/1
# GET /campaigns/1.json
# GET /campaigns/1.xml HTML
#----------------------------------------------------------------------------
def show
Expand All @@ -50,14 +53,16 @@ def show

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

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

# GET /campaigns/new
# GET /campaigns/new.json
# GET /campaigns/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 => @campaign }
format.xml { render :xml => @campaign }
end
end
Expand All @@ -89,6 +95,7 @@ def edit
end

# POST /campaigns
# POST /campaigns.json
# POST /campaigns.xml AJAX
#----------------------------------------------------------------------------
def create
Expand All @@ -100,15 +107,18 @@ def create
@campaigns = get_campaigns
get_data_for_sidebar
format.js # create.js.rjs
format.json { render :json => @campaign, :status => :created, :location => @campaign }
format.xml { render :xml => @campaign, :status => :created, :location => @campaign }
else
format.js # create.js.rjs
format.json { render :json => @campaign.errors, :status => :unprocessable_entity }
format.xml { render :xml => @campaign.errors, :status => :unprocessable_entity }
end
end
end

# PUT /campaigns/1
# PUT /campaigns/1.json
# PUT /campaigns/1.xml AJAX
#----------------------------------------------------------------------------
def update
Expand All @@ -118,19 +128,22 @@ def update
if @campaign.update_with_permissions(params[:campaign], params[:users])
get_data_for_sidebar if called_from_index_page?
format.js
format.json { head :ok }
format.xml { head :ok }
else
@users = User.except(@current_user) # Need it to redraw [Edit Campaign] form.
format.js
format.json { render :json => @campaign.errors, :status => :unprocessable_entity }
format.xml { render :xml => @campaign.errors, :status => :unprocessable_entity }
end
end

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

# DELETE /campaigns/1
# DELETE /campaigns/1.json
# DELETE /campaigns/1.xml HTML and AJAX
#----------------------------------------------------------------------------
def destroy
Expand All @@ -140,11 +153,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 /campaigns/search/query AJAX
Expand All @@ -154,6 +168,7 @@ def search

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

0 comments on commit b7fa3a6

Please sign in to comment.