diff options
author | Eric Davis <edavis@littlestreamsoftware.com> | 2010-06-05 03:52:59 +0000 |
---|---|---|
committer | Eric Davis <edavis@littlestreamsoftware.com> | 2010-06-05 03:52:59 +0000 |
commit | 345301284a9b47d98bf7af8c09d1c6301b9a2a81 (patch) | |
tree | 035467003f280740aa1b357e3c651e229413b861 /app | |
parent | f484fe855662f20c3716dd157c6c80a77f8f3505 (diff) | |
download | redmine-345301284a9b47d98bf7af8c09d1c6301b9a2a81.tar.gz redmine-345301284a9b47d98bf7af8c09d1c6301b9a2a81.zip |
Added JSON support to the issues API. #1214
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@3766 e93f8b46-1217-0410-a6f0-8f06a7374b81
Diffstat (limited to 'app')
-rw-r--r-- | app/controllers/application_controller.rb | 7 | ||||
-rw-r--r-- | app/controllers/issues_controller.rb | 9 |
2 files changed, 15 insertions, 1 deletions
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 54339b4e9..909125475 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -349,4 +349,11 @@ class ApplicationController < ActionController::Base render_error "An error occurred while executing the query and has been logged. Please report this error to your Redmine administrator." end + # Converts the errors on an ActiveRecord object into a common JSON format + def object_errors_to_json(object) + object.errors.collect do |attribute, error| + { attribute => error } + end.to_json + end + end diff --git a/app/controllers/issues_controller.rb b/app/controllers/issues_controller.rb index 6b42ce62b..8b5d73fa3 100644 --- a/app/controllers/issues_controller.rb +++ b/app/controllers/issues_controller.rb @@ -82,6 +82,7 @@ class IssuesController < ApplicationController respond_to do |format| format.html { render :template => 'issues/index.rhtml', :layout => !request.xhr? } format.xml { render :layout => false } + format.json { render :text => @issues.to_json, :layout => false } format.atom { render_feed(@issues, :title => "#{@project || Setting.app_title}: #{l(:label_issue_plural)}") } format.csv { send_data(issues_to_csv(@issues, @project), :type => 'text/csv; header=present', :filename => 'export.csv') } format.pdf { send_data(issues_to_pdf(@issues, @project, @query), :type => 'application/pdf', :filename => 'export.pdf') } @@ -122,6 +123,7 @@ class IssuesController < ApplicationController respond_to do |format| format.html { render :template => 'issues/show.rhtml' } format.xml { render :layout => false } + format.json { render :text => @issue.to_json, :layout => false } format.atom { render :action => 'changes', :layout => false, :content_type => 'application/atom+xml' } format.pdf { send_data(issue_to_pdf(@issue), :type => 'application/pdf', :filename => "#{@project.identifier}-#{@issue.id}.pdf") } end @@ -146,12 +148,14 @@ class IssuesController < ApplicationController { :action => 'show', :id => @issue }) } format.xml { render :action => 'show', :status => :created, :location => url_for(:controller => 'issues', :action => 'show', :id => @issue) } + format.json { render :text => @issue.to_json, :status => :created, :location => url_for(:controller => 'issues', :action => 'show'), :layout => false } end return else respond_to do |format| format.html { render :action => 'new' } format.xml { render(:xml => @issue.errors, :status => :unprocessable_entity); return } + format.json { render :text => object_errors_to_json(@issue), :status => :unprocessable_entity, :layout => false } end end end @@ -181,6 +185,7 @@ class IssuesController < ApplicationController respond_to do |format| format.html { redirect_back_or_default({:action => 'show', :id => @issue}) } format.xml { head :ok } + format.json { head :ok } end else render_attachment_warning_if_needed(@issue) @@ -190,6 +195,7 @@ class IssuesController < ApplicationController respond_to do |format| format.html { render :action => 'edit' } format.xml { render :xml => @issue.errors, :status => :unprocessable_entity } + format.json { render :text => object_errors_to_json(@issue), :status => :unprocessable_entity, :layout => false } end end end @@ -305,7 +311,7 @@ class IssuesController < ApplicationController TimeEntry.update_all("issue_id = #{reassign_to.id}", ['issue_id IN (?)', @issues]) end else - unless params[:format] == 'xml' + unless params[:format] == 'xml' || params[:format] == 'json' # display the destroy form if it's a user request return end @@ -315,6 +321,7 @@ class IssuesController < ApplicationController respond_to do |format| format.html { redirect_to :action => 'index', :project_id => @project } format.xml { head :ok } + format.json { head :ok } end end |