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 ac7a3c1..1c90d1f 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 @@ -7,14 +7,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} @@ -27,6 +28,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/track_list_block.rb b/plugins/community_track/lib/community_track_plugin/track_list_block.rb index 8cce31b..fefc03c 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') @@ -19,7 +22,18 @@ 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 'hits' + tracks.order('hits DESC') + when 'newer' + tracks.order('created_at DESC') + when 'random' + tracks.order('random()') + else + tracks + end + tracks.paginate(:per_page => per_page, :page => page) end def count_tracks @@ -49,6 +63,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 @@ -69,4 +84,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_public_controller_test.rb b/plugins/community_track/test/functional/community_track_plugin_public_controller_test.rb index 40f19ea..e0b5307 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 @@ -100,4 +100,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/track_list_block_test.rb b/plugins/community_track/test/unit/community_track_plugin/track_list_block_test.rb index 864dd2d..53b6543 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/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 113b86a..2530ec8 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 @@