[Git][noosfero/noosfero][master] 2 commits: api: return model errors when update an article

Leandro Nunes gitlab at mg.gitlab.com
Mon May 22 15:13:25 BRT 2017


Leandro Nunes pushed to branch master at Noosfero / noosfero


Commits:
a6cf5817 by Victor Costa at 2017-05-22T14:03:27-03:00
api: return model errors when update an article

- - - - -
d9a85f03 by Leandro Nunes at 2017-05-22T18:13:18+00:00
Merge branch 'api-article-error' into 'master'

api: return model errors when update an article

See merge request !1215
- - - - -


2 changed files:

- app/api/v1/articles.rb
- test/api/articles_test.rb


Changes:

=====================================
app/api/v1/articles.rb
=====================================
--- a/app/api/v1/articles.rb
+++ b/app/api/v1/articles.rb
@@ -50,8 +50,12 @@ module Api
         post ':id' do
           article = environment.articles.find(params[:id])
           return forbidden! unless article.allow_edit?(current_person)
-          article.update_attributes!(asset_with_image(params[:article]))
-          present_partial article, :with => Entities::Article
+          begin
+            article.update_attributes!(asset_with_image(params[:article]))
+            present_partial article, :with => Entities::Article
+          rescue ActiveRecord::RecordInvalid
+            render_model_errors!(article.errors)
+          end
         end
 
         delete ':id' do


=====================================
test/api/articles_test.rb
=====================================
--- a/test/api/articles_test.rb
+++ b/test/api/articles_test.rb
@@ -871,4 +871,11 @@ class ArticlesTest < ActiveSupport::TestCase
     assert_equal ({"name" => [{"error"=>"blank", "full_message"=>"Title can't be blank"}]}), json["errors"]
   end
 
+  should "return error messages when update an article with invalid data" do
+    params[:article] = {:name => nil}
+    article = fast_create(Article, :profile_id => person.id)
+    post "/api/v1/articles/#{article.id}?#{params.to_query}"
+    json = JSON.parse(last_response.body)
+    assert_equal ({"name" => [{"error"=>"blank", "full_message"=>"Title can't be blank"}]}), json["errors"]
+  end
 end



View it on GitLab: https://gitlab.com/noosfero/noosfero/compare/a8f842cd9fa5b95a29ff081494d406c1468b88e4...d9a85f0303db178305eb07ae4f9b1d92db302b24

---
View it on GitLab: https://gitlab.com/noosfero/noosfero/compare/a8f842cd9fa5b95a29ff081494d406c1468b88e4...d9a85f0303db178305eb07ae4f9b1d92db302b24
You're receiving this email because of your account on gitlab.com.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listas.softwarelivre.org/pipermail/noosfero-dev/attachments/20170522/1ee3bb5b/attachment-0001.html>


More information about the Noosfero-dev mailing list