Commit d90595acd954e4232abad6402b6fc7c6fd91ddfe

Authored by Victor Costa
Committed by Marcos Pereira
1 parent 75101844

api: accept article type when create articles

lib/noosfero/api/helpers.rb
... ... @@ -108,7 +108,7 @@ require_relative '../../find_by_contents'
108 108 def post_article(asset, params)
109 109 return forbidden! unless current_person.can_post_content?(asset)
110 110  
111   - klass_type= params[:content_type].nil? ? TinyMceArticle.name : params[:content_type]
  111 + klass_type = params[:content_type] || params[:article].delete(:type) || TinyMceArticle.name
112 112 return forbidden! unless ARTICLE_TYPES.include?(klass_type)
113 113  
114 114 article = klass_type.constantize.new(params[:article])
... ...
lib/noosfero/api/v1/articles.rb
... ... @@ -230,7 +230,7 @@ module Noosfero
230 230 parent_article = environment.articles.find(params[:id])
231 231 return forbidden! unless parent_article.allow_create?(current_person)
232 232  
233   - klass_type= params[:content_type].nil? ? 'TinyMceArticle' : params[:content_type]
  233 + klass_type = params[:content_type] || params[:article].delete(:type) || 'TinyMceArticle'
234 234 #FIXME see how to check the article types
235 235 #return forbidden! unless ARTICLE_TYPES.include?(klass_type)
236 236  
... ...
test/api/articles_test.rb
... ... @@ -408,6 +408,18 @@ class ArticlesTest < ActiveSupport::TestCase
408 408 assert_kind_of TextArticle, Article.last
409 409 end
410 410  
  411 + should "#{kind} create article with type passed as parameter" do
  412 + profile = fast_create(kind.camelcase.constantize, :environment_id => environment.id)
  413 + Person.any_instance.stubs(:can_post_content?).with(profile).returns(true)
  414 +
  415 + Article.delete_all
  416 + params[:article] = {:name => "Title", :type => 'TextArticle'}
  417 + post "/api/v1/#{kind.pluralize}/#{profile.id}/articles?#{params.to_query}"
  418 + json = JSON.parse(last_response.body)
  419 +
  420 + assert_kind_of TextArticle, Article.last
  421 + end
  422 +
411 423 should "#{kind}: create article of TinyMceArticle type if no content type is passed as parameter" do
412 424 profile = fast_create(kind.camelcase.constantize, :environment_id => environment.id)
413 425 Person.any_instance.stubs(:can_post_content?).with(profile).returns(true)
... ...