diff --git a/app/controllers/my_profile/cms_controller.rb b/app/controllers/my_profile/cms_controller.rb index 3d8aeb9..52857e0 100644 --- a/app/controllers/my_profile/cms_controller.rb +++ b/app/controllers/my_profile/cms_controller.rb @@ -49,6 +49,9 @@ class CmsController < MyProfileController if profile.enterprise? articles << EnterpriseHomepage end + if @parent && @parent.blog? + articles -= Article.folder_types.map(&:constantize) + end articles end @@ -109,6 +112,7 @@ class CmsController < MyProfileController # user must choose an article type first + @parent = profile.articles.find(params[:parent_id]) if params && params[:parent_id] record_coming @type = params[:type] if @type.blank? diff --git a/app/models/article.rb b/app/models/article.rb index 199791e..7187ba2 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -339,8 +339,13 @@ class Article < ActiveRecord::Base end end + def self.folder_types + ['Folder', 'Blog', 'Forum', 'Gallery'] + end + named_scope :published, :conditions => { :published => true } - named_scope :folders, :conditions => { :type => ['Folder', 'Blog', 'Forum', 'Gallery'] } + named_scope :folders, :conditions => { :type => folder_types} + named_scope :no_folders, :conditions => ['type NOT IN (?)', folder_types] named_scope :galleries, :conditions => { :type => 'Gallery' } named_scope :images, :conditions => { :is_image => true } diff --git a/app/models/blog.rb b/app/models/blog.rb index d4c1c6c..38ab37a 100644 --- a/app/models/blog.rb +++ b/app/models/blog.rb @@ -2,6 +2,13 @@ class Blog < Folder acts_as_having_posts + #FIXME This should be used until there is a migration to fix all blogs that + # already have folders inside them + def posts_with_no_folders + posts_without_no_folders.no_folders + end + alias_method_chain :posts, :no_folders + def self.short_description _('Blog') end diff --git a/app/models/folder.rb b/app/models/folder.rb index f72a8ac..704e608 100644 --- a/app/models/folder.rb +++ b/app/models/folder.rb @@ -1,5 +1,11 @@ class Folder < Article + validate :not_belong_to_blog + + def not_belong_to_blog + errors.add(:parent, "A folder should not belong to a blog.") if parent && parent.blog? + end + acts_as_having_settings :field => :setting xss_terminate :only => [ :body ], :with => 'white_list', :on => 'validation' diff --git a/test/functional/cms_controller_test.rb b/test/functional/cms_controller_test.rb index 58006fa..b840ce5 100644 --- a/test/functional/cms_controller_test.rb +++ b/test/functional/cms_controller_test.rb @@ -818,6 +818,15 @@ class CmsControllerTest < Test::Unit::TestCase assert_no_tag :tag => 'a', :attributes => { :href => "/myprofile/#{profile.identifier}/cms/new?type=Forum"} end + should 'not offer folders if in a blog' do + blog = fast_create(Blog, :profile_id => profile.id) + get :new, :profile => profile.identifier, :parent_id => blog.id, :cms => true + types = assigns(:article_types).map {|t| t[:name]} + Article.folder_types.each do |type| + assert_not_includes types, type + end + end + should 'offer to edit a blog' do profile.articles << Blog.new(:name => 'blog test', :profile => profile) diff --git a/test/functional/content_viewer_controller_test.rb b/test/functional/content_viewer_controller_test.rb index ccfa9a6..8af42e6 100644 --- a/test/functional/content_viewer_controller_test.rb +++ b/test/functional/content_viewer_controller_test.rb @@ -714,8 +714,8 @@ class ContentViewerControllerTest < Test::Unit::TestCase should 'add meta tag to rss feed on view post blog' do login_as(profile.identifier) - profile.articles << Blog.new(:name => 'Blog', :profile => profile) - profile.blog.posts << TextileArticle.new(:name => 'first post', :parent => profile.blog, :profile => profile) + blog = Blog.create!(:name => 'Blog', :profile => profile) + TextileArticle.create!(:name => 'first post', :parent => blog, :profile => profile) get :view_page, :profile => profile.identifier, :page => ['blog', 'first-post'] assert_tag :tag => 'link', :attributes => { :rel => 'alternate', :type => 'application/rss+xml', :title => 'Blog', :href => "http://#{environment.default_hostname}/testinguser/blog/feed" } end @@ -1208,9 +1208,8 @@ class ContentViewerControllerTest < Test::Unit::TestCase FastGettext.stubs(:locale).returns('es') blog = fast_create(Blog, :profile_id => profile.id, :path => 'blog') blog.stubs(:display_posts_in_current_language).returns(true) - en_article = fast_create(TextileArticle, :profile_id => @profile.id, :path => 'en_article', :language => 'en') - es_article = fast_create(TextileArticle, :profile_id => @profile.id, :path => 'es_article', :language => 'es', :translation_of_id => en_article) - blog.posts = [en_article, es_article] + en_article = fast_create(TextileArticle, :profile_id => @profile.id, :path => 'en_article', :language => 'en', :parent_id => blog.id) + es_article = fast_create(TextileArticle, :profile_id => @profile.id, :path => 'es_article', :language => 'es', :parent_id => blog.id, :translation_of_id => en_article) get :view_page, :profile => @profile.identifier, :page => blog.explode_path assert_tag :div, :attributes => { :id => "post-#{es_article.id}" } @@ -1232,8 +1231,8 @@ class ContentViewerControllerTest < Test::Unit::TestCase FastGettext.stubs(:locale).returns('es') blog = fast_create(Blog, :profile_id => profile.id, :path => 'blog') blog.stubs(:display_posts_in_current_language).returns(true) - en_article = fast_create(TextileArticle, :profile_id => @profile.id, :path => 'en_article', :language => 'en') - es_article = fast_create(TextileArticle, :profile_id => @profile.id, :path => 'es_article', :language => 'es', :translation_of_id => en_article) + en_article = fast_create(TextileArticle, :profile_id => @profile.id, :path => 'en_article', :language => 'en', :parent_id => blog.id) + es_article = fast_create(TextileArticle, :profile_id => @profile.id, :path => 'es_article', :language => 'es', :parent_id => blog.id, :translation_of_id => en_article) blog.posts = [en_article, es_article] get :view_page, :profile => @profile.identifier, :page => blog.explode_path diff --git a/test/functional/profile_controller_test.rb b/test/functional/profile_controller_test.rb index 88ddc10..331f45a 100644 --- a/test/functional/profile_controller_test.rb +++ b/test/functional/profile_controller_test.rb @@ -482,9 +482,9 @@ class ProfileControllerTest < Test::Unit::TestCase should 'show number of published posts in index' do profile.articles << blog = Blog.create(:name => 'Blog', :profile_id => profile.id) - blog.posts << TextileArticle.new(:name => 'Published post', :parent => profile.blog, :profile => profile) - blog.posts << TextileArticle.new(:name => 'Other published post', :parent => profile.blog, :profile => profile) - blog.posts << TextileArticle.new(:name => 'Unpublished post', :parent => profile.blog, :profile => profile, :published => false) + fast_create(TextileArticle, :name => 'Published post', :parent_id => profile.blog.id, :profile_id => profile.id) + fast_create(TextileArticle, :name => 'Other published post', :parent_id => profile.blog.id, :profile_id => profile.id) + fast_create(TextileArticle, :name => 'Unpublished post', :parent_id => profile.blog.id, :profile_id => profile.id, :published => false) get :index, :profile => profile.identifier assert_tag :tag => 'a', :content => '2 posts', :attributes => { :href => /\/testuser\/blog/ } diff --git a/test/unit/article_test.rb b/test/unit/article_test.rb index 3717b5d..2544acb 100644 --- a/test/unit/article_test.rb +++ b/test/unit/article_test.rb @@ -1444,4 +1444,34 @@ class ArticleTest < Test::Unit::TestCase assert !Article.new.tiny_mce? end + should 'return only folders' do + not_folders = [RssFeed, TinyMceArticle, Event, TextileArticle] + folders = [Folder, Blog, Gallery, Forum] + + not_folders.each do |klass| + item = fast_create(klass) + assert_not_includes Article.folders, item + end + + folders.each do |klass| + item = fast_create(klass) + assert_includes Article.folders, item + end + end + + should 'return no folders' do + not_folders = [RssFeed, TinyMceArticle, Event, TextileArticle] + folders = [Folder, Blog, Gallery, Forum] + + not_folders.each do |klass| + item = fast_create(klass) + assert_includes Article.no_folders, item + end + + folders.each do |klass| + item = fast_create(klass) + assert_not_includes Article.no_folders, item + end + end + end diff --git a/test/unit/blog_test.rb b/test/unit/blog_test.rb index e451928..61e492f 100644 --- a/test/unit/blog_test.rb +++ b/test/unit/blog_test.rb @@ -195,4 +195,15 @@ class BlogTest < ActiveSupport::TestCase assert !blog.reload.display_posts_in_current_language? end + #FIXME This should be used until there is a migration to fix all blogs that + # already have folders inside them + should 'not list folders in posts' do + blog = fast_create(Blog) + folder = fast_create(Folder, :parent_id => blog.id) + article = fast_create(TextileArticle, :parent_id => blog.id) + + assert_not_includes blog.posts, folder + assert_includes blog.posts, article + end + end diff --git a/test/unit/folder_test.rb b/test/unit/folder_test.rb index 561636d..96e36e3 100644 --- a/test/unit/folder_test.rb +++ b/test/unit/folder_test.rb @@ -132,4 +132,12 @@ class FolderTest < ActiveSupport::TestCase assert_no_match /[<>]/, folder.body end + should 'not have a blog as parent' do + folder = Folder.new + folder.parent = Blog.new + folder.valid? + + assert folder.errors.on(:parent) + end + end diff --git a/test/unit/rss_feed_test.rb b/test/unit/rss_feed_test.rb index 511e033..be8df85 100644 --- a/test/unit/rss_feed_test.rb +++ b/test/unit/rss_feed_test.rb @@ -212,7 +212,10 @@ class RssFeedTest < Test::Unit::TestCase a = ApproveArticle.create!(:name => 'test name', :article => article, :target => profile, :requestor => fast_create(Person)) a.finish - blog.posts << published_article = article.class.last + published_article = article.class.last + published_article.parent = blog + published_article.save + feed = RssFeed.new(:parent => blog, :profile => profile) assert_match "This is the content of the Sample Article", feed.data -- libgit2 0.21.2