Commit 11a81060fde6ffa40fc91365d2e3326fb5657d6f

Authored by Marcos Pereira
1 parent 251e6e43

fixes community track plugin

Signed-off-by: Evandro Magalhaes Leite Junior <evandro.leite@serpro.gov.br>
Signed-off-by: Gustavo Jaruga <darkshades@gmail.com>
Signed-off-by: Leandro Nunes dos Santos <leandro.santos@serpro.gov.br>
Signed-off-by: Marcos Ronaldo <marcos.rpj2@gmail.com>
Signed-off-by: Victor Costa <vfcosta@gmail.com>
app/controllers/my_profile/profile_design_controller.rb
@@ -67,4 +67,14 @@ class ProfileDesignController &lt; BoxOrganizerController @@ -67,4 +67,14 @@ class ProfileDesignController &lt; BoxOrganizerController
67 blocks 67 blocks
68 end 68 end
69 69
  70 + def update_categories
  71 + @object = params[:id] ? @profile.blocks.find(params[:id]) : Block.new
  72 + @categories = @toplevel_categories = environment.top_level_categories
  73 + if params[:category_id]
  74 + @current_category = Category.find(params[:category_id])
  75 + @categories = @current_category.children
  76 + end
  77 + render :template => 'shared/update_categories', :locals => { :category => @current_category, :object_name => 'block' }
  78 + end
  79 +
70 end 80 end
plugins/community_track/controllers/public/community_track_plugin_public_controller.rb
@@ -6,14 +6,15 @@ class CommunityTrackPluginPublicController &lt; PublicController @@ -6,14 +6,15 @@ class CommunityTrackPluginPublicController &lt; PublicController
6 6
7 def view_tracks 7 def view_tracks
8 block = Block.find(params[:id]) 8 block = Block.find(params[:id])
  9 + instance_eval(&block.set_seed)
9 p = params[:page].to_i 10 p = params[:page].to_i
10 per_page = params[:per_page] 11 per_page = params[:per_page]
11 per_page ||= block.limit 12 per_page ||= block.limit
12 per_page = per_page.to_i 13 per_page = per_page.to_i
13 - tracks = block.tracks(p, per_page) 14 + @tracks = block.tracks(p, per_page)
14 15
15 render :update do |page| 16 render :update do |page|
16 - page.insert_html :bottom, "track_list_#{block.id}", :partial => "blocks/#{block.track_partial}", :collection => tracks, :locals => {:block => block} 17 + page.insert_html :bottom, "track_list_#{block.id}", :partial => "blocks/#{block.track_partial}", :collection => @tracks, :locals => {:block => block}
17 18
18 if block.has_page?(p+1, per_page) 19 if block.has_page?(p+1, per_page)
19 page.replace_html "track_list_more_#{block.id}", :partial => 'blocks/track_list_more', :locals => {:block => block, :page => p+1, :force_same_page => params[:force_same_page], :per_page => per_page} 20 page.replace_html "track_list_more_#{block.id}", :partial => 'blocks/track_list_more', :locals => {:block => block, :page => p+1, :force_same_page => params[:force_same_page], :per_page => per_page}
@@ -26,6 +27,7 @@ class CommunityTrackPluginPublicController &lt; PublicController @@ -26,6 +27,7 @@ class CommunityTrackPluginPublicController &lt; PublicController
26 def all_tracks 27 def all_tracks
27 @per_page = 8 #FIXME 28 @per_page = 8 #FIXME
28 @block = Block.find(params[:id]) 29 @block = Block.find(params[:id])
  30 + instance_eval(&@block.set_seed)
29 @tracks = @block.tracks(1, @per_page) 31 @tracks = @block.tracks(1, @per_page)
30 @show_more = @block.has_page?(2, @per_page) 32 @show_more = @block.has_page?(2, @per_page)
31 end 33 end
plugins/community_track/lib/community_track_plugin.rb
@@ -13,7 +13,7 @@ class CommunityTrackPlugin &lt; Noosfero::Plugin @@ -13,7 +13,7 @@ class CommunityTrackPlugin &lt; Noosfero::Plugin
13 end 13 end
14 14
15 def content_types 15 def content_types
16 - if context.respond_to?(:params) && context.params 16 + if context.kind_of?(CmsController) && context.respond_to?(:params) && context.params
17 types = [] 17 types = []
18 parent_id = context.params[:parent_id] 18 parent_id = context.params[:parent_id]
19 types << CommunityTrackPlugin::Track if context.profile.community? && !parent_id 19 types << CommunityTrackPlugin::Track if context.profile.community? && !parent_id
plugins/community_track/lib/community_track_plugin/step.rb
@@ -60,7 +60,7 @@ class CommunityTrackPlugin::Step &lt; Folder @@ -60,7 +60,7 @@ class CommunityTrackPlugin::Step &lt; Folder
60 accept_comments 60 accept_comments
61 end 61 end
62 62
63 - def self.enabled_tools 63 + def enabled_tools
64 [TinyMceArticle, Forum] 64 [TinyMceArticle, Forum]
65 end 65 end
66 66
plugins/community_track/lib/community_track_plugin/track.rb
@@ -7,6 +7,11 @@ class CommunityTrackPlugin::Track &lt; Folder @@ -7,6 +7,11 @@ class CommunityTrackPlugin::Track &lt; Folder
7 7
8 attr_accessible :goals, :expected_results 8 attr_accessible :goals, :expected_results
9 9
  10 + def comments_count
  11 + @comments_count = sum_children_comments self unless @comments_count
  12 + @comments_count
  13 + end
  14 +
10 def validate_categories 15 def validate_categories
11 errors.add(:categories, _('should not be blank.')) if categories.empty? && pending_categorizations.blank? 16 errors.add(:categories, _('should not be blank.')) if categories.empty? && pending_categorizations.blank?
12 end 17 end
@@ -49,8 +54,13 @@ class CommunityTrackPlugin::Track &lt; Folder @@ -49,8 +54,13 @@ class CommunityTrackPlugin::Track &lt; Folder
49 false 54 false
50 end 55 end
51 56
52 - def comments_count  
53 - steps_unsorted.joins(:children).sum('children_articles.comments_count') 57 + def sum_children_comments node
  58 + result = 0
  59 + node.children.each do |c|
  60 + result += c.comments_count
  61 + result += sum_children_comments c
  62 + end
  63 + result
54 end 64 end
55 65
56 def css_class_name 66 def css_class_name
plugins/community_track/lib/community_track_plugin/track_list_block.rb
@@ -5,6 +5,9 @@ class CommunityTrackPlugin::TrackListBlock &lt; Block @@ -5,6 +5,9 @@ class CommunityTrackPlugin::TrackListBlock &lt; Block
5 settings_items :limit, :type => :integer, :default => 3 5 settings_items :limit, :type => :integer, :default => 3
6 settings_items :more_another_page, :type => :boolean, :default => false 6 settings_items :more_another_page, :type => :boolean, :default => false
7 settings_items :category_ids, :type => Array, :default => [] 7 settings_items :category_ids, :type => Array, :default => []
  8 + settings_items :order, :type => :string, :default => 'hits'
  9 +
  10 + attr_accessible :more_another_page, :category_ids, :order
8 11
9 def self.description 12 def self.description
10 _('Track List') 13 _('Track List')
@@ -23,7 +26,16 @@ class CommunityTrackPlugin::TrackListBlock &lt; Block @@ -23,7 +26,16 @@ class CommunityTrackPlugin::TrackListBlock &lt; Block
23 end 26 end
24 27
25 def tracks(page=1, per_page=limit) 28 def tracks(page=1, per_page=limit)
26 - all_tracks.order('hits DESC').paginate(:per_page => per_page, :page => page) 29 + tracks = all_tracks
  30 + tracks = case order
  31 + when 'newer'
  32 + tracks.order('created_at DESC')
  33 + when 'random'
  34 + tracks.order('random()')
  35 + else
  36 + tracks.order('hits DESC')
  37 + end
  38 + tracks.paginate(:per_page => per_page, :page => page)
27 end 39 end
28 40
29 def count_tracks 41 def count_tracks
@@ -53,6 +65,7 @@ class CommunityTrackPlugin::TrackListBlock &lt; Block @@ -53,6 +65,7 @@ class CommunityTrackPlugin::TrackListBlock &lt; Block
53 def content(args={}) 65 def content(args={})
54 block = self 66 block = self
55 proc do 67 proc do
  68 + instance_eval(&block.set_seed(true))
56 render :file => 'blocks/track_list', :locals => {:block => block} 69 render :file => 'blocks/track_list', :locals => {:block => block}
57 end 70 end
58 end 71 end
@@ -73,4 +86,26 @@ class CommunityTrackPlugin::TrackListBlock &lt; Block @@ -73,4 +86,26 @@ class CommunityTrackPlugin::TrackListBlock &lt; Block
73 { :profile => [:article, :category], :environment => [:article, :category] } 86 { :profile => [:article, :category], :environment => [:article, :category] }
74 end 87 end
75 88
  89 + def timeout
  90 + 1.hour
  91 + end
  92 +
  93 + def set_seed(new_seed=false)
  94 + block = self
  95 + proc do
  96 + if block.order == 'random'
  97 + if new_seed || cookies[:_noosfero_community_tracks_rand_seed].blank?
  98 + cookies[:_noosfero_community_tracks_rand_seed] = {value: rand, expires: Time.now + 600}
  99 + end
  100 + #XXX postgresql specific
  101 + seed_val = Environment.connection.quote(cookies[:_noosfero_community_tracks_rand_seed])
  102 + Environment.connection.execute("select setseed(#{seed_val})")
  103 + end
  104 + end
  105 + end
  106 +
  107 + def self.order_options
  108 + {_('Hits') => 'hits', _('Random') => 'random', _('Most Recent') => 'newer'}
  109 + end
  110 +
76 end 111 end
plugins/community_track/test/functional/community_track_plugin_cms_controller_test.rb
@@ -3,6 +3,9 @@ require_relative &#39;../test_helper&#39; @@ -3,6 +3,9 @@ require_relative &#39;../test_helper&#39;
3 class CmsControllerTest < ActionController::TestCase 3 class CmsControllerTest < ActionController::TestCase
4 4
5 def setup 5 def setup
  6 + @environment = Environment.default
  7 + @environment.enabled_plugins = ['CommunityTrackPlugin']
  8 + @environment.save!
6 @profile = fast_create(Community) 9 @profile = fast_create(Community)
7 @track = create_track('track', @profile) 10 @track = create_track('track', @profile)
8 @step = CommunityTrackPlugin::Step.create!(:name => 'step1', :body => 'body', :profile => @profile, :parent => @track, :published => false, :end_date => Date.today, :start_date => Date.today) 11 @step = CommunityTrackPlugin::Step.create!(:name => 'step1', :body => 'body', :profile => @profile, :parent => @track, :published => false, :end_date => Date.today, :start_date => Date.today)
@@ -36,4 +39,15 @@ class CmsControllerTest &lt; ActionController::TestCase @@ -36,4 +39,15 @@ class CmsControllerTest &lt; ActionController::TestCase
36 assert_equal 'changed', @step.name 39 assert_equal 'changed', @step.name
37 end 40 end
38 41
  42 + should 'have parent_id present in form' do
  43 + get :new, :parent_id => @track.id, :profile => @profile.identifier, :type => CommunityTrackPlugin::Step
  44 + assert_tag :tag => 'input', :attributes => { :name => 'parent_id' }
  45 + end
  46 +
  47 + should 'be able to create an step with a parent' do
  48 + amount_of_steps = CommunityTrackPlugin::Step.count
  49 + post :new, :parent_id => @track.id, :profile => @profile.identifier, :type => CommunityTrackPlugin::Step, :article => {:name => 'some', :body => 'some'}
  50 + assert_equal amount_of_steps + 1, CommunityTrackPlugin::Step.count
  51 + end
  52 +
39 end 53 end
plugins/community_track/test/functional/community_track_plugin_public_controller_test.rb
@@ -97,4 +97,19 @@ class CommunityTrackPluginPublicControllerTest &lt; ActionController::TestCase @@ -97,4 +97,19 @@ class CommunityTrackPluginPublicControllerTest &lt; ActionController::TestCase
97 assert_tag :tag => 'div', :attributes => {:id => 'errorExplanation'} 97 assert_tag :tag => 'div', :attributes => {:id => 'errorExplanation'}
98 end 98 end
99 99
  100 + should 'do not repeat tracks when paging a block with random order' do
  101 + @track.destroy
  102 + @block.order = 'random'
  103 + @block.save!
  104 +
  105 + per_page = 4
  106 + (per_page*3).times {|i| create_track("track_#{i}", @community) }
  107 +
  108 + tracks = 3.times.map do |i|
  109 + xhr :get, :view_tracks, :id => @block.id, :page => i+1, :per_page => per_page
  110 + assigns[:tracks].all
  111 + end.flatten
  112 + assert_equal tracks.count, tracks.uniq.count
  113 + end
  114 +
100 end 115 end
plugins/community_track/test/unit/community_track_plugin/step_test.rb
@@ -235,8 +235,8 @@ class StepTest &lt; ActiveSupport::TestCase @@ -235,8 +235,8 @@ class StepTest &lt; ActiveSupport::TestCase
235 end 235 end
236 236
237 should 'return enabled tools for a step' do 237 should 'return enabled tools for a step' do
238 - assert_includes CommunityTrackPlugin::Step.enabled_tools, TinyMceArticle  
239 - assert_includes CommunityTrackPlugin::Step.enabled_tools, Forum 238 + assert_includes @step.enabled_tools, TinyMceArticle
  239 + assert_includes @step.enabled_tools, Forum
240 end 240 end
241 241
242 should 'return class for selected tool' do 242 should 'return class for selected tool' do
plugins/community_track/test/unit/community_track_plugin/track_list_block_test.rb
@@ -9,7 +9,7 @@ class TrackListBlockTest &lt; ActiveSupport::TestCase @@ -9,7 +9,7 @@ class TrackListBlockTest &lt; ActiveSupport::TestCase
9 @block = create(CommunityTrackPlugin::TrackListBlock, :box => box) 9 @block = create(CommunityTrackPlugin::TrackListBlock, :box => box)
10 end 10 end
11 11
12 - attr_reader :profile 12 + attr_reader :profile, :track
13 13
14 should 'describe yourself' do 14 should 'describe yourself' do
15 assert CommunityTrackPlugin::TrackListBlock.description 15 assert CommunityTrackPlugin::TrackListBlock.description
@@ -111,4 +111,20 @@ class TrackListBlockTest &lt; ActiveSupport::TestCase @@ -111,4 +111,20 @@ class TrackListBlockTest &lt; ActiveSupport::TestCase
111 assert_equivalent [], @block.categories 111 assert_equivalent [], @block.categories
112 end 112 end
113 113
  114 + should 'list tracks in hits order by default' do
  115 + track2 = create_track("track2", profile)
  116 + track.update_attribute(:hits, 2)
  117 + track2.update_attribute(:hits, 1)
  118 + assert_equal [track, track2], @block.tracks
  119 + end
  120 +
  121 + should 'list tracks in newer order' do
  122 + @block.order = 'newer'
  123 + @block.save!
  124 + track2 = create_track("track2", profile)
  125 + track2.update_attribute(:created_at, Date.today)
  126 + track.update_attribute(:created_at, Date.today - 1.day)
  127 + assert_equal [track2, track], @block.tracks
  128 + end
  129 +
114 end 130 end
plugins/community_track/test/unit/community_track_plugin/track_test.rb
@@ -3,10 +3,12 @@ require_relative &#39;../../test_helper&#39; @@ -3,10 +3,12 @@ require_relative &#39;../../test_helper&#39;
3 class TrackTest < ActiveSupport::TestCase 3 class TrackTest < ActiveSupport::TestCase
4 4
5 def setup 5 def setup
6 - @profile = fast_create(Community) 6 + @profile = create(Community)
7 @track = create_track('track', @profile) 7 @track = create_track('track', @profile)
8 - @step = CommunityTrackPlugin::Step.create!(:parent => @track, :start_date => Date.today, :end_date => Date.today, :name => 'step', :profile => @profile) 8 + @step = CommunityTrackPlugin::Step.create!(:parent => @track, :start_date => DateTime.now, :end_date => DateTime.now, :name => 'step', :profile => @profile)
  9 + @track.children << @step
9 @tool = fast_create(Article, :parent_id => @step.id, :profile_id => @profile.id) 10 @tool = fast_create(Article, :parent_id => @step.id, :profile_id => @profile.id)
  11 + @step.children << @tool
10 end 12 end
11 13
12 should 'describe yourself' do 14 should 'describe yourself' do
@@ -25,8 +27,18 @@ class TrackTest &lt; ActiveSupport::TestCase @@ -25,8 +27,18 @@ class TrackTest &lt; ActiveSupport::TestCase
25 assert_equal 0, @track.comments_count 27 assert_equal 0, @track.comments_count
26 owner = create_user('testuser').person 28 owner = create_user('testuser').person
27 article = create(Article, :name => 'article', :parent_id => @step.id, :profile_id => owner.id) 29 article = create(Article, :name => 'article', :parent_id => @step.id, :profile_id => owner.id)
  30 + @track.children << @step
  31 + @step.children << article
28 comment = create(Comment, :source => article, :author_id => owner.id) 32 comment = create(Comment, :source => article, :author_id => owner.id)
29 - assert_equal 1, @track.comments_count 33 + @step2 = CommunityTrackPlugin::Step.create!(:parent => @track, :start_date => DateTime.now, :end_date => DateTime.now, :name => 'step2', :profile => @profile)
  34 + @step2.tool_type = 'Forum'
  35 + forum = fast_create(Forum, :parent_id => @step2.id, :profile_id => owner.id)
  36 + article_forum = create(Article, :name => 'article_forum', :parent_id => forum.id, :profile_id => owner.id)
  37 + forum.children << article_forum
  38 + forum_comment = create(Comment, :source => article_forum, :author_id => owner.id)
  39 + @track.children = [@step, @step2]
  40 + @track = Article.find(@track.id)
  41 + assert_equal 2, @track.comments_count
30 end 42 end
31 43
32 should 'return children steps' do 44 should 'return children steps' do
@@ -35,6 +47,7 @@ class TrackTest &lt; ActiveSupport::TestCase @@ -35,6 +47,7 @@ class TrackTest &lt; ActiveSupport::TestCase
35 47
36 should 'do not return other articles type at steps' do 48 should 'do not return other articles type at steps' do
37 article = fast_create(Article, :parent_id => @track.id, :profile_id => @track.profile.id) 49 article = fast_create(Article, :parent_id => @track.id, :profile_id => @track.profile.id)
  50 + @track = Article.find(@track.id)
38 assert_includes @track.children, article 51 assert_includes @track.children, article
39 assert_equal [@step], @track.steps_unsorted 52 assert_equal [@step], @track.steps_unsorted
40 end 53 end
plugins/community_track/test/unit/community_track_plugin_test.rb
@@ -7,6 +7,7 @@ class CommunityTrackPluginTest &lt; ActiveSupport::TestCase @@ -7,6 +7,7 @@ class CommunityTrackPluginTest &lt; ActiveSupport::TestCase
7 @profile = fast_create(Community) 7 @profile = fast_create(Community)
8 @params = {} 8 @params = {}
9 @context = mock 9 @context = mock
  10 + @context.stubs(:kind_of?).returns(CmsController)
10 @context.stubs(:profile).returns(@profile) 11 @context.stubs(:profile).returns(@profile)
11 @context.stubs(:params).returns(@params) 12 @context.stubs(:params).returns(@params)
12 @plugin.stubs(:context).returns(@context) 13 @plugin.stubs(:context).returns(@context)
plugins/community_track/views/blocks/_track_list_more.html.erb
@@ -6,7 +6,7 @@ @@ -6,7 +6,7 @@
6 </div> 6 </div>
7 <% else %> 7 <% else %>
8 <div class="more"> 8 <div class="more">
9 - <%= link_to_remote(c_('More'), :url => {:id => block.id, :controller => 'community_track_plugin_public', :action => 'view_tracks', :page => page, :per_page => per_page, :force_same_page => force_same_page}, :loaded => visual_effect(:highlight, "track_card_list_#{block.id}")) %> 9 + <%= link_to_remote(c_('More'), :url => {:id => block.id, :controller => 'community_track_plugin_public', :action => 'view_tracks', :page => page, :per_page => per_page, :force_same_page => force_same_page}, :method => :get) %>
10 </div> 10 </div>
11 <% end %> 11 <% end %>
12 </div> 12 </div>
plugins/community_track/views/box_organizer/community_track_plugin/_track_list_block.html.erb
1 <div id='edit-track-list-block'> 1 <div id='edit-track-list-block'>
2 <%= labelled_form_field c_('Limit of items'), text_field(:block, :limit, :size => 3) %> 2 <%= labelled_form_field c_('Limit of items'), text_field(:block, :limit, :size => 3) %>
  3 +
  4 + <%= labelled_form_field c_('Order'),
  5 + select(:block, :order,
  6 + options_for_select(CommunityTrackPlugin::TrackListBlock.order_options, :selected => @block.order)) %>
  7 +
3 <%= labelled_form_field check_box(:block, :more_another_page) + _('Show more at another page'), '' %> 8 <%= labelled_form_field check_box(:block, :more_another_page) + _('Show more at another page'), '' %>
4 <%= select_categories(:block, _('Select Categories')) %> 9 <%= select_categories(:block, _('Select Categories')) %>
5 <br/> 10 <br/>
plugins/community_track/views/cms/community_track_plugin/_step.html.erb
@@ -11,8 +11,10 @@ @@ -11,8 +11,10 @@
11 { :size => 14 }) 11 { :size => 14 })
12 )) %> 12 )) %>
13 13
14 - <%= labelled_form_field(_('Tool type'), select(:article, :tool_type, CommunityTrackPlugin::Step.enabled_tools.map {|t| [t.short_description, t.name]} )) %> 14 + <%= labelled_form_field(_('Tool type'), select(:article, :tool_type, @article.enabled_tools.map {|t| [t.short_description, t.name]} )) %>
15 <%= hidden_field_tag('success_back_to', url_for(@article.parent.view_url)) %> 15 <%= hidden_field_tag('success_back_to', url_for(@article.parent.view_url)) %>
  16 + <%= hidden_field_tag('parent_id', @article.parent_id) %>
  17 +
16 </div> 18 </div>
17 19
18 <%= labelled_form_field check_box(:article, :hidden) + _('Hidden Step'), '' %> 20 <%= labelled_form_field check_box(:article, :hidden) + _('Hidden Step'), '' %>
test/functional/profile_design_controller_test.rb
@@ -711,4 +711,17 @@ class ProfileDesignControllerTest &lt; ActionController::TestCase @@ -711,4 +711,17 @@ class ProfileDesignControllerTest &lt; ActionController::TestCase
711 end 711 end
712 end 712 end
713 713
  714 + should 'update selected categories in blocks' do
  715 + env = Environment.default
  716 + c1 = env.categories.build(:name => "Test category 1"); c1.save!
  717 +
  718 + block = profile.blocks.last
  719 +
  720 + Block.any_instance.expects(:accept_category?).at_least_once.returns true
  721 +
  722 + xhr :get, :update_categories, :profile => profile.identifier, :id => block.id, :category_id => c1.id
  723 +
  724 + assert_equal assigns(:current_category), c1
  725 + end
  726 +
714 end 727 end