Commit 6d06bc156c6163ed1bbad57105a4be59a6d3f851

Authored by Victor Costa
1 parent 46f264fa

community_track: added order options for track list block

plugins/community_track/controllers/public/community_track_plugin_public_controller.rb
... ... @@ -7,14 +7,15 @@ class CommunityTrackPluginPublicController < PublicController
7 7  
8 8 def view_tracks
9 9 block = Block.find(params[:id])
  10 + instance_eval(&block.set_seed)
10 11 p = params[:page].to_i
11 12 per_page = params[:per_page]
12 13 per_page ||= block.limit
13 14 per_page = per_page.to_i
14   - tracks = block.tracks(p, per_page)
  15 + @tracks = block.tracks(p, per_page)
15 16  
16 17 render :update do |page|
17   - page.insert_html :bottom, "track_list_#{block.id}", :partial => "blocks/#{block.track_partial}", :collection => tracks, :locals => {:block => block}
  18 + page.insert_html :bottom, "track_list_#{block.id}", :partial => "blocks/#{block.track_partial}", :collection => @tracks, :locals => {:block => block}
18 19  
19 20 if block.has_page?(p+1, per_page)
20 21 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
27 28 def all_tracks
28 29 @per_page = 8 #FIXME
29 30 @block = Block.find(params[:id])
  31 + instance_eval(&@block.set_seed)
30 32 @tracks = @block.tracks(1, @per_page)
31 33 @show_more = @block.has_page?(2, @per_page)
32 34 end
... ...
plugins/community_track/lib/community_track_plugin/track_list_block.rb
... ... @@ -5,6 +5,9 @@ class CommunityTrackPlugin::TrackListBlock < 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')
... ... @@ -19,7 +22,18 @@ class CommunityTrackPlugin::TrackListBlock < Block
19 22 end
20 23  
21 24 def tracks(page=1, per_page=limit)
22   - all_tracks.order('hits DESC').paginate(:per_page => per_page, :page => page)
  25 + tracks = all_tracks
  26 + tracks = case order
  27 + when 'hits'
  28 + tracks.order('hits DESC')
  29 + when 'newer'
  30 + tracks.order('created_at DESC')
  31 + when 'random'
  32 + tracks.order('random()')
  33 + else
  34 + tracks
  35 + end
  36 + tracks.paginate(:per_page => per_page, :page => page)
23 37 end
24 38  
25 39 def count_tracks
... ... @@ -49,6 +63,7 @@ class CommunityTrackPlugin::TrackListBlock < Block
49 63 def content(args={})
50 64 block = self
51 65 proc do
  66 + instance_eval(&block.set_seed(true))
52 67 render :file => 'blocks/track_list', :locals => {:block => block}
53 68 end
54 69 end
... ... @@ -69,4 +84,26 @@ class CommunityTrackPlugin::TrackListBlock < Block
69 84 { :profile => [:article, :category], :environment => [:article, :category] }
70 85 end
71 86  
  87 + def timeout
  88 + 1.hour
  89 + end
  90 +
  91 + def set_seed(new_seed=false)
  92 + block = self
  93 + proc do
  94 + if block.order == 'random'
  95 + if new_seed || cookies[:_noosfero_community_tracks_rand_seed].blank?
  96 + cookies[:_noosfero_community_tracks_rand_seed] = {value: rand, expires: Time.now + 600}
  97 + end
  98 + #XXX postgresql specific
  99 + seed_val = environment.connection.quote(cookies[:_noosfero_community_tracks_rand_seed])
  100 + environment.connection.execute("select setseed(#{seed_val})")
  101 + end
  102 + end
  103 + end
  104 +
  105 + def self.order_options
  106 + {_('Hits') => 'hits', _('Random') => 'random', _('Most Recent') => 'newer'}
  107 + end
  108 +
72 109 end
... ...
plugins/community_track/test/functional/community_track_plugin_public_controller_test.rb
... ... @@ -100,4 +100,19 @@ class CommunityTrackPluginPublicControllerTest < ActionController::TestCase
100 100 assert_tag :tag => 'div', :attributes => {:id => 'errorExplanation'}
101 101 end
102 102  
  103 + should 'do not repeat tracks when paging a block with random order' do
  104 + @track.destroy
  105 + @block.order = 'random'
  106 + @block.save!
  107 +
  108 + per_page = 4
  109 + (per_page*3).times {|i| create_track("track_#{i}", @community) }
  110 +
  111 + tracks = 3.times.map do |i|
  112 + xhr :get, :view_tracks, :id => @block.id, :page => i+1, :per_page => per_page
  113 + assigns[:tracks].all
  114 + end.flatten
  115 + assert_equal tracks.count, tracks.uniq.count
  116 + end
  117 +
103 118 end
... ...
plugins/community_track/test/unit/community_track_plugin/track_list_block_test.rb
... ... @@ -9,7 +9,7 @@ class TrackListBlockTest < 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
... ... @@ -111,4 +111,20 @@ class TrackListBlockTest < ActiveSupport::TestCase
111 111 assert_equivalent [], @block.categories
112 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 130 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 _('Limit of items'), text_field(:block, :limit, :size => 3) %>
  3 +
  4 + <%= labelled_form_field _('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/>
... ...