Commit fb58f4619c937dc632d3e30c92cea59daf812e39
Exists in
theme-brasil-digital-from-staging
and in
9 other branches
Merge branch 'track_order' into stable
* track_order: community_track: added order options for track list block noosfero-plugins: support out-of-tree plugins
Showing
6 changed files
with
96 additions
and
6 deletions
Show diff stats
plugins/community_track/controllers/public/community_track_plugin_public_controller.rb
| @@ -7,14 +7,15 @@ class CommunityTrackPluginPublicController < PublicController | @@ -7,14 +7,15 @@ class CommunityTrackPluginPublicController < PublicController | ||
| 7 | 7 | ||
| 8 | def view_tracks | 8 | def view_tracks |
| 9 | block = Block.find(params[:id]) | 9 | block = Block.find(params[:id]) |
| 10 | + instance_eval(&block.set_seed) | ||
| 10 | p = params[:page].to_i | 11 | p = params[:page].to_i |
| 11 | per_page = params[:per_page] | 12 | per_page = params[:per_page] |
| 12 | per_page ||= block.limit | 13 | per_page ||= block.limit |
| 13 | per_page = per_page.to_i | 14 | per_page = per_page.to_i |
| 14 | - tracks = block.tracks(p, per_page) | 15 | + @tracks = block.tracks(p, per_page) |
| 15 | 16 | ||
| 16 | render :update do |page| | 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 | if block.has_page?(p+1, per_page) | 20 | if block.has_page?(p+1, per_page) |
| 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} | 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,6 +28,7 @@ class CommunityTrackPluginPublicController < PublicController | ||
| 27 | def all_tracks | 28 | def all_tracks |
| 28 | @per_page = 8 #FIXME | 29 | @per_page = 8 #FIXME |
| 29 | @block = Block.find(params[:id]) | 30 | @block = Block.find(params[:id]) |
| 31 | + instance_eval(&@block.set_seed) | ||
| 30 | @tracks = @block.tracks(1, @per_page) | 32 | @tracks = @block.tracks(1, @per_page) |
| 31 | @show_more = @block.has_page?(2, @per_page) | 33 | @show_more = @block.has_page?(2, @per_page) |
| 32 | end | 34 | end |
plugins/community_track/lib/community_track_plugin/track_list_block.rb
| @@ -5,6 +5,9 @@ class CommunityTrackPlugin::TrackListBlock < Block | @@ -5,6 +5,9 @@ class CommunityTrackPlugin::TrackListBlock < Block | ||
| 5 | settings_items :limit, :type => :integer, :default => 3 | 5 | settings_items :limit, :type => :integer, :default => 3 |
| 6 | settings_items :more_another_page, :type => :boolean, :default => false | 6 | settings_items :more_another_page, :type => :boolean, :default => false |
| 7 | settings_items :category_ids, :type => Array, :default => [] | 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 | attr_accessible :more_another_page, :category_ids | 12 | attr_accessible :more_another_page, :category_ids |
| 10 | 13 | ||
| @@ -25,7 +28,18 @@ class CommunityTrackPlugin::TrackListBlock < Block | @@ -25,7 +28,18 @@ class CommunityTrackPlugin::TrackListBlock < Block | ||
| 25 | end | 28 | end |
| 26 | 29 | ||
| 27 | def tracks(page=1, per_page=limit) | 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 | end | 43 | end |
| 30 | 44 | ||
| 31 | def count_tracks | 45 | def count_tracks |
| @@ -55,6 +69,7 @@ class CommunityTrackPlugin::TrackListBlock < Block | @@ -55,6 +69,7 @@ class CommunityTrackPlugin::TrackListBlock < Block | ||
| 55 | def content(args={}) | 69 | def content(args={}) |
| 56 | block = self | 70 | block = self |
| 57 | proc do | 71 | proc do |
| 72 | + instance_eval(&block.set_seed(true)) | ||
| 58 | render :file => 'blocks/track_list', :locals => {:block => block} | 73 | render :file => 'blocks/track_list', :locals => {:block => block} |
| 59 | end | 74 | end |
| 60 | end | 75 | end |
| @@ -75,4 +90,26 @@ class CommunityTrackPlugin::TrackListBlock < Block | @@ -75,4 +90,26 @@ class CommunityTrackPlugin::TrackListBlock < Block | ||
| 75 | { :profile => [:article, :category], :environment => [:article, :category] } | 90 | { :profile => [:article, :category], :environment => [:article, :category] } |
| 76 | end | 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 | end | 115 | end |
plugins/community_track/test/functional/community_track_plugin_public_controller_test.rb
| @@ -100,4 +100,19 @@ class CommunityTrackPluginPublicControllerTest < ActionController::TestCase | @@ -100,4 +100,19 @@ class CommunityTrackPluginPublicControllerTest < ActionController::TestCase | ||
| 100 | assert_tag :tag => 'div', :attributes => {:id => 'errorExplanation'} | 100 | assert_tag :tag => 'div', :attributes => {:id => 'errorExplanation'} |
| 101 | end | 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 | end | 118 | end |
plugins/community_track/test/unit/community_track_plugin/track_list_block_test.rb
| @@ -9,7 +9,7 @@ class TrackListBlockTest < ActiveSupport::TestCase | @@ -9,7 +9,7 @@ class TrackListBlockTest < ActiveSupport::TestCase | ||
| 9 | @block = create(CommunityTrackPlugin::TrackListBlock, :box => box) | 9 | @block = create(CommunityTrackPlugin::TrackListBlock, :box => box) |
| 10 | end | 10 | end |
| 11 | 11 | ||
| 12 | - attr_reader :profile | 12 | + attr_reader :profile, :track |
| 13 | 13 | ||
| 14 | should 'describe yourself' do | 14 | should 'describe yourself' do |
| 15 | assert CommunityTrackPlugin::TrackListBlock.description | 15 | assert CommunityTrackPlugin::TrackListBlock.description |
| @@ -111,4 +111,20 @@ class TrackListBlockTest < ActiveSupport::TestCase | @@ -111,4 +111,20 @@ class TrackListBlockTest < ActiveSupport::TestCase | ||
| 111 | assert_equivalent [], @block.categories | 111 | assert_equivalent [], @block.categories |
| 112 | end | 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 | end | 130 | end |
plugins/community_track/views/box_organizer/community_track_plugin/_track_list_block.html.erb
| 1 | <div id='edit-track-list-block'> | 1 | <div id='edit-track-list-block'> |
| 2 | <%= labelled_form_field _('Limit of items'), text_field(:block, :limit, :size => 3) %> | 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 | <%= labelled_form_field check_box(:block, :more_another_page) + _('Show more at another page'), '' %> | 8 | <%= labelled_form_field check_box(:block, :more_another_page) + _('Show more at another page'), '' %> |
| 4 | <%= select_categories(:block, _('Select Categories')) %> | 9 | <%= select_categories(:block, _('Select Categories')) %> |
| 5 | <br/> | 10 | <br/> |
script/noosfero-plugins
| @@ -79,7 +79,22 @@ run(){ | @@ -79,7 +79,22 @@ run(){ | ||
| 79 | 79 | ||
| 80 | _enable(){ | 80 | _enable(){ |
| 81 | plugin="$1" | 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 | target="$enabled_plugins_dir/$plugin" | 98 | target="$enabled_plugins_dir/$plugin" |
| 84 | base="$base_plugins_dir/$plugin" | 99 | base="$base_plugins_dir/$plugin" |
| 85 | run "$source/before_enable.rb" | 100 | run "$source/before_enable.rb" |
| @@ -110,7 +125,7 @@ _enable(){ | @@ -110,7 +125,7 @@ _enable(){ | ||
| 110 | fi | 125 | fi |
| 111 | fi | 126 | fi |
| 112 | if [ "$installation_ok" = true ] && [ "$dependencies_ok" = true ]; then | 127 | if [ "$installation_ok" = true ] && [ "$dependencies_ok" = true ]; then |
| 113 | - ln -s "$source" "$target" | 128 | + ln -s "$linksource" "$target" |
| 114 | plugins_public_dir="$NOOSFERO_DIR/public/plugins" | 129 | plugins_public_dir="$NOOSFERO_DIR/public/plugins" |
| 115 | plugins_features_dir="$NOOSFERO_DIR/features/plugins" | 130 | plugins_features_dir="$NOOSFERO_DIR/features/plugins" |
| 116 | test -d "$target/public" && ln -s "$target/public" "$plugins_public_dir/$plugin" | 131 | test -d "$target/public" && ln -s "$target/public" "$plugins_public_dir/$plugin" |