Commit cd68aebb65fe497758bd021a24f068ba077a8091

Authored by Daniela Feitosa
2 parents 043be0b1 adb6d89d

Merge branch 'master_community_track' into 'master'

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>

See merge request !816
app/controllers/my_profile/cms_controller.rb
... ... @@ -3,6 +3,7 @@ class CmsController &lt; MyProfileController
3 3 protect 'edit_profile', :profile, :only => [:set_home_page]
4 4  
5 5 include ArticleHelper
  6 + include CategoriesHelper
6 7  
7 8 def search_tags
8 9 arg = params[:term].downcase
... ... @@ -256,12 +257,7 @@ class CmsController &lt; MyProfileController
256 257  
257 258 def update_categories
258 259 @object = params[:id] ? @profile.articles.find(params[:id]) : Article.new
259   - @categories = @toplevel_categories = environment.top_level_categories
260   - if params[:category_id]
261   - @current_category = Category.find(params[:category_id])
262   - @categories = @current_category.children
263   - end
264   - render :template => 'shared/update_categories', :locals => { :category => @current_category, :object_name => 'article' }
  260 + render_categories 'article'
265 261 end
266 262  
267 263 def search_communities_to_publish
... ...
app/controllers/my_profile/profile_design_controller.rb
... ... @@ -6,6 +6,7 @@ class ProfileDesignController &lt; BoxOrganizerController
6 6  
7 7 before_filter :protect_uneditable_block, :only => [:save]
8 8 before_filter :protect_fixed_block, :only => [:move_block]
  9 + include CategoriesHelper
9 10  
10 11 def protect_uneditable_block
11 12 block = boxes_holder.blocks.find(params[:id].gsub(/^block-/, ''))
... ... @@ -67,4 +68,9 @@ class ProfileDesignController &lt; BoxOrganizerController
67 68 blocks
68 69 end
69 70  
  71 + def update_categories
  72 + @object = params[:id] ? @profile.blocks.find(params[:id]) : Block.new
  73 + render_categories 'block'
  74 + end
  75 +
70 76 end
... ...
app/controllers/my_profile/profile_editor_controller.rb
... ... @@ -9,6 +9,7 @@ class ProfileEditorController &lt; MyProfileController
9 9 before_filter :check_user_can_edit_header_footer, :only => [:header_footer]
10 10 helper_method :has_welcome_page
11 11 helper CustomFieldsHelper
  12 + include CategoriesHelper
12 13  
13 14 def index
14 15 @pending_tasks = Task.to(profile).pending.without_spam.select{|i| user.has_permission?(i.permission, profile)}
... ... @@ -60,12 +61,7 @@ class ProfileEditorController &lt; MyProfileController
60 61  
61 62 def update_categories
62 63 @object = profile
63   - @categories = @toplevel_categories = environment.top_level_categories
64   - if params[:category_id]
65   - @current_category = Category.find(params[:category_id])
66   - @categories = @current_category.children
67   - end
68   - render :template => 'shared/update_categories', :locals => { :category => @current_category, :object_name => 'profile_data' }
  64 + render_categories 'profile_data'
69 65 end
70 66  
71 67 def header_footer
... ...
app/helpers/categories_helper.rb
... ... @@ -34,4 +34,15 @@ module CategoriesHelper
34 34 {:id => category_id ? "select-category-#{category_id}-link" : nil, :remote => true, :class => html_class}.merge(html_options)
35 35 end
36 36  
  37 + def render_categories object_name
  38 + @toplevel_categories = environment.top_level_categories
  39 + if params[:category_id]
  40 + @current_category = Category.find(params[:category_id])
  41 + @categories = @current_category.children
  42 + else
  43 + @categories = @toplevel_categories
  44 + end
  45 + render :template => 'shared/update_categories', :locals => { :category => @current_category, :object_name => object_name }
  46 + end
  47 +
37 48 end
... ...
app/models/block.rb
... ... @@ -4,8 +4,6 @@ class Block &lt; ActiveRecord::Base
4 4 :visualization_format, :language, :display_user,
5 5 :box, :edit_modes, :move_modes, :mirror
6 6  
7   - # to be able to generate HTML
8   - include ActionView::Helpers::UrlHelper
9 7 include ActionView::Helpers::TagHelper
10 8  
11 9 # Block-specific stuff
... ...
plugins/community_track/controllers/public/community_track_plugin_public_controller.rb
... ... @@ -6,14 +6,15 @@ class CommunityTrackPluginPublicController &lt; PublicController
6 6  
7 7 def view_tracks
8 8 block = Block.find(params[:id])
  9 + instance_eval(&block.set_seed)
9 10 p = params[:page].to_i
10 11 per_page = params[:per_page]
11 12 per_page ||= block.limit
12 13 per_page = per_page.to_i
13   - tracks = block.tracks(p, per_page)
  14 + @tracks = block.tracks(p, per_page)
14 15  
15 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 19 if block.has_page?(p+1, per_page)
19 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 27 def all_tracks
27 28 @per_page = 8 #FIXME
28 29 @block = Block.find(params[:id])
  30 + instance_eval(&@block.set_seed)
29 31 @tracks = @block.tracks(1, @per_page)
30 32 @show_more = @block.has_page?(2, @per_page)
31 33 end
... ...
plugins/community_track/lib/community_track_plugin.rb
... ... @@ -13,7 +13,7 @@ class CommunityTrackPlugin &lt; Noosfero::Plugin
13 13 end
14 14  
15 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 17 types = []
18 18 parent_id = context.params[:parent_id]
19 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 60 accept_comments
61 61 end
62 62  
63   - def self.enabled_tools
  63 + def enabled_tools
64 64 [TinyMceArticle, Forum]
65 65 end
66 66  
... ...
plugins/community_track/lib/community_track_plugin/track.rb
... ... @@ -7,6 +7,11 @@ class CommunityTrackPlugin::Track &lt; Folder
7 7  
8 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 15 def validate_categories
11 16 errors.add(:categories, _('should not be blank.')) if categories.empty? && pending_categorizations.blank?
12 17 end
... ... @@ -49,8 +54,13 @@ class CommunityTrackPlugin::Track &lt; Folder
49 54 false
50 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 64 end
55 65  
56 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 5 settings_items :limit, :type => :integer, :default => 3
6 6 settings_items :more_another_page, :type => :boolean, :default => false
7 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 12 def self.description
10 13 _('Track List')
... ... @@ -23,7 +26,16 @@ class CommunityTrackPlugin::TrackListBlock &lt; Block
23 26 end
24 27  
25 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 39 end
28 40  
29 41 def count_tracks
... ... @@ -50,27 +62,34 @@ class CommunityTrackPlugin::TrackListBlock &lt; Block
50 62 tracks
51 63 end
52 64  
53   - def content(args={})
54   - block = self
55   - proc do
56   - render :file => 'blocks/track_list', :locals => {:block => block}
57   - end
58   - end
59   -
60 65 def has_page?(page, per_page=limit)
61 66 return (page-1) * per_page < count_tracks
62 67 end
63 68  
64   - def footer
  69 + def self.expire_on
  70 + { :profile => [:article, :category], :environment => [:article, :category] }
  71 + end
  72 +
  73 + def timeout
  74 + 1.hour
  75 + end
  76 +
  77 + def set_seed(new_seed=false)
65 78 block = self
66   - return nil if !has_page?(2)
67 79 proc do
68   - render :partial => 'blocks/track_list_more', :locals => {:block => block, :page => 2, :per_page => block.limit}
  80 + if block.order == 'random'
  81 + if new_seed || cookies[:_noosfero_community_tracks_rand_seed].blank?
  82 + cookies[:_noosfero_community_tracks_rand_seed] = {value: rand, expires: Time.now + 600}
  83 + end
  84 + #XXX postgresql specific
  85 + seed_val = Environment.connection.quote(cookies[:_noosfero_community_tracks_rand_seed])
  86 + Environment.connection.execute("select setseed(#{seed_val})")
  87 + end
69 88 end
70 89 end
71 90  
72   - def self.expire_on
73   - { :profile => [:article, :category], :environment => [:article, :category] }
  91 + def self.order_options
  92 + {_('Hits') => 'hits', _('Random') => 'random', _('Most Recent') => 'newer'}
74 93 end
75 94  
76 95 end
... ...
plugins/community_track/test/functional/community_track_plugin_cms_controller_test.rb
... ... @@ -3,6 +3,9 @@ require_relative &#39;../test_helper&#39;
3 3 class CmsControllerTest < ActionController::TestCase
4 4  
5 5 def setup
  6 + @environment = Environment.default
  7 + @environment.enabled_plugins = ['CommunityTrackPlugin']
  8 + @environment.save!
6 9 @profile = fast_create(Community)
7 10 @track = create_track('track', @profile)
8 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 39 assert_equal 'changed', @step.name
37 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 53 end
... ...
plugins/community_track/test/functional/community_track_plugin_public_controller_test.rb
... ... @@ -97,4 +97,19 @@ class CommunityTrackPluginPublicControllerTest &lt; ActionController::TestCase
97 97 assert_tag :tag => 'div', :attributes => {:id => 'errorExplanation'}
98 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 115 end
... ...
plugins/community_track/test/unit/community_track_plugin/step_test.rb
... ... @@ -235,8 +235,8 @@ class StepTest &lt; ActiveSupport::TestCase
235 235 end
236 236  
237 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 240 end
241 241  
242 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 9 @block = create(CommunityTrackPlugin::TrackListBlock, :box => box)
10 10 end
11 11  
12   - attr_reader :profile
  12 + attr_reader :profile, :track
13 13  
14 14 should 'describe yourself' do
15 15 assert CommunityTrackPlugin::TrackListBlock.description
... ... @@ -34,16 +34,6 @@ class TrackListBlockTest &lt; ActiveSupport::TestCase
34 34 assert_equal @block.limit, @block.tracks.to_a.size
35 35 end
36 36  
37   - should 'return more link if has more tracks to show' do
38   - @block.limit.times { |i| create_track("track#{i}", profile) }
39   - assert @block.footer
40   - end
41   -
42   - should 'do not return more link if there is no more tracks to show' do
43   - (@block.limit-1).times { |i| create_track("track#{i}", profile) }
44   - refute @block.footer
45   - end
46   -
47 37 should 'count all tracks' do
48 38 @block.owner.articles.destroy_all
49 39 tracks_to_insert = @block.limit + 1
... ... @@ -111,4 +101,20 @@ class TrackListBlockTest &lt; ActiveSupport::TestCase
111 101 assert_equivalent [], @block.categories
112 102 end
113 103  
  104 + should 'list tracks in hits order by default' do
  105 + track2 = create_track("track2", profile)
  106 + track.update_attribute(:hits, 2)
  107 + track2.update_attribute(:hits, 1)
  108 + assert_equal [track, track2], @block.tracks
  109 + end
  110 +
  111 + should 'list tracks in newer order' do
  112 + @block.order = 'newer'
  113 + @block.save!
  114 + track2 = create_track("track2", profile)
  115 + track2.update_attribute(:created_at, Date.today)
  116 + track.update_attribute(:created_at, Date.today - 1.day)
  117 + assert_equal [track2, track], @block.tracks
  118 + end
  119 +
114 120 end
... ...
plugins/community_track/test/unit/community_track_plugin/track_test.rb
... ... @@ -3,10 +3,12 @@ require_relative &#39;../../test_helper&#39;
3 3 class TrackTest < ActiveSupport::TestCase
4 4  
5 5 def setup
6   - @profile = fast_create(Community)
  6 + @profile = create(Community)
7 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 10 @tool = fast_create(Article, :parent_id => @step.id, :profile_id => @profile.id)
  11 + @step.children << @tool
10 12 end
11 13  
12 14 should 'describe yourself' do
... ... @@ -25,8 +27,18 @@ class TrackTest &lt; ActiveSupport::TestCase
25 27 assert_equal 0, @track.comments_count
26 28 owner = create_user('testuser').person
27 29 article = create(Article, :name => 'article', :parent_id => @step.id, :profile_id => owner.id)
  30 + @track.children << @step
  31 + @step.children << article
28 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 42 end
31 43  
32 44 should 'return children steps' do
... ... @@ -35,6 +47,7 @@ class TrackTest &lt; ActiveSupport::TestCase
35 47  
36 48 should 'do not return other articles type at steps' do
37 49 article = fast_create(Article, :parent_id => @track.id, :profile_id => @track.profile.id)
  50 + @track = Article.find(@track.id)
38 51 assert_includes @track.children, article
39 52 assert_equal [@step], @track.steps_unsorted
40 53 end
... ...
plugins/community_track/test/unit/community_track_plugin_test.rb
... ... @@ -7,6 +7,7 @@ class CommunityTrackPluginTest &lt; ActiveSupport::TestCase
7 7 @profile = fast_create(Community)
8 8 @params = {}
9 9 @context = mock
  10 + @context.stubs(:kind_of?).returns(CmsController)
10 11 @context.stubs(:profile).returns(@profile)
11 12 @context.stubs(:params).returns(@params)
12 13 @plugin.stubs(:context).returns(@context)
... ...
plugins/community_track/views/blocks/_track_list_more.html.erb
... ... @@ -6,7 +6,7 @@
6 6 </div>
7 7 <% else %>
8 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 c_('More'), :id => block.id, :controller => 'community_track_plugin_public', :action => 'view_tracks', :page => page, :per_page => per_page, :force_same_page => force_same_page, :remote => true %>
10 10 </div>
11 11 <% end %>
12 12 </div>
... ...
plugins/community_track/views/blocks/footers/track_list.html.erb 0 → 100644
... ... @@ -0,0 +1,3 @@
  1 +<% if block.has_page?(2)%>
  2 + <%=render :partial => 'blocks/track_list_more', :locals => {:block => block, :page => 2, :per_page => block.limit} %>
  3 +<% end%>
... ...
plugins/community_track/views/box_organizer/community_track_plugin/_track_list_block.html.erb
1 1 <div id='edit-track-list-block'>
2 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 8 <%= labelled_form_field check_box(:block, :more_another_page) + _('Show more at another page'), '' %>
4 9 <%= select_categories(:block, _('Select Categories')) %>
5 10 <br/>
... ...
plugins/community_track/views/cms/community_track_plugin/_step.html.erb
... ... @@ -11,8 +11,10 @@
11 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 15 <%= hidden_field_tag('success_back_to', url_for(@article.parent.view_url)) %>
  16 + <%= hidden_field_tag('parent_id', @article.parent_id) %>
  17 +
16 18 </div>
17 19  
18 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 711 end
712 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 727 end
... ...