From 331b9925213373466d6bb0e8f3d775cb6a932bea Mon Sep 17 00:00:00 2001 From: AntonioTerceiro Date: Wed, 13 Feb 2008 23:25:28 +0000 Subject: [PATCH] ActionItem154: creating a home page and a RSS feed for new profiles --- app/models/article.rb | 4 ++-- app/models/environment.rb | 8 ++++++-- app/models/person.rb | 12 +++++++++++- app/models/profile.rb | 12 ++++++++++++ test/functional/cms_controller_test.rb | 4 ++-- test/unit/article_test.rb | 28 ++++++++++++++++++++++++---- test/unit/enterprise_test.rb | 7 +++++++ test/unit/environment_test.rb | 23 +++++++++++++++++------ test/unit/person_test.rb | 7 +++++++ test/unit/profile_test.rb | 29 ++++++++++++++++------------- test/unit/recent_documents_block_test.rb | 12 ++++++------ test/unit/rss_feed_test.rb | 16 ++++++++-------- 12 files changed, 118 insertions(+), 44 deletions(-) diff --git a/app/models/article.rb b/app/models/article.rb index acc01c1..7f52d8e 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -7,7 +7,7 @@ class Article < ActiveRecord::Base belongs_to :last_changed_by, :class_name => Person.name, :foreign_key => 'last_changed_by_id' - has_many :comments + has_many :comments, :dependent => :destroy has_and_belongs_to_many :categories @@ -29,7 +29,7 @@ class Article < ActiveRecord::Base # retrieves the latest +limit+ articles, sorted from the most recent to the # oldest. def self.recent(limit) - options = { :limit => limit, :order => 'created_on' } + options = { :limit => limit, :order => 'created_on desc, articles.id desc' } self.find(:all, options) end diff --git a/app/models/environment.rb b/app/models/environment.rb index 2401708..ffdaffa 100644 --- a/app/models/environment.rb +++ b/app/models/environment.rb @@ -31,13 +31,17 @@ class Environment < ActiveRecord::Base 3.times do env.boxes << Box.new end + + # main area env.boxes[0].blocks << MainBlock.new + # "left" area + env.boxes[1].blocks << LoginBlock.new env.boxes[1].blocks << EnvironmentStatisticsBlock.new env.boxes[1].blocks << RecentDocumentsBlock.new - env.boxes[2].blocks << LoginBlock.new - + # "right" area + env.boxes[2].blocks << ProfileListBlock.new end # One Environment can be reached by many domains diff --git a/app/models/person.rb b/app/models/person.rb index 9fdca97..e11fdc7 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -64,12 +64,22 @@ class Person < Profile 3.times do self.boxes << Box.new end + + # main area self.boxes.first.blocks << MainBlock.new + + # "left" area + self.boxes[1].blocks << ProfileInfoBlock.new + + # right area + self.boxes[2].blocks << TagsBlock.new + self.boxes[2].blocks << RecentDocumentsBlock.new true end - # FIXME this is *weird*, because this class is not inheriting the callback + # FIXME this is *weird*, because this class is not inheriting the callbacks before_create :set_default_environment + after_create :insert_default_homepage_and_feed end diff --git a/app/models/profile.rb b/app/models/profile.rb index 640c51f..bbc0f5b 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -232,4 +232,16 @@ class Profile < ActiveRecord::Base false end + after_create :insert_default_homepage_and_feed + def insert_default_homepage_and_feed + hp = self.articles.build(:name => _('Home page'), :body => _("

%s's home page

This is a default homepage created for %s. It can be changed though the control panel.

") % [ self.name, self.name]) + hp.save! + self.home_page = hp + self.save! + + feed = RssFeed.new(:name => 'feed') + self.articles << feed + feed.save! + end + end diff --git a/test/functional/cms_controller_test.rb b/test/functional/cms_controller_test.rb index 2aee3f9..8aa2afe 100644 --- a/test/functional/cms_controller_test.rb +++ b/test/functional/cms_controller_test.rb @@ -145,7 +145,7 @@ class CmsControllerTest < Test::Unit::TestCase should 'be able to create a RSS feed' do login_as(profile.identifier) assert_difference RssFeed, :count do - post :new, :type => RssFeed.name, :profile => profile.identifier, :article => { :name => 'feed', :limit => 15, :include => 'all', :feed_item_description => 'body' } + post :new, :type => RssFeed.name, :profile => profile.identifier, :article => { :name => 'new-feed', :limit => 15, :include => 'all', :feed_item_description => 'body' } assert_response :redirect end end @@ -171,7 +171,7 @@ class CmsControllerTest < Test::Unit::TestCase should 'be able to update an uploaded file' do post :new, :type => UploadedFile.name, :profile => profile.identifier, :article => { :uploaded_data => fixture_file_upload('/files/test.txt', 'text/plain')} - file = profile.articles.find(:first) + file = profile.articles.find_by_path('test.txt') assert_equal 'test.txt', file.name post :edit, :profile => profile.identifier, :id => file.id, :article => { :uploaded_data => fixture_file_upload('/files/test.txt', 'text/plain')} diff --git a/test/unit/article_test.rb b/test/unit/article_test.rb index da7255f..b9e7159 100644 --- a/test/unit/article_test.rb +++ b/test/unit/article_test.rb @@ -133,17 +133,20 @@ class ArticleTest < Test::Unit::TestCase end should 'search for recent documents' do + other_profile = create_user('otherpropfile').person + + Article.destroy_all + first = profile.articles.build(:name => 'first'); first.save! second = profile.articles.build(:name => 'second'); second.save! third = profile.articles.build(:name => 'third'); third.save! - forth = profile.articles.build(:name => 'forth'); forth.save! + fourth = profile.articles.build(:name => 'fourth'); fourth.save! fifth = profile.articles.build(:name => 'fifth'); fifth.save! - other_profile = create_user('otherpropfile').person other_first = other_profile.articles.build(:name => 'first'); other_first.save! - assert_equal [first,second,third], Article.recent(3) - assert_equal [first,second,third,forth,fifth,other_first], Article.recent(10) + assert_equal [other_first, fifth, fourth], Article.recent(3) + assert_equal [other_first, fifth, fourth, third, second, first], Article.recent(6) end should 'provied proper descriptions' do @@ -190,4 +193,21 @@ class ArticleTest < Test::Unit::TestCase end end + should 'remove comments when removing article' do + assert_no_difference Comment, :count do + a = profile.articles.build(:name => 'test article') + a.save! + + assert_difference Comment, :count, 1 do + comment = a.comments.build + comment.author = profile + comment.title = 'test comment' + comment.body = 'you suck!' + comment.save! + end + + a.destroy + end + end + end diff --git a/test/unit/enterprise_test.rb b/test/unit/enterprise_test.rb index f886d41..d099365 100644 --- a/test/unit/enterprise_test.rb +++ b/test/unit/enterprise_test.rb @@ -55,4 +55,11 @@ class EnterpriseTest < Test::Unit::TestCase end end + should 'get a default homepage and RSS feed' do + enterprise = Enterprise.create!(:name => 'my test enterprise', :identifier => 'myenterprise') + + assert_kind_of Article, enterprise.home_page + assert_kind_of RssFeed, enterprise.articles.find_by_path('feed') + end + end diff --git a/test/unit/environment_test.rb b/test/unit/environment_test.rb index c4600fd..fcae982 100644 --- a/test/unit/environment_test.rb +++ b/test/unit/environment_test.rb @@ -222,22 +222,33 @@ class EnvironmentTest < Test::Unit::TestCase should 'provide recent_documents' do environment = Environment.create(:name => 'a test environment') + p1 = environment.profiles.build(:identifier => 'testprofile1', :name => 'test profile 1'); p1.save! + p2 = environment.profiles.build(:identifier => 'testprofile2', :name => 'test profile 2'); p2.save! + + # clear the articles + Article.destroy_all + # p1 creates one article - p1 = environment.profiles.build(:identifier => 'testprofile1', :name => 'test profile 1') - p1.save! doc1 = p1.articles.build(:name => 'text 1'); doc1.save! # p2 creates two articles - p2 = environment.profiles.build(:identifier => 'testprofile2', :name => 'test profile 2') - p2.save! doc2 = p2.articles.build(:name => 'text 2'); doc2.save! doc3 = p2.articles.build(:name => 'text 3'); doc3.save! # p1 creates another article doc4 = p1.articles.build(:name => 'text 4'); doc4.save! - assert_equivalent [doc1,doc2,doc3,doc4], environment.recent_documents - assert_equivalent [doc1,doc2,doc3], environment.recent_documents(3) + all_recent = environment.recent_documents + [doc1,doc2,doc3,doc4].each do |item| + assert_includes all_recent, item + end + + last_three = environment.recent_documents(3) + [doc2, doc3, doc4].each do |item| + assert_includes last_three, item + end + assert_not_includes last_three, doc1 + end should 'have a description attribute' do diff --git a/test/unit/person_test.rb b/test/unit/person_test.rb index 63514da..5870619 100644 --- a/test/unit/person_test.rb +++ b/test/unit/person_test.rb @@ -127,4 +127,11 @@ class PersonTest < Test::Unit::TestCase assert person.blocks.size > 0, 'Person should have blocks upon creation' end + should 'get a default home page and a RSS feed' do + person = create_user('mytestuser').person + + assert_kind_of Article, person.home_page + assert_kind_of RssFeed, person.articles.find_by_path('feed') + end + end diff --git a/test/unit/profile_test.rb b/test/unit/profile_test.rb index f2e2854..f849f56 100644 --- a/test/unit/profile_test.rb +++ b/test/unit/profile_test.rb @@ -72,15 +72,7 @@ class ProfileTest < Test::Unit::TestCase should 'provide access to home page' do profile = Profile.create!(:identifier => 'newprofile', :name => 'New Profile') - assert_nil profile.home_page - - page = profile.articles.build(:name => "My custom home page") - page.save! - - profile.home_page = page - profile.save! - - assert_equal page, profile.home_page + assert_kind_of Article, profile.home_page end def test_name_should_be_mandatory @@ -116,9 +108,10 @@ class ProfileTest < Test::Unit::TestCase second = profile.articles.build(:name => 'second'); second.save! third = profile.articles.build(:name => 'third'); third.save! - n = Article.count + total = Article.count + mine = profile.articles.count profile.destroy - assert_equal n - 3, Article.count + assert_equal total - mine, Article.count end def test_should_define_info @@ -139,12 +132,14 @@ class ProfileTest < Test::Unit::TestCase should 'provide recent documents' do profile = Profile.create!(:name => 'testing profile', :identifier => 'testingprofile') + profile.articles.destroy_all + first = profile.articles.build(:name => 'first'); first.save! second = profile.articles.build(:name => 'second'); second.save! third = profile.articles.build(:name => 'third'); third.save! - assert_equal [first,second], profile.recent_documents(2) - assert_equal [first,second,third], profile.recent_documents + assert_equal [third, second], profile.recent_documents(2) + assert_equal [third, second, first], profile.recent_documents end should 'affiliate and provide a list of the affiliated users' do @@ -318,6 +313,14 @@ class ProfileTest < Test::Unit::TestCase assert_equal false, Profile.new.has_members? end + should 'create a homepage and a feed on creation' do + profile = Profile.create!(:name => 'my test profile', :identifier => 'mytestprofile') + + assert_kind_of Article, profile.home_page + assert_kind_of RssFeed, profile.articles.find_by_path('feed') + end + + private def assert_invalid_identifier(id) diff --git a/test/unit/recent_documents_block_test.rb b/test/unit/recent_documents_block_test.rb index 84d0293..06c2f6f 100644 --- a/test/unit/recent_documents_block_test.rb +++ b/test/unit/recent_documents_block_test.rb @@ -7,7 +7,7 @@ class RecentDocumentsBlockTest < Test::Unit::TestCase profile.articles.build(:name => 'first').save! profile.articles.build(:name => 'second').save! profile.articles.build(:name => 'third').save! - profile.articles.build(:name => 'forth').save! + profile.articles.build(:name => 'fourth').save! profile.articles.build(:name => 'fifth').save! box = Box.create!(:owner => profile) @@ -26,7 +26,7 @@ class RecentDocumentsBlockTest < Test::Unit::TestCase assert_match /href=.*\/testinguser\/first/, output assert_match /href=.*\/testinguser\/second/, output assert_match /href=.*\/testinguser\/third/, output - assert_match /href=.*\/testinguser\/forth/, output + assert_match /href=.*\/testinguser\/fourth/, output assert_match /href=.*\/testinguser\/fifth/, output end @@ -35,11 +35,11 @@ class RecentDocumentsBlockTest < Test::Unit::TestCase output = block.content - assert_match /href=.*\/testinguser\/first/, output - assert_match /href=.*\/testinguser\/second/, output + assert_match /href=.*\/testinguser\/fifth/, output + assert_match /href=.*\/testinguser\/fourth/, output assert_match /href=.*\/testinguser\/third/, output - assert_no_match /href=.*\/testinguser\/forth/, output - assert_no_match /href=.*\/testinguser\/fifth/, output + assert_no_match /href=.*\/testinguser\/second/, output + assert_no_match /href=.*\/testinguser\/first/, output end end diff --git a/test/unit/rss_feed_test.rb b/test/unit/rss_feed_test.rb index ae132ee..a104bc2 100644 --- a/test/unit/rss_feed_test.rb +++ b/test/unit/rss_feed_test.rb @@ -29,7 +29,7 @@ class RssFeedTest < Test::Unit::TestCase a2 = profile.articles.build(:name => 'article 2'); a2.save! a3 = profile.articles.build(:name => 'article 3'); a3.save! - feed = RssFeed.new(:name => 'feed') + feed = RssFeed.new(:name => 'testfeed') feed.profile = profile feed.save! @@ -45,12 +45,12 @@ class RssFeedTest < Test::Unit::TestCase a2 = profile.articles.build(:name => 'article 2'); a2.save! a3 = profile.articles.build(:name => 'article 3'); a3.save! - feed = RssFeed.new(:name => 'feed') + feed = RssFeed.new(:name => 'testfeed') feed.profile = profile feed.save! rss = feed.data - assert_no_match /feed<\/title>/, rss + assert_no_match /<item><title>testfeed<\/title>/, rss end should 'list recent article from parent article' do @@ -66,7 +66,7 @@ class RssFeedTest < Test::Unit::TestCase profile = create_user('testuser').person a1 = profile.articles.build(:name => 'article 1', 'abstract' => 'my abstract', 'body' => 'my text'); a1.save! - feed = RssFeed.new(:name => 'feed') + feed = RssFeed.new(:name => 'testfeed') feed.profile = profile feed.save! @@ -90,7 +90,7 @@ class RssFeedTest < Test::Unit::TestCase a3_2 = a3.children.build(:name => 'article 3.2', :parent => a3, :profile => profile); a3_2.save! a3_2_1 = a3_2.children.build(:name => 'article 3.2.1', :parent => a3_2, :profile => profile); a3_2_1.save! - feed = RssFeed.new(:name => 'feed') + feed = RssFeed.new(:name => 'testfeed') feed.parent = a3 feed.profile = profile feed.include = 'parent_and_children' @@ -108,7 +108,7 @@ class RssFeedTest < Test::Unit::TestCase should 'provide link to profile' do profile = create_user('testuser').person - feed = RssFeed.new(:name => 'feed') + feed = RssFeed.new(:name => 'testfeed') feed.profile = profile feed.save! @@ -118,7 +118,7 @@ class RssFeedTest < Test::Unit::TestCase should 'provide link to each article' do profile = create_user('testuser').person art = profile.articles.build(:name => 'myarticle'); art.save! - feed = RssFeed.new(:name => 'feed') + feed = RssFeed.new(:name => 'testfeed') feed.profile = profile feed.save! @@ -133,7 +133,7 @@ class RssFeedTest < Test::Unit::TestCase a2 = profile.articles.build(:name => 'article 2'); a2.save! a3 = profile.articles.build(:name => 'article 3'); a3.save! - feed = RssFeed.new(:name => 'feed') + feed = RssFeed.new(:name => 'testfeed') feed.profile = profile feed.save! -- libgit2 0.21.2