From 11a81060fde6ffa40fc91365d2e3326fb5657d6f Mon Sep 17 00:00:00 2001 From: Marcos Ronaldo Date: Tue, 15 Mar 2016 17:39:29 -0300 Subject: [PATCH] fixes community track plugin --- app/controllers/my_profile/profile_design_controller.rb | 10 ++++++++++ plugins/community_track/controllers/public/community_track_plugin_public_controller.rb | 6 ++++-- plugins/community_track/lib/community_track_plugin.rb | 2 +- plugins/community_track/lib/community_track_plugin/step.rb | 2 +- plugins/community_track/lib/community_track_plugin/track.rb | 14 ++++++++++++-- plugins/community_track/lib/community_track_plugin/track_list_block.rb | 37 ++++++++++++++++++++++++++++++++++++- plugins/community_track/test/functional/community_track_plugin_cms_controller_test.rb | 14 ++++++++++++++ plugins/community_track/test/functional/community_track_plugin_public_controller_test.rb | 15 +++++++++++++++ plugins/community_track/test/unit/community_track_plugin/step_test.rb | 4 ++-- plugins/community_track/test/unit/community_track_plugin/track_list_block_test.rb | 18 +++++++++++++++++- plugins/community_track/test/unit/community_track_plugin/track_test.rb | 19 ++++++++++++++++--- plugins/community_track/test/unit/community_track_plugin_test.rb | 1 + plugins/community_track/views/blocks/_track_list_more.html.erb | 2 +- plugins/community_track/views/box_organizer/community_track_plugin/_track_list_block.html.erb | 5 +++++ plugins/community_track/views/cms/community_track_plugin/_step.html.erb | 4 +++- test/functional/profile_design_controller_test.rb | 13 +++++++++++++ 16 files changed, 151 insertions(+), 15 deletions(-) diff --git a/app/controllers/my_profile/profile_design_controller.rb b/app/controllers/my_profile/profile_design_controller.rb index fecb028..f04db5a 100644 --- a/app/controllers/my_profile/profile_design_controller.rb +++ b/app/controllers/my_profile/profile_design_controller.rb @@ -67,4 +67,14 @@ class ProfileDesignController < BoxOrganizerController blocks end + def update_categories + @object = params[:id] ? @profile.blocks.find(params[:id]) : Block.new + @categories = @toplevel_categories = environment.top_level_categories + if params[:category_id] + @current_category = Category.find(params[:category_id]) + @categories = @current_category.children + end + render :template => 'shared/update_categories', :locals => { :category => @current_category, :object_name => 'block' } + end + end diff --git a/plugins/community_track/controllers/public/community_track_plugin_public_controller.rb b/plugins/community_track/controllers/public/community_track_plugin_public_controller.rb index 0808097..254e275 100644 --- a/plugins/community_track/controllers/public/community_track_plugin_public_controller.rb +++ b/plugins/community_track/controllers/public/community_track_plugin_public_controller.rb @@ -6,14 +6,15 @@ class CommunityTrackPluginPublicController < PublicController def view_tracks block = Block.find(params[:id]) + instance_eval(&block.set_seed) p = params[:page].to_i per_page = params[:per_page] per_page ||= block.limit per_page = per_page.to_i - tracks = block.tracks(p, per_page) + @tracks = block.tracks(p, per_page) render :update do |page| - page.insert_html :bottom, "track_list_#{block.id}", :partial => "blocks/#{block.track_partial}", :collection => tracks, :locals => {:block => block} + page.insert_html :bottom, "track_list_#{block.id}", :partial => "blocks/#{block.track_partial}", :collection => @tracks, :locals => {:block => block} if block.has_page?(p+1, per_page) 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 < PublicController def all_tracks @per_page = 8 #FIXME @block = Block.find(params[:id]) + instance_eval(&@block.set_seed) @tracks = @block.tracks(1, @per_page) @show_more = @block.has_page?(2, @per_page) end diff --git a/plugins/community_track/lib/community_track_plugin.rb b/plugins/community_track/lib/community_track_plugin.rb index e7a75b3..c4eb4e6 100644 --- a/plugins/community_track/lib/community_track_plugin.rb +++ b/plugins/community_track/lib/community_track_plugin.rb @@ -13,7 +13,7 @@ class CommunityTrackPlugin < Noosfero::Plugin end def content_types - if context.respond_to?(:params) && context.params + if context.kind_of?(CmsController) && context.respond_to?(:params) && context.params types = [] parent_id = context.params[:parent_id] types << CommunityTrackPlugin::Track if context.profile.community? && !parent_id diff --git a/plugins/community_track/lib/community_track_plugin/step.rb b/plugins/community_track/lib/community_track_plugin/step.rb index 97dd8ca..e83ac50 100644 --- a/plugins/community_track/lib/community_track_plugin/step.rb +++ b/plugins/community_track/lib/community_track_plugin/step.rb @@ -60,7 +60,7 @@ class CommunityTrackPlugin::Step < Folder accept_comments end - def self.enabled_tools + def enabled_tools [TinyMceArticle, Forum] end diff --git a/plugins/community_track/lib/community_track_plugin/track.rb b/plugins/community_track/lib/community_track_plugin/track.rb index afd7fed..68affd1 100644 --- a/plugins/community_track/lib/community_track_plugin/track.rb +++ b/plugins/community_track/lib/community_track_plugin/track.rb @@ -7,6 +7,11 @@ class CommunityTrackPlugin::Track < Folder attr_accessible :goals, :expected_results + def comments_count + @comments_count = sum_children_comments self unless @comments_count + @comments_count + end + def validate_categories errors.add(:categories, _('should not be blank.')) if categories.empty? && pending_categorizations.blank? end @@ -49,8 +54,13 @@ class CommunityTrackPlugin::Track < Folder false end - def comments_count - steps_unsorted.joins(:children).sum('children_articles.comments_count') + def sum_children_comments node + result = 0 + node.children.each do |c| + result += c.comments_count + result += sum_children_comments c + end + result end def css_class_name diff --git a/plugins/community_track/lib/community_track_plugin/track_list_block.rb b/plugins/community_track/lib/community_track_plugin/track_list_block.rb index 95bd73e..4bebc24 100644 --- a/plugins/community_track/lib/community_track_plugin/track_list_block.rb +++ b/plugins/community_track/lib/community_track_plugin/track_list_block.rb @@ -5,6 +5,9 @@ class CommunityTrackPlugin::TrackListBlock < Block settings_items :limit, :type => :integer, :default => 3 settings_items :more_another_page, :type => :boolean, :default => false settings_items :category_ids, :type => Array, :default => [] + settings_items :order, :type => :string, :default => 'hits' + + attr_accessible :more_another_page, :category_ids, :order def self.description _('Track List') @@ -23,7 +26,16 @@ class CommunityTrackPlugin::TrackListBlock < Block end def tracks(page=1, per_page=limit) - all_tracks.order('hits DESC').paginate(:per_page => per_page, :page => page) + tracks = all_tracks + tracks = case order + when 'newer' + tracks.order('created_at DESC') + when 'random' + tracks.order('random()') + else + tracks.order('hits DESC') + end + tracks.paginate(:per_page => per_page, :page => page) end def count_tracks @@ -53,6 +65,7 @@ class CommunityTrackPlugin::TrackListBlock < Block def content(args={}) block = self proc do + instance_eval(&block.set_seed(true)) render :file => 'blocks/track_list', :locals => {:block => block} end end @@ -73,4 +86,26 @@ class CommunityTrackPlugin::TrackListBlock < Block { :profile => [:article, :category], :environment => [:article, :category] } end + def timeout + 1.hour + end + + def set_seed(new_seed=false) + block = self + proc do + if block.order == 'random' + if new_seed || cookies[:_noosfero_community_tracks_rand_seed].blank? + cookies[:_noosfero_community_tracks_rand_seed] = {value: rand, expires: Time.now + 600} + end + #XXX postgresql specific + seed_val = Environment.connection.quote(cookies[:_noosfero_community_tracks_rand_seed]) + Environment.connection.execute("select setseed(#{seed_val})") + end + end + end + + def self.order_options + {_('Hits') => 'hits', _('Random') => 'random', _('Most Recent') => 'newer'} + end + end diff --git a/plugins/community_track/test/functional/community_track_plugin_cms_controller_test.rb b/plugins/community_track/test/functional/community_track_plugin_cms_controller_test.rb index 28e2812..455bcee 100644 --- a/plugins/community_track/test/functional/community_track_plugin_cms_controller_test.rb +++ b/plugins/community_track/test/functional/community_track_plugin_cms_controller_test.rb @@ -3,6 +3,9 @@ require_relative '../test_helper' class CmsControllerTest < ActionController::TestCase def setup + @environment = Environment.default + @environment.enabled_plugins = ['CommunityTrackPlugin'] + @environment.save! @profile = fast_create(Community) @track = create_track('track', @profile) @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 < ActionController::TestCase assert_equal 'changed', @step.name end + should 'have parent_id present in form' do + get :new, :parent_id => @track.id, :profile => @profile.identifier, :type => CommunityTrackPlugin::Step + assert_tag :tag => 'input', :attributes => { :name => 'parent_id' } + end + + should 'be able to create an step with a parent' do + amount_of_steps = CommunityTrackPlugin::Step.count + post :new, :parent_id => @track.id, :profile => @profile.identifier, :type => CommunityTrackPlugin::Step, :article => {:name => 'some', :body => 'some'} + assert_equal amount_of_steps + 1, CommunityTrackPlugin::Step.count + end + end diff --git a/plugins/community_track/test/functional/community_track_plugin_public_controller_test.rb b/plugins/community_track/test/functional/community_track_plugin_public_controller_test.rb index 36e4526..b84c42b 100644 --- a/plugins/community_track/test/functional/community_track_plugin_public_controller_test.rb +++ b/plugins/community_track/test/functional/community_track_plugin_public_controller_test.rb @@ -97,4 +97,19 @@ class CommunityTrackPluginPublicControllerTest < ActionController::TestCase assert_tag :tag => 'div', :attributes => {:id => 'errorExplanation'} end + should 'do not repeat tracks when paging a block with random order' do + @track.destroy + @block.order = 'random' + @block.save! + + per_page = 4 + (per_page*3).times {|i| create_track("track_#{i}", @community) } + + tracks = 3.times.map do |i| + xhr :get, :view_tracks, :id => @block.id, :page => i+1, :per_page => per_page + assigns[:tracks].all + end.flatten + assert_equal tracks.count, tracks.uniq.count + end + end diff --git a/plugins/community_track/test/unit/community_track_plugin/step_test.rb b/plugins/community_track/test/unit/community_track_plugin/step_test.rb index 67085b0..4fe1949 100644 --- a/plugins/community_track/test/unit/community_track_plugin/step_test.rb +++ b/plugins/community_track/test/unit/community_track_plugin/step_test.rb @@ -235,8 +235,8 @@ class StepTest < ActiveSupport::TestCase end should 'return enabled tools for a step' do - assert_includes CommunityTrackPlugin::Step.enabled_tools, TinyMceArticle - assert_includes CommunityTrackPlugin::Step.enabled_tools, Forum + assert_includes @step.enabled_tools, TinyMceArticle + assert_includes @step.enabled_tools, Forum end should 'return class for selected tool' do diff --git a/plugins/community_track/test/unit/community_track_plugin/track_list_block_test.rb b/plugins/community_track/test/unit/community_track_plugin/track_list_block_test.rb index 6480e05..654a01e 100644 --- a/plugins/community_track/test/unit/community_track_plugin/track_list_block_test.rb +++ b/plugins/community_track/test/unit/community_track_plugin/track_list_block_test.rb @@ -9,7 +9,7 @@ class TrackListBlockTest < ActiveSupport::TestCase @block = create(CommunityTrackPlugin::TrackListBlock, :box => box) end - attr_reader :profile + attr_reader :profile, :track should 'describe yourself' do assert CommunityTrackPlugin::TrackListBlock.description @@ -111,4 +111,20 @@ class TrackListBlockTest < ActiveSupport::TestCase assert_equivalent [], @block.categories end + should 'list tracks in hits order by default' do + track2 = create_track("track2", profile) + track.update_attribute(:hits, 2) + track2.update_attribute(:hits, 1) + assert_equal [track, track2], @block.tracks + end + + should 'list tracks in newer order' do + @block.order = 'newer' + @block.save! + track2 = create_track("track2", profile) + track2.update_attribute(:created_at, Date.today) + track.update_attribute(:created_at, Date.today - 1.day) + assert_equal [track2, track], @block.tracks + end + end diff --git a/plugins/community_track/test/unit/community_track_plugin/track_test.rb b/plugins/community_track/test/unit/community_track_plugin/track_test.rb index 58d9841..fd86499 100644 --- a/plugins/community_track/test/unit/community_track_plugin/track_test.rb +++ b/plugins/community_track/test/unit/community_track_plugin/track_test.rb @@ -3,10 +3,12 @@ require_relative '../../test_helper' class TrackTest < ActiveSupport::TestCase def setup - @profile = fast_create(Community) + @profile = create(Community) @track = create_track('track', @profile) - @step = CommunityTrackPlugin::Step.create!(:parent => @track, :start_date => Date.today, :end_date => Date.today, :name => 'step', :profile => @profile) + @step = CommunityTrackPlugin::Step.create!(:parent => @track, :start_date => DateTime.now, :end_date => DateTime.now, :name => 'step', :profile => @profile) + @track.children << @step @tool = fast_create(Article, :parent_id => @step.id, :profile_id => @profile.id) + @step.children << @tool end should 'describe yourself' do @@ -25,8 +27,18 @@ class TrackTest < ActiveSupport::TestCase assert_equal 0, @track.comments_count owner = create_user('testuser').person article = create(Article, :name => 'article', :parent_id => @step.id, :profile_id => owner.id) + @track.children << @step + @step.children << article comment = create(Comment, :source => article, :author_id => owner.id) - assert_equal 1, @track.comments_count + @step2 = CommunityTrackPlugin::Step.create!(:parent => @track, :start_date => DateTime.now, :end_date => DateTime.now, :name => 'step2', :profile => @profile) + @step2.tool_type = 'Forum' + forum = fast_create(Forum, :parent_id => @step2.id, :profile_id => owner.id) + article_forum = create(Article, :name => 'article_forum', :parent_id => forum.id, :profile_id => owner.id) + forum.children << article_forum + forum_comment = create(Comment, :source => article_forum, :author_id => owner.id) + @track.children = [@step, @step2] + @track = Article.find(@track.id) + assert_equal 2, @track.comments_count end should 'return children steps' do @@ -35,6 +47,7 @@ class TrackTest < ActiveSupport::TestCase should 'do not return other articles type at steps' do article = fast_create(Article, :parent_id => @track.id, :profile_id => @track.profile.id) + @track = Article.find(@track.id) assert_includes @track.children, article assert_equal [@step], @track.steps_unsorted end diff --git a/plugins/community_track/test/unit/community_track_plugin_test.rb b/plugins/community_track/test/unit/community_track_plugin_test.rb index 90f935d..09136c7 100644 --- a/plugins/community_track/test/unit/community_track_plugin_test.rb +++ b/plugins/community_track/test/unit/community_track_plugin_test.rb @@ -7,6 +7,7 @@ class CommunityTrackPluginTest < ActiveSupport::TestCase @profile = fast_create(Community) @params = {} @context = mock + @context.stubs(:kind_of?).returns(CmsController) @context.stubs(:profile).returns(@profile) @context.stubs(:params).returns(@params) @plugin.stubs(:context).returns(@context) diff --git a/plugins/community_track/views/blocks/_track_list_more.html.erb b/plugins/community_track/views/blocks/_track_list_more.html.erb index d477880..86bb473 100644 --- a/plugins/community_track/views/blocks/_track_list_more.html.erb +++ b/plugins/community_track/views/blocks/_track_list_more.html.erb @@ -6,7 +6,7 @@ <% else %>
- <%= 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}")) %> + <%= 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) %>
<% end %> diff --git a/plugins/community_track/views/box_organizer/community_track_plugin/_track_list_block.html.erb b/plugins/community_track/views/box_organizer/community_track_plugin/_track_list_block.html.erb index 03ebf8b..5c923c1 100644 --- a/plugins/community_track/views/box_organizer/community_track_plugin/_track_list_block.html.erb +++ b/plugins/community_track/views/box_organizer/community_track_plugin/_track_list_block.html.erb @@ -1,5 +1,10 @@
<%= labelled_form_field c_('Limit of items'), text_field(:block, :limit, :size => 3) %> + + <%= labelled_form_field c_('Order'), + select(:block, :order, + options_for_select(CommunityTrackPlugin::TrackListBlock.order_options, :selected => @block.order)) %> + <%= labelled_form_field check_box(:block, :more_another_page) + _('Show more at another page'), '' %> <%= select_categories(:block, _('Select Categories')) %>
diff --git a/plugins/community_track/views/cms/community_track_plugin/_step.html.erb b/plugins/community_track/views/cms/community_track_plugin/_step.html.erb index d45ada8..df0b743 100644 --- a/plugins/community_track/views/cms/community_track_plugin/_step.html.erb +++ b/plugins/community_track/views/cms/community_track_plugin/_step.html.erb @@ -11,8 +11,10 @@ { :size => 14 }) )) %> - <%= labelled_form_field(_('Tool type'), select(:article, :tool_type, CommunityTrackPlugin::Step.enabled_tools.map {|t| [t.short_description, t.name]} )) %> + <%= labelled_form_field(_('Tool type'), select(:article, :tool_type, @article.enabled_tools.map {|t| [t.short_description, t.name]} )) %> <%= hidden_field_tag('success_back_to', url_for(@article.parent.view_url)) %> + <%= hidden_field_tag('parent_id', @article.parent_id) %> +
<%= labelled_form_field check_box(:article, :hidden) + _('Hidden Step'), '' %> diff --git a/test/functional/profile_design_controller_test.rb b/test/functional/profile_design_controller_test.rb index 536f093..6c4a527 100644 --- a/test/functional/profile_design_controller_test.rb +++ b/test/functional/profile_design_controller_test.rb @@ -711,4 +711,17 @@ class ProfileDesignControllerTest < ActionController::TestCase end end + should 'update selected categories in blocks' do + env = Environment.default + c1 = env.categories.build(:name => "Test category 1"); c1.save! + + block = profile.blocks.last + + Block.any_instance.expects(:accept_category?).at_least_once.returns true + + xhr :get, :update_categories, :profile => profile.identifier, :id => block.id, :category_id => c1.id + + assert_equal assigns(:current_category), c1 + end + end -- libgit2 0.21.2