Commit 9152b209f308c3fd448ce9246f165b246a8d9a6c

Authored by Rodrigo Souto
Committed by Daniela Feitosa
1 parent ac0943b0

Blogs can't have folders inside

* Forbidding folders to be created inside blogs.
  * Avoiding folders on the blog's posts until it's decided what's going
    to be done the the folders that are already inside blogs
    (migration).

(ActionItem1892)
app/controllers/my_profile/cms_controller.rb
@@ -49,6 +49,9 @@ class CmsController < MyProfileController @@ -49,6 +49,9 @@ class CmsController < MyProfileController
49 if profile.enterprise? 49 if profile.enterprise?
50 articles << EnterpriseHomepage 50 articles << EnterpriseHomepage
51 end 51 end
  52 + if @parent && @parent.blog?
  53 + articles -= Article.folder_types.map(&:constantize)
  54 + end
52 articles 55 articles
53 end 56 end
54 57
@@ -109,6 +112,7 @@ class CmsController &lt; MyProfileController @@ -109,6 +112,7 @@ class CmsController &lt; MyProfileController
109 112
110 # user must choose an article type first 113 # user must choose an article type first
111 114
  115 + @parent = profile.articles.find(params[:parent_id]) if params && params[:parent_id]
112 record_coming 116 record_coming
113 @type = params[:type] 117 @type = params[:type]
114 if @type.blank? 118 if @type.blank?
app/models/article.rb
@@ -339,8 +339,13 @@ class Article &lt; ActiveRecord::Base @@ -339,8 +339,13 @@ class Article &lt; ActiveRecord::Base
339 end 339 end
340 end 340 end
341 341
  342 + def self.folder_types
  343 + ['Folder', 'Blog', 'Forum', 'Gallery']
  344 + end
  345 +
342 named_scope :published, :conditions => { :published => true } 346 named_scope :published, :conditions => { :published => true }
343 - named_scope :folders, :conditions => { :type => ['Folder', 'Blog', 'Forum', 'Gallery'] } 347 + named_scope :folders, :conditions => { :type => folder_types}
  348 + named_scope :no_folders, :conditions => ['type NOT IN (?)', folder_types]
344 named_scope :galleries, :conditions => { :type => 'Gallery' } 349 named_scope :galleries, :conditions => { :type => 'Gallery' }
345 named_scope :images, :conditions => { :is_image => true } 350 named_scope :images, :conditions => { :is_image => true }
346 351
app/models/blog.rb
@@ -2,6 +2,13 @@ class Blog &lt; Folder @@ -2,6 +2,13 @@ class Blog &lt; Folder
2 2
3 acts_as_having_posts 3 acts_as_having_posts
4 4
  5 + #FIXME This should be used until there is a migration to fix all blogs that
  6 + # already have folders inside them
  7 + def posts_with_no_folders
  8 + posts_without_no_folders.no_folders
  9 + end
  10 + alias_method_chain :posts, :no_folders
  11 +
5 def self.short_description 12 def self.short_description
6 _('Blog') 13 _('Blog')
7 end 14 end
app/models/folder.rb
1 class Folder < Article 1 class Folder < Article
2 2
  3 + validate :not_belong_to_blog
  4 +
  5 + def not_belong_to_blog
  6 + errors.add(:parent, "A folder should not belong to a blog.") if parent && parent.blog?
  7 + end
  8 +
3 acts_as_having_settings :field => :setting 9 acts_as_having_settings :field => :setting
4 10
5 xss_terminate :only => [ :body ], :with => 'white_list', :on => 'validation' 11 xss_terminate :only => [ :body ], :with => 'white_list', :on => 'validation'
test/functional/cms_controller_test.rb
@@ -818,6 +818,15 @@ class CmsControllerTest &lt; Test::Unit::TestCase @@ -818,6 +818,15 @@ class CmsControllerTest &lt; Test::Unit::TestCase
818 assert_no_tag :tag => 'a', :attributes => { :href => "/myprofile/#{profile.identifier}/cms/new?type=Forum"} 818 assert_no_tag :tag => 'a', :attributes => { :href => "/myprofile/#{profile.identifier}/cms/new?type=Forum"}
819 end 819 end
820 820
  821 + should 'not offer folders if in a blog' do
  822 + blog = fast_create(Blog, :profile_id => profile.id)
  823 + get :new, :profile => profile.identifier, :parent_id => blog.id, :cms => true
  824 + types = assigns(:article_types).map {|t| t[:name]}
  825 + Article.folder_types.each do |type|
  826 + assert_not_includes types, type
  827 + end
  828 + end
  829 +
821 should 'offer to edit a blog' do 830 should 'offer to edit a blog' do
822 profile.articles << Blog.new(:name => 'blog test', :profile => profile) 831 profile.articles << Blog.new(:name => 'blog test', :profile => profile)
823 832
test/functional/content_viewer_controller_test.rb
@@ -714,8 +714,8 @@ class ContentViewerControllerTest &lt; Test::Unit::TestCase @@ -714,8 +714,8 @@ class ContentViewerControllerTest &lt; Test::Unit::TestCase
714 714
715 should 'add meta tag to rss feed on view post blog' do 715 should 'add meta tag to rss feed on view post blog' do
716 login_as(profile.identifier) 716 login_as(profile.identifier)
717 - profile.articles << Blog.new(:name => 'Blog', :profile => profile)  
718 - profile.blog.posts << TextileArticle.new(:name => 'first post', :parent => profile.blog, :profile => profile) 717 + blog = Blog.create!(:name => 'Blog', :profile => profile)
  718 + TextileArticle.create!(:name => 'first post', :parent => blog, :profile => profile)
719 get :view_page, :profile => profile.identifier, :page => ['blog', 'first-post'] 719 get :view_page, :profile => profile.identifier, :page => ['blog', 'first-post']
720 assert_tag :tag => 'link', :attributes => { :rel => 'alternate', :type => 'application/rss+xml', :title => 'Blog', :href => "http://#{environment.default_hostname}/testinguser/blog/feed" } 720 assert_tag :tag => 'link', :attributes => { :rel => 'alternate', :type => 'application/rss+xml', :title => 'Blog', :href => "http://#{environment.default_hostname}/testinguser/blog/feed" }
721 end 721 end
@@ -1208,9 +1208,8 @@ class ContentViewerControllerTest &lt; Test::Unit::TestCase @@ -1208,9 +1208,8 @@ class ContentViewerControllerTest &lt; Test::Unit::TestCase
1208 FastGettext.stubs(:locale).returns('es') 1208 FastGettext.stubs(:locale).returns('es')
1209 blog = fast_create(Blog, :profile_id => profile.id, :path => 'blog') 1209 blog = fast_create(Blog, :profile_id => profile.id, :path => 'blog')
1210 blog.stubs(:display_posts_in_current_language).returns(true) 1210 blog.stubs(:display_posts_in_current_language).returns(true)
1211 - en_article = fast_create(TextileArticle, :profile_id => @profile.id, :path => 'en_article', :language => 'en')  
1212 - es_article = fast_create(TextileArticle, :profile_id => @profile.id, :path => 'es_article', :language => 'es', :translation_of_id => en_article)  
1213 - blog.posts = [en_article, es_article] 1211 + en_article = fast_create(TextileArticle, :profile_id => @profile.id, :path => 'en_article', :language => 'en', :parent_id => blog.id)
  1212 + es_article = fast_create(TextileArticle, :profile_id => @profile.id, :path => 'es_article', :language => 'es', :parent_id => blog.id, :translation_of_id => en_article)
1214 1213
1215 get :view_page, :profile => @profile.identifier, :page => blog.explode_path 1214 get :view_page, :profile => @profile.identifier, :page => blog.explode_path
1216 assert_tag :div, :attributes => { :id => "post-#{es_article.id}" } 1215 assert_tag :div, :attributes => { :id => "post-#{es_article.id}" }
@@ -1232,8 +1231,8 @@ class ContentViewerControllerTest &lt; Test::Unit::TestCase @@ -1232,8 +1231,8 @@ class ContentViewerControllerTest &lt; Test::Unit::TestCase
1232 FastGettext.stubs(:locale).returns('es') 1231 FastGettext.stubs(:locale).returns('es')
1233 blog = fast_create(Blog, :profile_id => profile.id, :path => 'blog') 1232 blog = fast_create(Blog, :profile_id => profile.id, :path => 'blog')
1234 blog.stubs(:display_posts_in_current_language).returns(true) 1233 blog.stubs(:display_posts_in_current_language).returns(true)
1235 - en_article = fast_create(TextileArticle, :profile_id => @profile.id, :path => 'en_article', :language => 'en')  
1236 - es_article = fast_create(TextileArticle, :profile_id => @profile.id, :path => 'es_article', :language => 'es', :translation_of_id => en_article) 1234 + en_article = fast_create(TextileArticle, :profile_id => @profile.id, :path => 'en_article', :language => 'en', :parent_id => blog.id)
  1235 + es_article = fast_create(TextileArticle, :profile_id => @profile.id, :path => 'es_article', :language => 'es', :parent_id => blog.id, :translation_of_id => en_article)
1237 blog.posts = [en_article, es_article] 1236 blog.posts = [en_article, es_article]
1238 1237
1239 get :view_page, :profile => @profile.identifier, :page => blog.explode_path 1238 get :view_page, :profile => @profile.identifier, :page => blog.explode_path
test/functional/profile_controller_test.rb
@@ -482,9 +482,9 @@ class ProfileControllerTest &lt; Test::Unit::TestCase @@ -482,9 +482,9 @@ class ProfileControllerTest &lt; Test::Unit::TestCase
482 482
483 should 'show number of published posts in index' do 483 should 'show number of published posts in index' do
484 profile.articles << blog = Blog.create(:name => 'Blog', :profile_id => profile.id) 484 profile.articles << blog = Blog.create(:name => 'Blog', :profile_id => profile.id)
485 - blog.posts << TextileArticle.new(:name => 'Published post', :parent => profile.blog, :profile => profile)  
486 - blog.posts << TextileArticle.new(:name => 'Other published post', :parent => profile.blog, :profile => profile)  
487 - blog.posts << TextileArticle.new(:name => 'Unpublished post', :parent => profile.blog, :profile => profile, :published => false) 485 + fast_create(TextileArticle, :name => 'Published post', :parent_id => profile.blog.id, :profile_id => profile.id)
  486 + fast_create(TextileArticle, :name => 'Other published post', :parent_id => profile.blog.id, :profile_id => profile.id)
  487 + fast_create(TextileArticle, :name => 'Unpublished post', :parent_id => profile.blog.id, :profile_id => profile.id, :published => false)
488 488
489 get :index, :profile => profile.identifier 489 get :index, :profile => profile.identifier
490 assert_tag :tag => 'a', :content => '2 posts', :attributes => { :href => /\/testuser\/blog/ } 490 assert_tag :tag => 'a', :content => '2 posts', :attributes => { :href => /\/testuser\/blog/ }
test/unit/article_test.rb
@@ -1444,4 +1444,34 @@ class ArticleTest &lt; Test::Unit::TestCase @@ -1444,4 +1444,34 @@ class ArticleTest &lt; Test::Unit::TestCase
1444 assert !Article.new.tiny_mce? 1444 assert !Article.new.tiny_mce?
1445 end 1445 end
1446 1446
  1447 + should 'return only folders' do
  1448 + not_folders = [RssFeed, TinyMceArticle, Event, TextileArticle]
  1449 + folders = [Folder, Blog, Gallery, Forum]
  1450 +
  1451 + not_folders.each do |klass|
  1452 + item = fast_create(klass)
  1453 + assert_not_includes Article.folders, item
  1454 + end
  1455 +
  1456 + folders.each do |klass|
  1457 + item = fast_create(klass)
  1458 + assert_includes Article.folders, item
  1459 + end
  1460 + end
  1461 +
  1462 + should 'return no folders' do
  1463 + not_folders = [RssFeed, TinyMceArticle, Event, TextileArticle]
  1464 + folders = [Folder, Blog, Gallery, Forum]
  1465 +
  1466 + not_folders.each do |klass|
  1467 + item = fast_create(klass)
  1468 + assert_includes Article.no_folders, item
  1469 + end
  1470 +
  1471 + folders.each do |klass|
  1472 + item = fast_create(klass)
  1473 + assert_not_includes Article.no_folders, item
  1474 + end
  1475 + end
  1476 +
1447 end 1477 end
test/unit/blog_test.rb
@@ -195,4 +195,15 @@ class BlogTest &lt; ActiveSupport::TestCase @@ -195,4 +195,15 @@ class BlogTest &lt; ActiveSupport::TestCase
195 assert !blog.reload.display_posts_in_current_language? 195 assert !blog.reload.display_posts_in_current_language?
196 end 196 end
197 197
  198 + #FIXME This should be used until there is a migration to fix all blogs that
  199 + # already have folders inside them
  200 + should 'not list folders in posts' do
  201 + blog = fast_create(Blog)
  202 + folder = fast_create(Folder, :parent_id => blog.id)
  203 + article = fast_create(TextileArticle, :parent_id => blog.id)
  204 +
  205 + assert_not_includes blog.posts, folder
  206 + assert_includes blog.posts, article
  207 + end
  208 +
198 end 209 end
test/unit/folder_test.rb
@@ -132,4 +132,12 @@ class FolderTest &lt; ActiveSupport::TestCase @@ -132,4 +132,12 @@ class FolderTest &lt; ActiveSupport::TestCase
132 assert_no_match /[<>]/, folder.body 132 assert_no_match /[<>]/, folder.body
133 end 133 end
134 134
  135 + should 'not have a blog as parent' do
  136 + folder = Folder.new
  137 + folder.parent = Blog.new
  138 + folder.valid?
  139 +
  140 + assert folder.errors.on(:parent)
  141 + end
  142 +
135 end 143 end
test/unit/rss_feed_test.rb
@@ -212,7 +212,10 @@ class RssFeedTest &lt; Test::Unit::TestCase @@ -212,7 +212,10 @@ class RssFeedTest &lt; Test::Unit::TestCase
212 a = ApproveArticle.create!(:name => 'test name', :article => article, :target => profile, :requestor => fast_create(Person)) 212 a = ApproveArticle.create!(:name => 'test name', :article => article, :target => profile, :requestor => fast_create(Person))
213 a.finish 213 a.finish
214 214
215 - blog.posts << published_article = article.class.last 215 + published_article = article.class.last
  216 + published_article.parent = blog
  217 + published_article.save
  218 +
216 feed = RssFeed.new(:parent => blog, :profile => profile) 219 feed = RssFeed.new(:parent => blog, :profile => profile)
217 220
218 assert_match "This is the content of the Sample Article", feed.data 221 assert_match "This is the content of the Sample Article", feed.data