Commit fb58f4619c937dc632d3e30c92cea59daf812e39

Authored by Victor Costa
2 parents 09ac1c70 6d06bc15

Merge branch 'track_order' into stable

* track_order:
  community_track: added order options for track list block
  noosfero-plugins: support out-of-tree plugins
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 attr_accessible :more_another_page, :category_ids
10 13  
... ... @@ -25,7 +28,18 @@ class CommunityTrackPlugin::TrackListBlock < Block
25 28 end
26 29  
27 30 def tracks(page=1, per_page=limit)
28   - all_tracks.order('hits DESC').paginate(:per_page => per_page, :page => page)
  31 + tracks = all_tracks
  32 + tracks = case order
  33 + when 'hits'
  34 + tracks.order('hits DESC')
  35 + when 'newer'
  36 + tracks.order('created_at DESC')
  37 + when 'random'
  38 + tracks.order('random()')
  39 + else
  40 + tracks
  41 + end
  42 + tracks.paginate(:per_page => per_page, :page => page)
29 43 end
30 44  
31 45 def count_tracks
... ... @@ -55,6 +69,7 @@ class CommunityTrackPlugin::TrackListBlock < Block
55 69 def content(args={})
56 70 block = self
57 71 proc do
  72 + instance_eval(&block.set_seed(true))
58 73 render :file => 'blocks/track_list', :locals => {:block => block}
59 74 end
60 75 end
... ... @@ -75,4 +90,26 @@ class CommunityTrackPlugin::TrackListBlock < Block
75 90 { :profile => [:article, :category], :environment => [:article, :category] }
76 91 end
77 92  
  93 + def timeout
  94 + 1.hour
  95 + end
  96 +
  97 + def set_seed(new_seed=false)
  98 + block = self
  99 + proc do
  100 + if block.order == 'random'
  101 + if new_seed || cookies[:_noosfero_community_tracks_rand_seed].blank?
  102 + cookies[:_noosfero_community_tracks_rand_seed] = {value: rand, expires: Time.now + 600}
  103 + end
  104 + #XXX postgresql specific
  105 + seed_val = environment.connection.quote(cookies[:_noosfero_community_tracks_rand_seed])
  106 + environment.connection.execute("select setseed(#{seed_val})")
  107 + end
  108 + end
  109 + end
  110 +
  111 + def self.order_options
  112 + {_('Hits') => 'hits', _('Random') => 'random', _('Most Recent') => 'newer'}
  113 + end
  114 +
78 115 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/>
... ...
script/noosfero-plugins
... ... @@ -79,7 +79,22 @@ run(){
79 79  
80 80 _enable(){
81 81 plugin="$1"
82   - source="$available_plugins_dir/$plugin"
  82 +
  83 + if [ -d "$available_plugins_dir/$plugin" ]; then
  84 + source="$available_plugins_dir/$plugin"
  85 + linksource="../../plugins/$plugin"
  86 + else
  87 + if [ ! -d "$plugin" ]; then
  88 + echo "E: $plugin not found (needs to be an existing directory)"
  89 + return
  90 + fi
  91 +
  92 + # out-of-tree plugins
  93 + source="$plugin"
  94 + linksource="$source"
  95 + plugin=$(basename "$plugin")
  96 + fi
  97 +
83 98 target="$enabled_plugins_dir/$plugin"
84 99 base="$base_plugins_dir/$plugin"
85 100 run "$source/before_enable.rb"
... ... @@ -110,7 +125,7 @@ _enable(){
110 125 fi
111 126 fi
112 127 if [ "$installation_ok" = true ] && [ "$dependencies_ok" = true ]; then
113   - ln -s "$source" "$target"
  128 + ln -s "$linksource" "$target"
114 129 plugins_public_dir="$NOOSFERO_DIR/public/plugins"
115 130 plugins_features_dir="$NOOSFERO_DIR/features/plugins"
116 131 test -d "$target/public" && ln -s "$target/public" "$plugins_public_dir/$plugin"
... ...