Commit d6508a9aed038383a0d8fb9aa60a39a8b3c644fb

Authored by Victor Costa
2 parents 6c40dd18 7a65a00d

Merge branch 'AI3138-choose_type_display_content' into rails3

Conflicts:
	plugins/display_content/lib/display_content_block.rb
	plugins/display_content/test/unit/display_content_block_test.rb
plugins/display_content/db/migrate/20140520100130_update_display_content_sections_array.rb 0 → 100644
... ... @@ -0,0 +1,30 @@
  1 +class UpdateDisplayContentSectionsArray < ActiveRecord::Migration
  2 +
  3 + def self.up
  4 + translator = {'Publish date' => 'publish_date', 'Title' => 'title', 'Abstract' => 'abstract', 'Body' => 'body', 'Image' => 'image', 'Tags' => 'tags',
  5 + 'Data de publicação' => 'publish_date', 'Título' => 'title', 'Resumo' => 'abstract', 'Corpo' => 'body', 'Imagem' => 'image'}
  6 +
  7 + DisplayContentBlock.find_each do |block|
  8 + new_sections = []
  9 +
  10 + block.sections.each do |section|
  11 + new_value = translator[section["name"]]
  12 + new_section = new_value.blank? ? section : {:value => new_value, :checked => !section["checked"].blank? }
  13 +
  14 + new_section_to_update = new_sections.select {|s| s[:value] == new_value}.first
  15 + if new_section_to_update.blank?
  16 + new_sections << new_section
  17 + else
  18 + new_section_to_update[:checked] = new_section[:checked]
  19 + end
  20 + end
  21 + block.sections = new_sections
  22 + block.update_attribute(:settings, block.settings)
  23 + end
  24 + end
  25 +
  26 + def self.down
  27 + raise "this migration can't be reverted"
  28 + end
  29 +
  30 +end
... ...
plugins/display_content/lib/display_content_block.rb
... ... @@ -15,16 +15,16 @@ class DisplayContentBlock &lt; Block
15 15 N_('December')
16 16 ]
17 17  
  18 + AVAILABLE_SECTIONS = ['publish_date', 'title', 'abstract', 'body', 'image' ,'tags']
  19 +
18 20 settings_items :nodes, :type => Array, :default => []
19 21 settings_items :sections,
20 22 :type => Array,
21   - :default => [{:name => _('Publish date'), :checked => true},
22   - {:name => _('Title'), :checked => true},
23   - {:name => _('Abstract'), :checked => true},
24   - {:name => _('Body'), :checked => false},
25   - {:name => _('Image'), :checked => false},
26   - {:name => _('Tags'), :checked => false}]
  23 + :default => [{:value => 'publish_date', :checked => true},
  24 + {:value => 'title', :checked => true},
  25 + {:value => 'abstract', :checked => true}]
27 26 settings_items :display_folder_children, :type => :boolean, :default => true
  27 + settings_items :types, :type => Array, :default => ['TextileArticle', 'TinyMceArticle', 'RawHTMLArticle']
28 28  
29 29 attr_accessible :sections, :checked_nodes, :display_folder_children
30 30  
... ... @@ -36,6 +36,50 @@ class DisplayContentBlock &lt; Block
36 36 _('This block displays articles chosen by you. You can edit the block to select which of your articles is going to be displayed in the block.')
37 37 end
38 38  
  39 + def section_name(section)
  40 + {
  41 + 'publish_date' => _('Publish date'),
  42 + 'title' => _('Title'),
  43 + 'abstract' => _('Abstract'),
  44 + 'body' => _('Body'),
  45 + 'image' => _('Image'),
  46 + 'tags' => _('Tags')
  47 + }[section] || section
  48 + end
  49 +
  50 + alias :orig_sections :sections
  51 + def sections
  52 + available_sections = AVAILABLE_SECTIONS
  53 + available_sections = available_sections - orig_sections.map{|e|e[:value]}
  54 + sections = available_sections.map do |section|
  55 + {:value => section, :checked => false}
  56 + end
  57 + sections + orig_sections
  58 + end
  59 +
  60 + def available_content_types
  61 + @available_content_types ||= [TinyMceArticle, RawHTMLArticle, TextileArticle, UploadedFile, Event, Folder, Blog, Forum, Gallery, RssFeed] + plugins.dispatch(:content_types)
  62 + checked_types = types.map {|t| t.constantize}
  63 + checked_types + (@available_content_types - checked_types)
  64 + end
  65 +
  66 + #FIXME make this test copy of Context Content
  67 + def first_content_types
  68 + available_content_types.first(first_types_count)
  69 + end
  70 +
  71 + def more_content_types
  72 + available_content_types.drop(first_types_count)
  73 + end
  74 +
  75 + def first_types_count
  76 + [2, types.length].max
  77 + end
  78 +
  79 + def types=(new_types)
  80 + settings[:types] = new_types.reject(&:blank?)
  81 + end
  82 +
39 83 def checked_nodes= params
40 84 self.nodes = params.keys
41 85 end
... ... @@ -45,7 +89,7 @@ class DisplayContentBlock &lt; Block
45 89 def expand_nodes
46 90 return self.nodes if self.holder.nil?
47 91  
48   - articles = self.holder.articles.find(nodes)
  92 + articles = self.holder.articles.where(:id => nodes)
49 93 children = articles.map { |article| article.children }.compact.flatten
50 94  
51 95 if display_folder_children
... ... @@ -73,9 +117,10 @@ class DisplayContentBlock &lt; Block
73 117  
74 118 def content(args={})
75 119 block = self
76   - extra_condition = display_folder_children ? 'OR articles.parent_id IN(:nodes)':''
77   - 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}])
  120 + nodes_conditions = nodes.blank? ? '' : " AND articles.id IN(:nodes) "
  121 + nodes_conditions += ' OR articles.parent_id IN(:nodes) ' if !nodes.blank? && display_folder_children
78 122  
  123 + docs = owner.articles.find(:all, :conditions => ["articles.type IN(:types) #{nodes.blank? ? '' : nodes_conditions}", {:nodes => self.nodes, :types => self.types}])
79 124 proc do
80 125 block.block_title(block.title) +
81 126 content_tag('ul', docs.map {|item|
... ... @@ -85,24 +130,24 @@ class DisplayContentBlock &lt; Block
85 130 tags_section = ''
86 131  
87 132 block.sections.select { |section|
88   - case section[:name]
89   - when 'Publish date'
90   - content_sections += (block.display_section?(section) ? (content_tag('div', block.show_date(item.published_at, false), :class => 'published-at') ) : '')
91   - when 'Title'
  133 + case section[:value]
  134 + when 'publish_date'
  135 + content_sections += (block.display_section?(section) ? (content_tag('div', show_date(item.published_at, false), :class => 'published-at') ) : '')
  136 + when 'title'
92 137 content_sections += (block.display_section?(section) ? (content_tag('div', link_to(h(item.title), item.url), :class => 'title') ) : '')
93   - when 'Abstract'
  138 + when 'abstract'
94 139 content_sections += (block.display_section?(section) ? (content_tag('div', item.abstract , :class => 'lead')) : '' )
95 140 if block.display_section?(section)
96 141 read_more_section = content_tag('div', link_to(_('Read more'), item.url), :class => 'read_more')
97 142 end
98   - when 'Body'
  143 + when 'body'
99 144 content_sections += (block.display_section?(section) ? (content_tag('div', item.body ,:class => 'body')) : '' )
100   - when 'Image'
  145 + when 'image'
101 146 image_section = image_tag item.image.public_filename if item.image
102 147 if !image_section.blank?
103 148 content_sections += (block.display_section?(section) ? (content_tag('div', link_to( image_section, item.url ) ,:class => 'image')) : '' )
104 149 end
105   - when 'Tags'
  150 + when 'tags'
106 151 if !item.tags.empty?
107 152 tags_section = item.tags.map { |t| content_tag('span', t.name) }.join("")
108 153 content_sections += (block.display_section?(section) ? (content_tag('div', tags_section, :class => 'tags')) : '')
... ... @@ -111,7 +156,7 @@ class DisplayContentBlock &lt; Block
111 156 }
112 157  
113 158 content_sections += read_more_section if !read_more_section.blank?
114   -
  159 +#raise sections.inspect
115 160 content_tag('li', content_sections)
116 161 end
117 162 }.join(" "))
... ...
plugins/display_content/test/unit/display_content_block_test.rb
... ... @@ -370,7 +370,7 @@ class DisplayContentBlockTest &lt; ActiveSupport::TestCase
370 370 a2 = fast_create(TextArticle, :name => 'test article 2', :profile_id => profile.id, :abstract => 'abstract article 2')
371 371  
372 372 block = DisplayContentBlock.new
373   - block.sections = [{:name => 'Abstract', :checked => true}]
  373 + block.sections = [{:value => 'abstract', :checked => true}]
374 374 block.nodes = [a1.id, a2.id]
375 375 box = mock()
376 376 block.stubs(:box).returns(box)
... ... @@ -425,7 +425,7 @@ class DisplayContentBlockTest &lt; ActiveSupport::TestCase
425 425  
426 426 block = DisplayContentBlock.new
427 427 block.nodes = [a.id]
428   - block.sections = [{:name => 'Title', :checked => true}]
  428 + block.sections = [{:value => 'title', :checked => true}]
429 429 box = mock()
430 430 block.stubs(:box).returns(box)
431 431 box.stubs(:owner).returns(profile)
... ... @@ -439,7 +439,7 @@ class DisplayContentBlockTest &lt; ActiveSupport::TestCase
439 439  
440 440 block = DisplayContentBlock.new
441 441 block.nodes = [a.id]
442   - block.sections = [{:name => 'Abstract', :checked => true}]
  442 + block.sections = [{:value => 'abstract', :checked => true}]
443 443 box = mock()
444 444 block.stubs(:box).returns(box)
445 445 box.stubs(:owner).returns(profile)
... ... @@ -453,7 +453,7 @@ class DisplayContentBlockTest &lt; ActiveSupport::TestCase
453 453  
454 454 block = DisplayContentBlock.new
455 455 block.nodes = [a.id]
456   - block.sections = [{:name => 'Body', :checked => true}]
  456 + block.sections = [{:value => 'body', :checked => true}]
457 457 box = mock()
458 458 block.stubs(:box).returns(box)
459 459 box.stubs(:owner).returns(profile)
... ... @@ -466,7 +466,7 @@ class DisplayContentBlockTest &lt; ActiveSupport::TestCase
466 466  
467 467 block = DisplayContentBlock.new
468 468  
469   - assert block.display_section?({:name => 'Title', :checked => true})
  469 + assert block.display_section?({:value => 'title', :checked => true})
470 470 end
471 471  
472 472 should 'display_attribute be true if the attribute was chosen' do
... ... @@ -474,8 +474,8 @@ class DisplayContentBlockTest &lt; ActiveSupport::TestCase
474 474  
475 475 block = DisplayContentBlock.new
476 476  
477   - block.sections = [{:name => 'Body', :checked => true}]
478   - section = block.sections.first
  477 + section = {:value => 'body', :checked => true}
  478 + block.sections = [section]
479 479  
480 480 assert block.display_section?(section)
481 481 end
... ... @@ -485,7 +485,7 @@ class DisplayContentBlockTest &lt; ActiveSupport::TestCase
485 485  
486 486 block = DisplayContentBlock.new
487 487  
488   - assert block.display_section?({:name => 'Publish date', :checked => true})
  488 + assert block.display_section?({:value => 'publish_date', :checked => true})
489 489 end
490 490  
491 491 should 'show publishd date if defined by user' do
... ... @@ -494,7 +494,7 @@ class DisplayContentBlockTest &lt; ActiveSupport::TestCase
494 494  
495 495 block = DisplayContentBlock.new
496 496 block.nodes = [a.id]
497   - block.sections = [{:name => 'Publish date', :checked => true}]
  497 + block.sections = [{:value => 'publish_date', :checked => true}]
498 498 box = mock()
499 499 block.stubs(:box).returns(box)
500 500 box.stubs(:owner).returns(profile)
... ... @@ -547,7 +547,6 @@ class DisplayContentBlockTest &lt; ActiveSupport::TestCase
547 547 profile = create_user('testuser').person
548 548 Article.delete_all
549 549 a1 = fast_create(PluginArticle, :name => 'test article 1', :profile_id => profile.id)
550   -
551 550 Noosfero::Plugin.stubs(:all).returns([Plugin1.name])
552 551 env = fast_create(Environment)
553 552 env.enable_plugin(Plugin1)
... ... @@ -555,9 +554,82 @@ class DisplayContentBlockTest &lt; ActiveSupport::TestCase
555 554 block = DisplayContentBlock.new
556 555 box = mock()
557 556 box.stubs(:owner).returns(profile)
  557 + Noosfero::Plugin.stubs(:all).returns(['DisplayContentBlockTest::Plugin1'])
558 558 box.stubs(:environment).returns(env)
559 559 block.stubs(:box).returns(box)
560 560 assert_equal [a1], block.articles_of_parent
561 561 end
562 562  
  563 + should "the section display all available sections" do
  564 + block = DisplayContentBlock.new
  565 + assert_equivalent ['publish_date', 'abstract', 'body', 'image' ,'tags', 'title'], block.sections.map{|e|e[:value]}
  566 + end
  567 +
  568 + should "the section display all available sections if the section value has only one key" do
  569 + block = DisplayContentBlock.new
  570 + block.sections = [{:value => 'abstract', :checked => true}]
  571 + assert_equivalent ['publish_date', 'abstract', 'body', 'image' ,'tags', 'title'], block.sections.map{|e|e[:value]}
  572 + end
  573 +
  574 + should 'return available content types with checked types first' do
  575 + Noosfero::Plugin::Manager.any_instance.stubs(:enabled_plugins).returns([])
  576 + block = DisplayContentBlock.create!
  577 + block.types = ['TinyMceArticle']
  578 +
  579 + block.types = ['TinyMceArticle', 'Folder']
  580 + assert_equal [TinyMceArticle, Folder, UploadedFile, Event, TextileArticle, RawHTMLArticle, Blog, Forum, Gallery, RssFeed], block.available_content_types
  581 + end
  582 +
  583 + should 'return available content types' do
  584 + Noosfero::Plugin::Manager.any_instance.stubs(:enabled_plugins).returns([])
  585 + block = DisplayContentBlock.create!
  586 + block.types = ['TinyMceArticle']
  587 + block.types = []
  588 + assert_equal [UploadedFile, Event, TinyMceArticle, TextileArticle, RawHTMLArticle, Folder, Blog, Forum, Gallery, RssFeed], block.available_content_types
  589 + end
  590 +
  591 + should 'return first 2 content types' do
  592 + Noosfero::Plugin::Manager.any_instance.stubs(:enabled_plugins).returns([])
  593 + block = DisplayContentBlock.create!
  594 + block.types = ['TinyMceArticle']
  595 + assert_equal 2, block.first_content_types.length
  596 + end
  597 +
  598 + should 'return all but first 2 content types' do
  599 + Noosfero::Plugin::Manager.any_instance.stubs(:enabled_plugins).returns([])
  600 + block = DisplayContentBlock.create!
  601 + block.types = ['TinyMceArticle']
  602 + assert_equal block.available_content_types.length - 2, block.more_content_types.length
  603 + end
  604 +
  605 + should 'return 2 as default value for first_types_count' do
  606 + block = DisplayContentBlock.create!
  607 + block.types = ['TinyMceArticle']
  608 + assert_equal 2, block.first_types_count
  609 + end
  610 +
  611 + should 'return types length if it has more than 2 selected types' do
  612 + block = DisplayContentBlock.create!
  613 + block.types = ['UploadedFile', 'Event', 'Folder']
  614 + assert_equal 3, block.first_types_count
  615 + end
  616 +
  617 + should 'return selected types at first_content_types' do
  618 + Noosfero::Plugin::Manager.any_instance.stubs(:enabled_plugins).returns([])
  619 + block = DisplayContentBlock.create!
  620 + block.types = ['UploadedFile', 'Event', 'Folder']
  621 + assert_equal [UploadedFile, Event, Folder], block.first_content_types
  622 + assert_equal block.available_content_types - [UploadedFile, Event, Folder], block.more_content_types
  623 + end
  624 +
  625 + should 'include plugin content at available content types' do
  626 + block = DisplayContentBlock.create!
  627 + class SomePluginContent;end
  628 + class SomePlugin; def content_types; SomePluginContent end end
  629 + Noosfero::Plugin::Manager.any_instance.stubs(:enabled_plugins).returns([SomePlugin.new])
  630 +
  631 + block.types = []
  632 + assert_equal [UploadedFile, Event, TinyMceArticle, TextileArticle, RawHTMLArticle, Folder, Blog, Forum, Gallery, RssFeed, SomePluginContent], block.available_content_types
  633 + end
  634 +
563 635 end
... ...
plugins/display_content/views/box_organizer/_choose_by_content_type.rhtml 0 → 100644
... ... @@ -0,0 +1,15 @@
  1 +<%= label :block, :types, _('Display content types:'), :class => 'formlabel' %>
  2 +<div class="content_types">
  3 + <% @block.first_content_types.each do |type| %>
  4 + <%= labelled_form_field check_box(:block, 'types', {:multiple => true}, type.name, nil) + _(type.short_description), '' %>
  5 + <% end %>
  6 + <% if !@block.more_content_types.empty? %>
  7 + <a href="#" onclick="jQuery('.content_types .more').toggle(); return false;"><%= _('more') %></a>
  8 + <% end %>
  9 + <div class="more" style="display: none">
  10 + <% @block.more_content_types.each do |type| %>
  11 + <%= labelled_form_field check_box(:block, 'types', {:multiple => true}, type.name, nil) + _(type.short_description), '' %>
  12 + <% end %>
  13 + </div>
  14 +</div>
  15 +
... ...
plugins/display_content/views/box_organizer/_choose_directly.rhtml 0 → 100644
... ... @@ -0,0 +1,30 @@
  1 +<div id="display_content">
  2 +</div>
  3 +
  4 +<div class="display_folder_children">
  5 + <%= labelled_form_field check_box(:block, :display_folder_children) + _('Dinamically load children of selected folders'), '' %>
  6 +</div>
  7 +
  8 +<script type="text/javascript" >
  9 +
  10 +jQuery_1_8_3("#display_content").jstree({
  11 + plugins : ["themes","json_data", "checkbox"],
  12 + checkbox : {
  13 + real_checkboxes : true,
  14 + real_checkboxes_names : function (n) { return [("block[checked_nodes[" + n.attr('node_id') + "]]"), 1]; }
  15 + },
  16 + themes : {"theme" : "classic", "icons" : true, "url": "/plugins/display_content/javascripts/jstree/themes/classic/style.css"},
  17 + json_data : {
  18 + ajax : {
  19 + url : '<%= url_for @block.url_params %>',
  20 + async: true,
  21 + data : function (m) {
  22 + return m.attr ? {"id" : m.attr("node_id")} : {};
  23 + }
  24 + }
  25 + }
  26 +});
  27 +
  28 +jQuery( "#sortable" ).sortable();
  29 +
  30 +</script>
... ...
plugins/display_content/views/box_organizer/_display_content_block.html.erb
1 1 <div id="display_content_plugin">
2 2  
  3 +<%= javascript_tag 'render_all_jquery_ui_widgets()' %>
  4 +
3 5 <h3> <%= _('Choose which attributes should be displayed and drag to reorder them:') %> </h3>
4 6  
5 7 <table class="sections">
6 8 <tbody id="sortable">
7 9 <% for section in @block.sections do %>
8 10 <tr>
9   - <td><%= hidden_field_tag 'block[sections][][name]', section[:name] %> <%= check_box_tag 'block[sections][][checked]', section[:name], section[:checked] %></td>
10   - <td><%= section[:name]%></td>
  11 + <td>
  12 + <%= hidden_field_tag 'block[sections][][value]', section[:value] %>
  13 + <%= check_box_tag 'block[sections][][checked]', section[:value], section[:checked] %>
  14 + </td>
  15 + <td><%= @block.section_name(section[:value]) %></td>
11 16 </tr>
12 17 <% end %>
13 18 </tbody>
14 19 </table>
15 20  
16 21 <h3> <%= _('Choose which content should be displayed:') %> </h3>
17   -<div id="display_content">
18   -</div>
19   -
20   -<div class="display_folder_children">
21   - <%= labelled_form_field check_box(:block, :display_folder_children) + _('Dinamically load children of selected folders'), '' %>
22   -</div>
  22 +<% tabs = [] %>
  23 +<% tabs << {:title => _('Choose directly'), :id => 'choose-directly', :content => (render :partial => 'choose_directly')} %>
  24 +<% tabs << {:title => _('Choose by Content Type'), :id => 'choose_by_content_type', :content => (render :partial => 'choose_by_content_type')} %>
23 25  
24   -<script type="text/javascript" >
  26 +<%= render_tabs(tabs) %>
25 27  
26   -jQuery_1_8_3("#display_content").jstree({
27   - plugins : ["themes","json_data", "checkbox"],
28   - checkbox : {
29   - real_checkboxes : true,
30   - real_checkboxes_names : function (n) { return [("block[checked_nodes[" + n.attr('node_id') + "]]"), 1]; }
31   - },
32   - themes : {"theme" : "classic", "icons" : true, "url": "/plugins/display_content/javascripts/jstree/themes/classic/style.css"},
33   - json_data : {
34   - ajax : {
35   - url : '<%= url_for @block.url_params %>',
36   - async: true,
37   - data : function (m) {
38   - return m.attr ? {"id" : m.attr("node_id")} : {};
39   - }
40   - }
41   - }
42   -});
43   -
44   -jQuery( "#sortable" ).sortable();
45   -
46   -</script>
47 28 </div>
  29 +
... ...