From 2920c5c036db7fdea76b13c800a6e2ab6e0caf43 Mon Sep 17 00:00:00 2001 From: Victor Costa Date: Fri, 29 Nov 2013 18:10:19 -0300 Subject: [PATCH] Change display content to dinamically show parent content --- plugins/display_content/lib/display_content_block.rb | 52 ++++++++++++++++++++++++++++++++-------------------- plugins/display_content/test/functional/display_content_plugin_admin_controller_test.rb | 24 +++++++++++------------- plugins/display_content/test/functional/display_content_plugin_myprofile_controller_test.rb | 56 +++++++++++++++++++++++++++----------------------------- plugins/display_content/test/unit/display_content_block_test.rb | 268 +++++++++++++++++++++++++++++++++++++++++++++++++--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- plugins/display_content/views/box_organizer/_display_content_block.rhtml | 4 ++++ 5 files changed, 123 insertions(+), 281 deletions(-) diff --git a/plugins/display_content/lib/display_content_block.rb b/plugins/display_content/lib/display_content_block.rb index dadada6..5bc5563 100644 --- a/plugins/display_content/lib/display_content_block.rb +++ b/plugins/display_content/lib/display_content_block.rb @@ -1,8 +1,8 @@ class DisplayContentBlock < Block settings_items :nodes, :type => Array, :default => [] - settings_items :parent_nodes, :type => Array, :default => [] settings_items :chosen_attributes, :type => Array, :default => ['title'] + settings_items :display_folder_children, :type => :boolean, :default => true def self.description _('Display your contents') @@ -13,20 +13,28 @@ class DisplayContentBlock < Block end def checked_nodes= params + self.nodes = params.keys + end + + before_save :expand_nodes + + def expand_nodes return self.nodes if self.holder.nil? - articles = [] - parent_articles = [] - self.holder.articles.find(params.keys).map do |article| - if article.folder? - articles = articles + article.children - parent_articles << article.id - else - articles<< article - end - parent_articles = parent_articles + get_parent(article) unless parent_articles.include?(article.parent_id) + + articles = self.holder.articles.find(nodes) + children = articles.map { |article| article.children }.compact.flatten + + if display_folder_children + articles = articles - children + else + articles = (articles + children).uniq end - self.parent_nodes = parent_articles - self.nodes = articles.map{|a| a.id if a.is_a?(TextArticle) }.compact + + self.nodes = articles.map(&:id) + end + + def parent_nodes + @parent_nodes ||= self.holder.articles.find(nodes).map { |article| get_parent(article) }.compact.flatten end VALID_CONTENT = ['RawHTMLArticle', 'TextArticle', 'TextileArticle', 'TinyMceArticle', 'Folder', 'Blog', 'Forum'] @@ -38,14 +46,18 @@ class DisplayContentBlock < Block include ActionController::UrlWriter def content(args={}) - docs = owner.articles.find(:all, :conditions => {:id => self.nodes}) + extra_condition = display_folder_children ? 'OR articles.parent_id IN(:nodes)':'' + docs = nodes.blank? ? [] : owner.articles.find(:all, :conditions => ["(articles.id IN(:nodes) #{extra_condition}) AND articles.type IN(:types)", {:nodes => self.nodes, :types => VALID_CONTENT}]) + block_title(title) + - content_tag('ul', docs.map {|item| - content_tag('li', - (display_attribute?('title') ? content_tag('div', link_to(h(item.title), item.url), :class => 'title') : '') + - (display_attribute?('abstract') ? content_tag('div', item.abstract ,:class => 'lead') : '') + - (display_attribute?('body') ? content_tag('div', item.body ,:class => 'body') : '') - ) + content_tag('ul', docs.map {|item| + if !item.folder? + content_tag('li', + (display_attribute?('title') ? content_tag('div', link_to(h(item.title), item.url), :class => 'title') : '') + + (display_attribute?('abstract') ? content_tag('div', item.abstract ,:class => 'lead') : '') + + (display_attribute?('body') ? content_tag('div', item.body ,:class => 'body') : '') + ) + end }.join("\n")) end diff --git a/plugins/display_content/test/functional/display_content_plugin_admin_controller_test.rb b/plugins/display_content/test/functional/display_content_plugin_admin_controller_test.rb index db5434b..1eb8d6c 100644 --- a/plugins/display_content/test/functional/display_content_plugin_admin_controller_test.rb +++ b/plugins/display_content/test/functional/display_content_plugin_admin_controller_test.rb @@ -73,18 +73,16 @@ class DisplayContentPluginAdminControllerTest < ActionController::TestCase should 'index action returns an json with node undetermined if the node is in the parent nodes list' do Article.delete_all - article = fast_create(TextileArticle, :name => 'test article 1', :profile_id => environment.portal_community.id) - block.parent_nodes= [article.id] + f = fast_create(Folder, :name => 'test folder 1', :profile_id => environment.portal_community.id) + article = fast_create(TextileArticle, :name => 'test article 1', :profile_id => environment.portal_community.id, :parent_id => f.id) + article2 = fast_create(TextileArticle, :name => 'test article 2', :profile_id => environment.portal_community.id, :parent_id => f.id) + block.nodes = [article.id] block.save! get :index, :block_id => block.id json_response = ActiveSupport::JSON.decode(@response.body) - expected_json = {'data' => article.title} - expected_json['attr'] = { 'node_id' => article.id, 'parent_id' => article.parent_id} - expected_json['attr'].merge!({'class' => 'jstree-undetermined'}) - expected_json['children'] = [] - - assert_equivalent [expected_json], json_response + expected_json = { 'node_id' => f.id, 'class' => 'jstree-undetermined', 'parent_id' => f.parent_id} + assert_equal expected_json, json_response.first['attr'] end should 'index action returns an json with node closed if the node has article with children' do @@ -106,7 +104,7 @@ class DisplayContentPluginAdminControllerTest < ActionController::TestCase f = fast_create(Folder, :name => 'test folder 1', :profile_id => environment.portal_community.id) a1 = fast_create(TextileArticle, :name => 'test article 1', :profile_id => environment.portal_community.id, :parent_id => f.id) a2 = fast_create(TextileArticle, :name => 'test article 2', :profile_id => environment.portal_community.id, :parent_id => f.id) - block.parent_nodes = [f.id] + block.checked_nodes= {a1.id => true} block.save! get :index, :block_id => block.id @@ -114,7 +112,7 @@ class DisplayContentPluginAdminControllerTest < ActionController::TestCase expected_json = {'data' => f.title} expected_json['attr'] = { 'node_id' => f.id, 'parent_id' => f.parent_id} children = [ - {'data' => a1.title, 'attr' => {'node_id' => a1.id, 'parent_id' => a1.parent_id}}, + {'data' => a1.title, 'attr' => {'node_id' => a1.id, 'parent_id' => a1.parent_id, "class" => "jstree-checked"}}, {'data' => a2.title, 'attr' => {'node_id' => a2.id, 'parent_id'=> a2.parent_id}} ] expected_json['attr'].merge!({'class' => 'jstree-undetermined'}) @@ -130,7 +128,7 @@ class DisplayContentPluginAdminControllerTest < ActionController::TestCase a1 = fast_create(TextileArticle, :name => 'test article 1', :profile_id => environment.portal_community.id, :parent_id => f.id) a2 = fast_create(TextileArticle, :name => 'test article 2', :profile_id => environment.portal_community.id, :parent_id => f.id) a3 = fast_create(TextileArticle, :name => 'test article 3', :profile_id => environment.portal_community.id) - block.parent_nodes = [f.id] + block.checked_nodes= {a2.id => true, a3.id => true} block.save! get :index, :block_id => block.id @@ -140,7 +138,7 @@ class DisplayContentPluginAdminControllerTest < ActionController::TestCase value['attr'] = { 'node_id' => f.id, 'parent_id' => f.parent_id} children = [ {'data' => a1.title, 'attr' => {'node_id' => a1.id, 'parent_id' => a1.parent_id}}, - {'data' => a2.title, 'attr' => {'node_id' => a2.id, 'parent_id'=> a2.parent_id}} + {'data' => a2.title, 'attr' => {'node_id' => a2.id, 'parent_id'=> a2.parent_id, "class" => "jstree-checked"}} ] value['attr'].merge!({'class' => 'jstree-undetermined'}) value['children'] = children @@ -148,7 +146,7 @@ class DisplayContentPluginAdminControllerTest < ActionController::TestCase expected_json.push(value) value = {'data' => a3.title} - value['attr'] = { 'node_id' => a3.id, 'parent_id' => a3.parent_id} + value['attr'] = { 'node_id' => a3.id, 'parent_id' => a3.parent_id, "class" => "jstree-checked"} expected_json.push(value) assert_equivalent expected_json, json_response diff --git a/plugins/display_content/test/functional/display_content_plugin_myprofile_controller_test.rb b/plugins/display_content/test/functional/display_content_plugin_myprofile_controller_test.rb index 8eca057..67d30a3 100644 --- a/plugins/display_content/test/functional/display_content_plugin_myprofile_controller_test.rb +++ b/plugins/display_content/test/functional/display_content_plugin_myprofile_controller_test.rb @@ -73,18 +73,16 @@ class DisplayContentPluginMyprofileControllerTest < ActionController::TestCase should 'index action returns an json with node undetermined if the node is in the parent nodes list' do Article.delete_all - article = fast_create(TextileArticle, :name => 'test article 1', :profile_id => profile.id) - block.parent_nodes= [article.id] + f = fast_create(Folder, :name => 'test folder 1', :profile_id => profile.id) + article = fast_create(TextileArticle, :name => 'test article 1', :profile_id => profile.id, :parent_id => f.id) + article2 = fast_create(TextileArticle, :name => 'test article 2', :profile_id => profile.id, :parent_id => f.id) + block.nodes = [article.id] block.save! get :index, :block_id => block.id, :profile => profile.identifier json_response = ActiveSupport::JSON.decode(@response.body) - expected_json = {'data' => article.title} - expected_json['attr'] = { 'node_id' => article.id, 'parent_id' => article.parent_id} - expected_json['attr'].merge!({'class' => 'jstree-undetermined'}) - expected_json['children'] = [] - - assert_equivalent [expected_json], json_response + expected_json = { 'node_id' => f.id, 'class' => 'jstree-undetermined', 'parent_id' => f.parent_id} + assert_equal expected_json, json_response.first['attr'] end should 'index action returns an json with node closed if the node has article with children' do @@ -107,7 +105,7 @@ class DisplayContentPluginMyprofileControllerTest < ActionController::TestCase f = fast_create(Folder, :name => 'test folder 1', :profile_id => profile.id) a1 = fast_create(TextileArticle, :name => 'test article 1', :profile_id => profile.id, :parent_id => f.id) a2 = fast_create(TextileArticle, :name => 'test article 2', :profile_id => profile.id, :parent_id => f.id) - block.parent_nodes = [f.id] + block.checked_nodes = {a1.id => true} block.save! get :index, :block_id => block.id, :profile => profile.identifier @@ -115,32 +113,32 @@ class DisplayContentPluginMyprofileControllerTest < ActionController::TestCase expected_json = {'data' => f.title} expected_json['attr'] = { 'node_id' => f.id, 'parent_id' => f.parent_id} children = [ - {'data' => a1.title, 'attr' => {'node_id' => a1.id, 'parent_id' => a1.parent_id}}, + {'data' => a1.title, 'attr' => {'node_id' => a1.id, 'parent_id' => a1.parent_id, "class" => "jstree-checked"}}, {'data' => a2.title, 'attr' => {'node_id' => a2.id, 'parent_id'=> a2.parent_id}} ] expected_json['attr'].merge!({'class' => 'jstree-undetermined'}) expected_json['children'] = children expected_json['state'] = 'closed' - assert_equivalent [expected_json], json_response - end - - should 'index action returns an json with all the children nodes and root nodes if some parent is in the parents list and there is others root articles' do - Article.delete_all - f = fast_create(Folder, :name => 'test folder 1', :profile_id => profile.id) - a1 = fast_create(TextileArticle, :name => 'test article 1', :profile_id => profile.id, :parent_id => f.id) - a2 = fast_create(TextileArticle, :name => 'test article 2', :profile_id => profile.id, :parent_id => f.id) - a3 = fast_create(TextileArticle, :name => 'test article 3', :profile_id => profile.id) - block.parent_nodes = [f.id] - block.save! - - get :index, :block_id => block.id, :profile => profile.identifier - json_response = ActiveSupport::JSON.decode(@response.body) - expected_json = [] - value = {'data' => f.title} - value['attr'] = { 'node_id' => f.id, 'parent_id' => f.parent_id} - children = [ - {'data' => a1.title, 'attr' => {'node_id' => a1.id, 'parent_id' => a1.parent_id}}, + assert_equivalent [expected_json], json_response + end + + should 'index action returns an json with all the children nodes and root nodes if some parent is in the parents list and there is others root articles' do + Article.delete_all + f = fast_create(Folder, :name => 'test folder 1', :profile_id => profile.id) + a1 = fast_create(TextileArticle, :name => 'test article 1', :profile_id => profile.id, :parent_id => f.id) + a2 = fast_create(TextileArticle, :name => 'test article 2', :profile_id => profile.id, :parent_id => f.id) + a3 = fast_create(TextileArticle, :name => 'test article 3', :profile_id => profile.id) + block.checked_nodes = {a1.id => true} + block.save! + + get :index, :block_id => block.id, :profile => profile.identifier + json_response = ActiveSupport::JSON.decode(@response.body) + expected_json = [] + value = {'data' => f.title} + value['attr'] = { 'node_id' => f.id, 'parent_id' => f.parent_id} + children = [ + {'data' => a1.title, 'attr' => {'node_id' => a1.id, 'parent_id' => a1.parent_id, "class" => "jstree-checked"}}, {'data' => a2.title, 'attr' => {'node_id' => a2.id, 'parent_id'=> a2.parent_id}} ] value['attr'].merge!({'class' => 'jstree-undetermined'}) diff --git a/plugins/display_content/test/unit/display_content_block_test.rb b/plugins/display_content/test/unit/display_content_block_test.rb index cb4d66a..e04c879 100644 --- a/plugins/display_content/test/unit/display_content_block_test.rb +++ b/plugins/display_content/test/unit/display_content_block_test.rb @@ -23,7 +23,7 @@ class DisplayContentBlockTest < ActiveSupport::TestCase assert_equal [], block.nodes end - should 'not set nodes if there is no holder' do + should 'not expand nodes if there is no holder' do Article.delete_all a1 = fast_create(TextArticle, :name => 'test article 1', :profile_id => 1) @@ -31,7 +31,9 @@ class DisplayContentBlockTest < ActiveSupport::TestCase block = DisplayContentBlock.new block.stubs(:holder).returns(nil) block.checked_nodes= checked_articles - assert_equal [], block.nodes + a1.delete + block.save! + assert_equal [a1.id], block.nodes end should 'nodes be the article ids in hash of checked nodes' do @@ -90,28 +92,7 @@ class DisplayContentBlockTest < ActiveSupport::TestCase assert_equal [], block.nodes - [a1.id, a4.id] end - should "save the first children level of folders" do - profile = create_user('testuser').person - Article.delete_all - a1 = fast_create(TextileArticle, :name => 'test article 1', :profile_id => profile.id) - a2 = fast_create(TextileArticle, :name => 'test article 2', :profile_id => profile.id) - f1 = fast_create(Folder, :name => 'test folder 1', :profile_id => profile.id) - a3 = fast_create(TextileArticle, :name => 'test article 3', :profile_id => profile.id, :parent_id => f1.id) - f2 = fast_create(Folder, :name => 'test folder 1', :profile_id => profile.id) - a4 = fast_create(TextileArticle, :name => 'test article 4', :profile_id => profile.id, :parent_id => f2.id) - a5 = fast_create(TextileArticle, :name => 'test article 5', :profile_id => profile.id, :parent_id => f2.id) - - checked_articles= {a1.id => true, a2.id => true, f1.id => false, f2.id => true} - - block = DisplayContentBlock.new - block.stubs(:holder).returns(profile) - block.checked_nodes= checked_articles - - assert_equal [], [a1.id, a2.id, a3.id, a4.id, a5.id] - block.nodes - assert_equal [], block.nodes - [a1.id, a2.id, a3.id, a4.id, a5.id] - end - - should "not save deeper level of folder's children" do + should "save selected folders and articles" do profile = create_user('testuser').person Article.delete_all a1 = fast_create(TextileArticle, :name => 'test article 1', :profile_id => profile.id) @@ -128,11 +109,10 @@ class DisplayContentBlockTest < ActiveSupport::TestCase block.stubs(:holder).returns(profile) block.checked_nodes= checked_articles - assert_equal [], [a1.id, a2.id, a3.id] - block.nodes - assert_equal [], block.nodes - [a1.id, a2.id, a3.id] + assert_equivalent [a1.id, a2.id, f1.id], block.nodes end - should "save the first children level of blogs" do + should "save selected articles and blogs" do profile = create_user('testuser').person Article.delete_all a1 = fast_create(TextileArticle, :name => 'test article 1', :profile_id => profile.id) @@ -149,8 +129,7 @@ class DisplayContentBlockTest < ActiveSupport::TestCase block.stubs(:holder).returns(profile) block.checked_nodes= checked_articles - assert_equal [], [a1.id, a2.id, a3.id, a4.id, a5.id] - block.nodes - assert_equal [], block.nodes - [a1.id, a2.id, a3.id, a4.id, a5.id] + assert_equivalent [a1.id, a2.id, b1.id, b2.id], block.nodes end should 'TextileArticle be saved as node' do @@ -192,7 +171,7 @@ class DisplayContentBlockTest < ActiveSupport::TestCase assert_equal [], block.nodes - [a1.id] end - should 'Event not be saved as node' do + should 'Event be saved as node' do profile = create_user('testuser').person Article.delete_all a1 = fast_create(TextArticle, :name => 'test article 1', :profile_id => profile.id) @@ -203,41 +182,10 @@ class DisplayContentBlockTest < ActiveSupport::TestCase block = DisplayContentBlock.new block.stubs(:holder).returns(profile) block.checked_nodes= checked_articles - assert_equal [], [a1.id, a2.id] - block.nodes - assert_equal [], block.nodes - [a1.id, a2.id] + assert_equivalent [a1.id, a2.id, a3.id], block.nodes end - should 'RSS not be saved as node' do - profile = create_user('testuser').person - Article.delete_all - a1 = fast_create(TextArticle, :name => 'test article 1', :profile_id => profile.id) - a2 = fast_create(TextArticle, :name => 'test article 2', :profile_id => profile.id) - a3 = fast_create(RssFeed, :name => 'test article 3', :profile_id => profile.id) - - checked_articles= {a1.id => true, a2.id => true, a3.id => false} - block = DisplayContentBlock.new - block.stubs(:holder).returns(profile) - block.checked_nodes= checked_articles - assert_equal [], [a1.id, a2.id] - block.nodes - assert_equal [], block.nodes - [a1.id, a2.id] - end - - should 'UploadedFile not be saved as node' do - profile = create_user('testuser').person - Article.delete_all - a1 = fast_create(TextArticle, :name => 'test article 1', :profile_id => profile.id) - a2 = fast_create(TextArticle, :name => 'test article 2', :profile_id => profile.id) - a3 = fast_create(UploadedFile, :name => 'test article 3', :profile_id => profile.id) - - checked_articles= {a1.id => true, a2.id => true, a3.id => false} - block = DisplayContentBlock.new - block.stubs(:holder).returns(profile) - block.checked_nodes= checked_articles - assert_equal [], [a1.id, a2.id] - block.nodes - assert_equal [], block.nodes - [a1.id, a2.id] - end - - should 'Folder not be saved as node' do + should 'Folder be saved as node' do profile = create_user('testuser').person Article.delete_all a1 = fast_create(TextArticle, :name => 'test article 1', :profile_id => profile.id) @@ -248,11 +196,10 @@ class DisplayContentBlockTest < ActiveSupport::TestCase block = DisplayContentBlock.new block.stubs(:holder).returns(profile) block.checked_nodes= checked_articles - assert_equal [], [a1.id, a2.id] - block.nodes - assert_equal [], block.nodes - [a1.id, a2.id] + assert_equivalent [a1.id, a2.id, a3.id], block.nodes end - should 'Forum not be saved as node' do + should 'Forum be saved as node' do profile = create_user('testuser').person Article.delete_all a1 = fast_create(TextArticle, :name => 'test article 1', :profile_id => profile.id) @@ -263,26 +210,10 @@ class DisplayContentBlockTest < ActiveSupport::TestCase block = DisplayContentBlock.new block.stubs(:holder).returns(profile) block.checked_nodes= checked_articles - assert_equal [], [a1.id, a2.id] - block.nodes - assert_equal [], block.nodes - [a1.id, a2.id] - end - - should 'Gallery not be saved as node' do - profile = create_user('testuser').person - Article.delete_all - a1 = fast_create(TextArticle, :name => 'test article 1', :profile_id => profile.id) - a2 = fast_create(TextArticle, :name => 'test article 2', :profile_id => profile.id) - a3 = fast_create(Gallery, :name => 'test article 3', :profile_id => profile.id) - - checked_articles= {a1.id => true, a2.id => true, a3.id => false} - block = DisplayContentBlock.new - block.stubs(:holder).returns(profile) - block.checked_nodes= checked_articles - assert_equal [], [a1.id, a2.id] - block.nodes - assert_equal [], block.nodes - [a1.id, a2.id] + assert_equivalent [a1.id, a2.id, a3.id], block.nodes end - should 'Blog not be saved as node' do + should 'Blog be saved as node' do profile = create_user('testuser').person Article.delete_all a1 = fast_create(TextArticle, :name => 'test article 1', :profile_id => profile.id) @@ -293,141 +224,7 @@ class DisplayContentBlockTest < ActiveSupport::TestCase block = DisplayContentBlock.new block.stubs(:holder).returns(profile) block.checked_nodes= checked_articles - assert_equal [], [a1.id, a2.id] - block.nodes - assert_equal [], block.nodes - [a1.id, a2.id] - end - - should "save the article parents in parent_nodes variable" do - profile = create_user('testuser').person - Article.delete_all - a1 = fast_create(TextileArticle, :name => 'test article 1', :profile_id => profile.id) - a2 = fast_create(TextileArticle, :name => 'test article 2', :profile_id => profile.id) - f1 = fast_create(Folder, :name => 'test folder 1', :profile_id => profile.id) - a3 = fast_create(TextileArticle, :name => 'test article 3', :profile_id => profile.id, :parent_id => f1.id) - f2 = fast_create(Folder, :name => 'test folder 1', :profile_id => profile.id) - a4 = fast_create(TextileArticle, :name => 'test article 4', :profile_id => profile.id, :parent_id => f2.id) - - checked_articles= {a1.id => 1, a3.id => 1, a4.id => 1, f2.id => true} - - block = DisplayContentBlock.new - block.stubs(:holder).returns(profile) - block.checked_nodes= checked_articles - - assert_equal [], [f1.id, f2.id] - block.parent_nodes - assert_equal [], block.parent_nodes - [f1.id, f2.id] - end - - should "save deeper level of article parents in parent_nodes variable" do - profile = create_user('testuser').person - Article.delete_all - a1 = fast_create(TextileArticle, :name => 'test article 1', :profile_id => profile.id) - f1 = fast_create(Folder, :name => 'test folder 1', :profile_id => profile.id) - f2 = fast_create(Folder, :name => 'test folder 1', :profile_id => profile.id, :parent_id => f1.id) - f3 = fast_create(Folder, :name => 'test folder 1', :profile_id => profile.id, :parent_id => f2.id) - a2 = fast_create(TextileArticle, :name => 'test article 4', :profile_id => profile.id, :parent_id => f3.id) - - checked_articles= {a2.id => 1} - - block = DisplayContentBlock.new - block.stubs(:holder).returns(profile) - block.checked_nodes= checked_articles - - assert_equal [], [f1.id, f2.id, f3.id] - block.parent_nodes - assert_equal [], block.parent_nodes - [f1.id, f2.id, f3.id] - end - - should "save only once time of parents if more than one children article is checked" do - profile = create_user('testuser').person - Article.delete_all - a1 = fast_create(TextileArticle, :name => 'test article 1', :profile_id => profile.id) - f1 = fast_create(Folder, :name => 'test folder 1', :profile_id => profile.id) - a2 = fast_create(TextileArticle, :name => 'test article 3', :profile_id => profile.id, :parent_id => f1.id) - f2 = fast_create(Folder, :name => 'test folder 1', :profile_id => profile.id) - a3 = fast_create(TextileArticle, :name => 'test article 4', :profile_id => profile.id, :parent_id => f2.id) - a4 = fast_create(TextileArticle, :name => 'test article 5', :profile_id => profile.id, :parent_id => f2.id) - a5 = fast_create(TextileArticle, :name => 'test article 2', :profile_id => profile.id, :parent_id => f2.id) - - checked_articles= {a1.id => 1, a2.id => 1, a3.id => 1, a4.id => 1, a5.id => 1} - - block = DisplayContentBlock.new - block.stubs(:holder).returns(profile) - block.checked_nodes= checked_articles - - assert_equal [], [f1.id, f2.id] - block.parent_nodes - assert_equal [], block.parent_nodes - [f1.id, f2.id] - end - - should "save only once time of parents if a deeper level of children is checked" do - profile = create_user('testuser').person - Article.delete_all - a1 = fast_create(TextileArticle, :name => 'test article 1', :profile_id => profile.id) - f1 = fast_create(Folder, :name => 'test folder 1', :profile_id => profile.id) - f2 = fast_create(Folder, :name => 'test folder 2', :profile_id => profile.id, :parent_id => f1.id) - f3 = fast_create(Folder, :name => 'test folder 2', :profile_id => profile.id, :parent_id => f2.id) - f4 = fast_create(Folder, :name => 'test folder 2', :profile_id => profile.id, :parent_id => f3.id) - f5 = fast_create(Folder, :name => 'test folder 2', :profile_id => profile.id, :parent_id => f2.id) - a2 = fast_create(TextileArticle, :name => 'test article 3', :profile_id => profile.id, :parent_id => f4.id) - a3 = fast_create(TextileArticle, :name => 'test article 4', :profile_id => profile.id, :parent_id => f5.id) - - checked_articles= {a2.id => 1, a3.id => 1} - - block = DisplayContentBlock.new - block.stubs(:holder).returns(profile) - block.checked_nodes= checked_articles - - assert_equal [], [f1.id, f2.id, f3.id, f4.id, f5.id] - block.parent_nodes - assert_equal [], block.parent_nodes - [f1.id, f2.id, f3.id, f4.id, f5.id] - end - - should "save the folder in parent_nodes variable if it was checked" do - profile = create_user('testuser').person - Article.delete_all - f1 = fast_create(Folder, :name => 'test folder 1', :profile_id => profile.id) - a1 = fast_create(TextileArticle, :name => 'test article 1', :profile_id => profile.id, :parent_id => f1.id) - a2 = fast_create(TextileArticle, :name => 'test article 4', :profile_id => profile.id, :parent_id => f1.id) - - checked_articles= {f1.id => 1} - - block = DisplayContentBlock.new - block.stubs(:holder).returns(profile) - block.checked_nodes= checked_articles - - assert_equal [], [f1.id] - block.parent_nodes - assert_equal [], block.parent_nodes - [f1.id] - end - - should "save the blog in parent_nodes variable if it was checked" do - profile = create_user('testuser').person - Article.delete_all - b1 = fast_create(Blog, :name => 'test folder 1', :profile_id => profile.id) - a1 = fast_create(TextileArticle, :name => 'test article 1', :profile_id => profile.id, :parent_id => b1.id) - a2 = fast_create(TextileArticle, :name => 'test article 4', :profile_id => profile.id, :parent_id => b1.id) - - checked_articles= {b1.id => 1} - - block = DisplayContentBlock.new - block.stubs(:holder).returns(profile) - block.checked_nodes= checked_articles - - assert_equal [], [b1.id] - block.parent_nodes - assert_equal [], block.parent_nodes - [b1.id] - end - - should "save the forum in parent_nodes variable if it was checked" do - profile = create_user('testuser').person - Article.delete_all - f1 = fast_create(Forum, :name => 'test folder 1', :profile_id => profile.id) - a1 = fast_create(TextileArticle, :name => 'test article 1', :profile_id => profile.id, :parent_id => f1.id) - a2 = fast_create(TextileArticle, :name => 'test article 4', :profile_id => profile.id, :parent_id => f1.id) - - checked_articles= {f1.id => 1} - - block = DisplayContentBlock.new - block.stubs(:holder).returns(profile) - block.checked_nodes= checked_articles - - assert_equal [], [f1.id] - block.parent_nodes - assert_equal [], block.parent_nodes - [f1.id] + assert_equivalent [a1.id, a2.id, a3.id], block.nodes end should "return all root articles from profile" do @@ -672,4 +469,37 @@ class DisplayContentBlockTest < ActiveSupport::TestCase assert block.display_attribute?('body') end + should 'do not save children if a folder is checked' do + profile = create_user('testuser').person + Article.delete_all + f1 = fast_create(Folder, :name => 'test folder 1', :profile_id => profile.id) + a1 = fast_create(TextileArticle, :name => 'test article 1', :profile_id => profile.id, :parent_id => f1.id) + a2 = fast_create(TextileArticle, :name => 'test article 2', :profile_id => profile.id, :parent_id => f1.id) + a3 = fast_create(TextileArticle, :name => 'test article 3', :profile_id => profile.id, :parent_id => f1.id) + + checked_articles= {f1.id => true, a1.id => true, a2.id => true, a3.id => false} + block = DisplayContentBlock.new + block.stubs(:holder).returns(profile) + block.checked_nodes= checked_articles + block.save! + assert_equivalent [f1.id], block.nodes + end + + should 'save folder and children if display_folder_children is false' do + profile = create_user('testuser').person + Article.delete_all + f1 = fast_create(Folder, :name => 'test folder 1', :profile_id => profile.id) + a1 = fast_create(TextileArticle, :name => 'test article 1', :profile_id => profile.id, :parent_id => f1.id) + a2 = fast_create(TextileArticle, :name => 'test article 2', :profile_id => profile.id, :parent_id => f1.id) + a3 = fast_create(TextileArticle, :name => 'test article 3', :profile_id => profile.id, :parent_id => f1.id) + + checked_articles= {f1.id => true, a1.id => true, a2.id => true, a3.id => false} + block = DisplayContentBlock.new + block.display_folder_children = false + block.stubs(:holder).returns(profile) + block.checked_nodes= checked_articles + block.save! + assert_equivalent [f1.id, a1.id, a2.id, a3.id], block.nodes + end + end diff --git a/plugins/display_content/views/box_organizer/_display_content_block.rhtml b/plugins/display_content/views/box_organizer/_display_content_block.rhtml index b1f417e..051f5ce 100644 --- a/plugins/display_content/views/box_organizer/_display_content_block.rhtml +++ b/plugins/display_content/views/box_organizer/_display_content_block.rhtml @@ -11,6 +11,10 @@
+
+ <%= labelled_form_field check_box(:block, :display_folder_children) + _('Dinamically load children of selected folders'), '' %> +
+