Permalink
Browse files

Correctly handle XML/YAML/JSON requests for core project objects

  • Loading branch information...
1 parent 82d4d90 commit 284d73ce30abe05b22e0c158ed96bc0855a4d572 @jamesu jamesu committed May 3, 2010
@@ -207,6 +207,24 @@ def render(opts = nil, extra_options = {}, &block)
end
end
+ def handle_api_error(f,object)
+ f.xml { render :xml => object.errors.to_xml, :status => :unprocessable_entity }
+ f.json { render :as_json => object.errors.to_xml, :status => :unprocessable_entity }
+ f.yaml { render :as_yaml => object.errors.to_xml, :status => :unprocessable_entity }
+ end
+
+ def handle_api_success(f,object,is_new=false)
+ if is_new
+ f.xml { render :xml => object.to_xml, :status => :created }
+ f.json { render :as_json => object.to_xml, :status => :created }
+ f.yaml { render :as_yaml => object.to_xml, :status => :created }
+ else
+ f.xml { head :ok }
+ f.json { head :ok }
+ f.yaml { head :ok }
+ end
+ end
+
def set_user
@current_user = current_user || nil
end
@@ -44,9 +44,19 @@ def create
end
respond_to do |f|
- f.html { redirect_to redirect_path }
- f.m { redirect_to redirect_path }
- f.js { fetch_new_comments }
+ if !@comment.new_record?
+ # success!
+ f.html { redirect_to redirect_path }
+ f.m { redirect_to redirect_path }
+ f.js { fetch_new_comments }
+ handle_api_success(f, @comment, true)
+ else
+ # error
+ f.html { redirect_to redirect_path }
+ f.m { redirect_to redirect_path }
+ f.js { fetch_new_comments }
+ handle_api_error(f, @comment)
+ end
end
end
@@ -61,10 +71,21 @@ def edit
def update
if @has_permission
- @comment.save_uploads(params) if @comment.update_attributes(params[:comment])
+ @saved = @comment.update_attributes(params[:comment])
+ @comment.save_uploads(params) if @saved
end
- respond_to{|f|f.js}
+ if @saved
+ respond_to do |f|
+ f.js
+ handle_api_success(f, @comment)
+ end
+ else
+ respond_to do |f|
+ f.js
+ handle_api_error(f, @comment)
+ end
+ end
end
def convert
@@ -81,10 +102,16 @@ def convert
end
end
- respond_to do |f|
- f.js {
- render :template => 'comments/update'
- }
+ if !@task.new_record?
+ respond_to do |f|
+ f.js { render :template => 'comments/update' }
+ handle_api_success(f, @task, true)
+ end
+ else
+ respond_to do |f|
+ f.js { render :template => 'comments/update' }
+ handle_api_error(f, @task)
+ end
end
end
@@ -95,6 +122,18 @@ def destroy
else
@has_permission = false
end
+
+ if @has_permission
+ respond_to do |f|
+ f.js
+ handle_api_success(f, @comment)
+ end
+ else
+ respond_to do |f|
+ f.js
+ handle_api_error(f, @comment)
+ end
+ end
end
private
@@ -22,17 +22,13 @@ def create
respond_to do |f|
f.html { redirect_to project_conversation_path(@current_project,@conversation) }
f.m { redirect_to project_conversation_path(@current_project,@conversation) }
- f.xml { redirect_to project_conversation_path(@current_project,@conversation) }
- f.json { redirect_to project_conversation_path(@current_project,@conversation) }
- f.yaml { redirect_to project_conversation_path(@current_project,@conversation) }
+ handle_api_success(f, @conversation, true)
end
else
respond_to do |f|
f.html { render :action => :new }
f.m { render :action => :new }
- f.xml { render :xml => @conversation.errors.to_xml }
- f.json { render :as_json => @conversation.errors.to_xml }
- f.yaml { render :as_yaml => @conversation.errors.to_xml }
+ handle_api_error(f, @conversation)
end
end
end
@@ -68,11 +64,21 @@ def show
end
def update
- @conversation.update_attributes(params[:conversation])
- respond_to do |f|
- f.js
- f.m { redirect_to project_conversation_path(@current_project, @conversation) }
- f.html { redirect_to project_conversation_path(@current_project, @conversation) }
+ @saved = @conversation.update_attributes(params[:conversation])
+ if @saved
+ respond_to do |f|
+ f.js
+ f.m { redirect_to project_conversation_path(@current_project, @conversation) }
+ f.html { redirect_to project_conversation_path(@current_project, @conversation) }
+ handle_api_success(f, @conversation)
+ end
+ else
+ respond_to do |f|
+ f.js
+ f.m { redirect_to project_conversation_path(@current_project, @conversation) }
+ f.html { redirect_to project_conversation_path(@current_project, @conversation) }
+ handle_api_error(f, @conversation)
+ end
end
end
@@ -87,13 +93,15 @@ def destroy
end
f.m { redirect_to project_conversations_path(@current_project) }
f.js
+ handle_api_success(f, @conversation)
end
else
respond_to do |f|
flash[:error] = t('common.not_allowed')
f.html { redirect_to project_conversation_path(@current_project, @conversation) }
f.m { redirect_to project_conversation_path(@current_project, @conversation) }
f.js { render :text => 'alert("Not allowed!");'; }
+ handle_api_error(f, @conversation)
end
end
end
@@ -126,7 +134,7 @@ def load_conversation
redirect_to project_path(@current_project) unless @conversation
end
-
+
def add_watchers(hash)
(hash || []).each do |user_id, should_notify|
if should_notify == "1" and Person.exists? :project_id => @conversation.project_id, :user_id => user_id
@@ -1,17 +1,25 @@
class DividersController < ApplicationController
before_filter :load_page
+ before_filter :load_divider, :only => [:show, :edit, :update, :destroy]
def create
calculate_position
@divider = @page.build_divider(params[:divider])
- save_slot(@divider) if @divider.save
- @divider.save
- respond_to{|f|f.js}
+ save_slot(@divider) if @page.editable?(current_user) && @divider.save
+
+ respond_to do |f|
+ if !@divider.new_record?
+ f.js
+ handle_api_success(f, @divider, true)
+ else
+ f.js
+ handle_api_error(f, @divider)
+ end
+ end
end
def show
- @divider = @page.dividers.find(params[:id])
respond_to do |f|
f.xml { render :xml => @divider.to_xml }
f.json{ render :as_json => @divider.to_xml }
@@ -20,25 +28,53 @@ def show
end
def edit
- @divider = @page.dividers.find(params[:id])
respond_to{|f|f.js}
end
def update
- @divider = @page.dividers.find(params[:id])
- @divider.update_attributes(params[:divider])
- respond_to{|f|f.js}
+ if @divider.editable?(current_user) and @divider.update_attributes(params[:divider])
+ respond_to do |f|
+ f.js
+ handle_api_success(f, @divider)
+ end
+ else
+ respond_to do |f|
+ f.js
+ handle_api_error(f, @divider)
+ end
+ end
end
def destroy
- @divider = @page.dividers.find(params[:id])
- @slot_id = @divider.page_slot.id if @divider
- @divider.destroy if @divider
- respond_to{|f|f.js}
+ @slot_id = @divider.page_slot.id
+
+ if @divider.editable?(current_user)
+ @divider.destroy
+ respond_to do |f|
+ f.js
+ handle_api_success(f, @divider)
+ end
+ else
+ respond_to do |f|
+ f.js
+ handle_api_error(f, @divider)
+ end
+ end
end
private
def load_page
@page = @current_project.pages.find(params[:page_id])
end
+
+ def load_divider
+ begin
+ @divider = @page.dividers.find(params[:id])
+ rescue
+ respond_to do |f|
+ f.js
+ handle_api_error(f, @divider)
+ end
+ end
+ end
end
@@ -1,16 +1,25 @@
class NotesController < ApplicationController
before_filter :load_page
+ before_filter :load_note, :only => [:show, :edit, :update, :destroy]
def create
calculate_position
@note = @page.build_note(params[:note])
- save_slot(@note) if @note.save
- respond_to{|f|f.js}
+ save_slot(@note) if @page.editable?(current_user) && @note.save
+
+ respond_to do |f|
+ if !@note.new_record?
+ f.js
+ handle_api_success(f, @note, true)
+ else
+ f.js
+ handle_api_error(f, @note)
+ end
+ end
end
def show
- @note = @page.notes.find(params[:id])
respond_to do |f|
f.xml { render :xml => @note.to_xml }
f.json{ render :as_json => @note.to_xml }
@@ -19,25 +28,53 @@ def show
end
def edit
- @note = @page.notes.find(params[:id])
respond_to{|f|f.js}
end
def update
- @note = @page.notes.find(params[:id])
- @note.update_attributes(params[:note])
- respond_to{|f|f.js}
+ if @note.editable?(current_user) and @note.update_attributes(params[:note])
+ respond_to do |f|
+ f.js
+ handle_api_success(f, @note)
+ end
+ else
+ respond_to do |f|
+ f.js
+ handle_api_error(f, @note)
+ end
+ end
end
def destroy
- @note = @page.notes.find(params[:id])
- @slot_id = @note.page_slot.id if @note
- @note.destroy if @note
- respond_to{|f|f.js}
+ @slot_id = @note.page_slot.id
+
+ if @note.editable?(current_user)
+ @note.destroy
+ respond_to do |f|
+ f.js
+ handle_api_success(f, @note)
+ end
+ else
+ respond_to do |f|
+ f.js
+ handle_api_error(f, @note)
+ end
+ end
end
private
def load_page
@page = @current_project.pages.find(params[:page_id])
end
+
+ def load_note
+ begin
+ @note = @page.notes.find(params[:id])
+ rescue
+ respond_to do |f|
+ f.js
+ handle_api_error(f, @note)
+ end
+ end
+ end
end
Oops, something went wrong.

0 comments on commit 284d73c

Please sign in to comment.