From fd26529052b18f50d2db4483a6de41ae524169ef Mon Sep 17 00:00:00 2001 From: MoisesMachado Date: Mon, 25 Aug 2008 20:53:34 +0000 Subject: [PATCH] ActionItem591: made new profiles coby articles from templates --- app/models/article.rb | 10 ++++++++++ app/models/profile.rb | 49 +++++++++++++++++++++++++++++++++---------------- test/unit/article_test.rb | 19 +++++++++++++++++++ test/unit/community_test.rb | 21 ++++----------------- test/unit/enterprise_test.rb | 22 +++++----------------- test/unit/person_test.rb | 23 +++++------------------ test/unit/profile_test.rb | 34 +++++++++++++++++++++++++++++++++- 7 files changed, 109 insertions(+), 69 deletions(-) diff --git a/app/models/article.rb b/app/models/article.rb index f8fb795..6db4cd7 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -167,6 +167,16 @@ class Article < ActiveRecord::Base profile.public? && public_article end + def copy(options) + attrs = attributes.reject! { |key, value| article_attr_blacklist.include?(key) } + attrs.merge!(options) + Article.create(attrs) + end + + def article_attr_blacklist + ['id', 'profile_id', 'parent_id', 'slug', 'path', 'updated_at', 'created_at', 'last_changed_by_id', 'version', 'lock_version', 'type', 'children_count', 'comments_count'] + end + private def sanitize_tag_list diff --git a/app/models/profile.rb b/app/models/profile.rb index 0f4a25c..c2b5cf6 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -207,7 +207,7 @@ class Profile < ActiveRecord::Base end end - # this method should be override to provide the correct template + # this method should be overwritten to provide the correct template def template nil end @@ -325,21 +325,38 @@ class Profile < ActiveRecord::Base after_create :insert_default_article_set def insert_default_article_set - # a default homepage - hp = default_homepage(:name => _("%s's home page") % self.name, :body => _("

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

") % self.name, :advertise => false) - hp.profile = self - hp.save! - self.home_page = hp - self.save! - - # a default rss feed - feed = RssFeed.new(:name => 'feed') - self.articles << feed - - # a default private folder if public - if self.public? - folder = Folder.new(:name => _("Intranet"), :public_article => false) - self.articles << folder + if template + copy_articles_from template + else + # a default homepage + hp = default_homepage(:name => _("My home page"), :body => _("

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

"), :advertise => false) + hp.profile = self + hp.save! + self.home_page = hp + self.save! + + # a default rss feed + feed = RssFeed.new(:name => 'feed') + self.articles << feed + + # a default private folder if public + if self.public? + folder = Folder.new(:name => _("Intranet"), :public_article => false) + self.articles << folder + end + end + end + + def copy_articles_from other + other.top_level_articles.each do |a| + copy_article_tree a + end + end + + def copy_article_tree(article, parent=nil) + article_copy = article.copy(:profile => self, :parent => parent) + article.children.each do |a| + copy_article_tree a, article_copy end end diff --git a/test/unit/article_test.rb b/test/unit/article_test.rb index fd41e39..73e1a76 100644 --- a/test/unit/article_test.rb +++ b/test/unit/article_test.rb @@ -414,4 +414,23 @@ class ArticleTest < Test::Unit::TestCase assert article.display_to?(friend) end + should 'make a copy of the article as child of it' do + person = create_user('test_user').person + a = person.articles.create!(:name => 'test article', :body => 'some text') + b = a.copy(:parent => a, :profile => a.profile) + + assert_includes a.children, b + assert_equal 'some text', b.body + end + + should 'make a copy of the article to other profile' do + p1 = create_user('test_user1').person + p2 = create_user('test_user2').person + a = p1.articles.create!(:name => 'test article', :body => 'some text') + b = a.copy(:parent => a, :profile => p2) + + assert_includes p2.articles, b + assert_equal 'some text', b.body + end + end diff --git a/test/unit/community_test.rb b/test/unit/community_test.rb index e5891ca..03fabda 100644 --- a/test/unit/community_test.rb +++ b/test/unit/community_test.rb @@ -82,24 +82,11 @@ class CommunityTest < Test::Unit::TestCase assert !RoleAssignment.exists?(i.id) end end - - should 'copy set of boxes from community template' do - template = Community.create!(:name => 'test template', :identifier => 'test_template') - template.boxes.destroy_all - template.boxes << Box.new - template.boxes[0].blocks << Block.new - template.save! - env = Environment.create!(:name => 'test_env') - env.settings[:community_template_id] = template.id - env.save! - - assert_equal template, env.community_template - - com = Community.create!(:name => 'test ent', :identifier => 'test_ent', :environment => env) - - assert_equal 1, com.boxes.size - assert_equal 1, com.boxes[0].blocks.size + should 'have a community template' do + env = Environment.create!(:name => 'test env') + p = Community.create!(:name => 'test_com', :identifier => 'test_com', :environment => env) + assert_kind_of Community, p.template end end diff --git a/test/unit/enterprise_test.rb b/test/unit/enterprise_test.rb index 5b05138..dfc4ee1 100644 --- a/test/unit/enterprise_test.rb +++ b/test/unit/enterprise_test.rb @@ -205,23 +205,11 @@ class EnterpriseTest < Test::Unit::TestCase assert_not_includes ent.blocks.map(&:class), ProductsBlock end - should 'copy set of boxes from enterprise template' do - template = Enterprise.create!(:name => 'test template', :identifier => 'test_template') - template.boxes.destroy_all - template.boxes << Box.new - template.boxes[0].blocks << Block.new - template.save! - - env = Environment.create!(:name => 'test_env') - env.settings[:enterprise_template_id] = template.id - env.save! - - assert_equal template, env.enterprise_template - - ent = Enterprise.create!(:name => 'test ent', :identifier => 'test_ent', :environment => env) - - assert_equal 1, ent.boxes.size - assert_equal 1, ent.boxes[0].blocks.size + should 'have a enterprise template' do + env = Environment.create!(:name => 'test env') + p = Enterprise.create!(:name => 'test_com', :identifier => 'test_com', :environment => env) + assert_kind_of Enterprise, p.template end + end diff --git a/test/unit/person_test.rb b/test/unit/person_test.rb index 5b0ac75..3f01e1c 100644 --- a/test/unit/person_test.rb +++ b/test/unit/person_test.rb @@ -279,23 +279,10 @@ class PersonTest < Test::Unit::TestCase assert person.display_info_to?(friend) end - should 'copy set of boxes from person template' do - template = create_user('test_template').person - template.boxes.destroy_all - template.boxes << Box.new - template.boxes[0].blocks << Block.new - template.save! - - env = Environment.create!(:name => 'test_env') - env.settings[:person_template_id] = template.id - env.save! - - assert_equal template, env.person_template - - person = create_user('test_user', :environment => env).person - - assert_equal 1, person.boxes.size - assert_equal 1, person.boxes[0].blocks.size + should 'have a person template' do + env = Environment.create!(:name => 'test env') + p = create_user('test_user', :environment => env).person + assert_kind_of Person, p.template end - + end diff --git a/test/unit/profile_test.rb b/test/unit/profile_test.rb index da7c11c..bf80987 100644 --- a/test/unit/profile_test.rb +++ b/test/unit/profile_test.rb @@ -805,11 +805,43 @@ class ProfileTest < Test::Unit::TestCase should 'create a initial private folder when a public profile is created' do p1 = Profile.create!(:name => 'test profile 1', :identifier => 'test_profile1') p2 = Profile.create!(:name => 'test profile 2', :identifier => 'test_profile2', :public_profile => false) - + assert p1.articles.find(:first, :conditions => {:public_article => false}) assert !p2.articles.find(:first, :conditions => {:public_article => false}) end + should 'copy set of articles from a template' do + template = create_user('test_template').person + template.articles.destroy_all + a1 = template.articles.create(:name => 'some xyz article') + a2 = template.articles.create(:name => 'some child article', :parent => a1) + + Profile.any_instance.stubs(:template).returns(template) + + p = Profile.create!(:name => 'test_profile', :identifier => 'test_profile') + + assert_equal 1, p.top_level_articles.size + top_art = p.top_level_articles[0] + assert_equal 'some xyz article', top_art.name + assert_equal 1, top_art.children.size + child_art = top_art.children[0] + assert_equal 'some child article', child_art.name + end + + should 'copy set of boxes from profile template' do + template = Profile.create!(:name => 'test template', :identifier => 'test_template') + template.boxes.destroy_all + template.boxes << Box.new + template.boxes[0].blocks << Block.new + template.save! + + Profile.any_instance.stubs(:template).returns(template) + + p = Profile.create!(:name => 'test prof', :identifier => 'test_prof') + + assert_equal 1, p.boxes.size + assert_equal 1, p.boxes[0].blocks.size + end private def assert_invalid_identifier(id) -- libgit2 0.21.2