Commit a03dc433dc0c45d84130ed108298b3e02dc8fd36
Committed by
Antonio Terceiro
1 parent
27667670
Exists in
master
and in
23 other branches
ActionItem990: Added pagination and cache to friends
Showing
7 changed files
with
53 additions
and
4 deletions
Show diff stats
app/controllers/public/profile_controller.rb
| @@ -28,7 +28,9 @@ class ProfileController < PublicController | @@ -28,7 +28,9 @@ class ProfileController < PublicController | ||
| 28 | end | 28 | end |
| 29 | 29 | ||
| 30 | def friends | 30 | def friends |
| 31 | - @friends= profile.friends | 31 | + if is_cache_expired?(profile.friends_cache_key(params)) |
| 32 | + @friends = profile.friends.paginate(:per_page => per_page, :page => params[:npage]) | ||
| 33 | + end | ||
| 32 | end | 34 | end |
| 33 | 35 | ||
| 34 | def members | 36 | def members |
| @@ -82,4 +84,7 @@ class ProfileController < PublicController | @@ -82,4 +84,7 @@ class ProfileController < PublicController | ||
| 82 | end | 84 | end |
| 83 | end | 85 | end |
| 84 | 86 | ||
| 87 | + def per_page | ||
| 88 | + 10 | ||
| 89 | + end | ||
| 85 | end | 90 | end |
app/helpers/profile_helper.rb
| @@ -12,4 +12,9 @@ module ProfileHelper | @@ -12,4 +12,9 @@ module ProfileHelper | ||
| 12 | end | 12 | end |
| 13 | end | 13 | end |
| 14 | 14 | ||
| 15 | + def pagination_links(collection, options={}) | ||
| 16 | + options = {:prev_label => '« ' + _('Previous'), :next_label => _('Next') + ' »'}.merge(options) | ||
| 17 | + will_paginate(collection, options) | ||
| 18 | + end | ||
| 19 | + | ||
| 15 | end | 20 | end |
app/models/person.rb
| @@ -38,7 +38,7 @@ class Person < Profile | @@ -38,7 +38,7 @@ class Person < Profile | ||
| 38 | end | 38 | end |
| 39 | 39 | ||
| 40 | def remove_friend(friend) | 40 | def remove_friend(friend) |
| 41 | - friends.delete(friend) | 41 | + Friendship.find(:first, :conditions => {:friend_id => friend, :person_id => id}).destroy |
| 42 | end | 42 | end |
| 43 | 43 | ||
| 44 | FIELDS = %w[ | 44 | FIELDS = %w[ |
| @@ -238,4 +238,26 @@ class Person < Profile | @@ -238,4 +238,26 @@ class Person < Profile | ||
| 238 | refused_communities << community | 238 | refused_communities << community |
| 239 | end | 239 | end |
| 240 | 240 | ||
| 241 | + def blocks_to_expire_cache | ||
| 242 | + [CommunitiesBlock] | ||
| 243 | + end | ||
| 244 | + | ||
| 245 | + def cache_keys(params = {}) | ||
| 246 | + [communities_cache_key] | ||
| 247 | + end | ||
| 248 | + | ||
| 249 | + def communities_cache_key(params = {}) | ||
| 250 | + page = params[:npage] || '1' | ||
| 251 | + identifier + '-communities-page-' + page | ||
| 252 | + end | ||
| 253 | + | ||
| 254 | + def friends_cache_key(params = {}) | ||
| 255 | + page = params[:npage] || '1' | ||
| 256 | + identifier + '-friends-page-' + page | ||
| 257 | + end | ||
| 258 | + | ||
| 259 | + def manage_friends_cache_key(params = {}) | ||
| 260 | + page = params[:npage] || '1' | ||
| 261 | + identifier + '-manage-friends-page-' + page | ||
| 262 | + end | ||
| 241 | end | 263 | end |
app/models/profile.rb
| @@ -606,4 +606,12 @@ class Profile < ActiveRecord::Base | @@ -606,4 +606,12 @@ class Profile < ActiveRecord::Base | ||
| 606 | self.articles.find(:all, :conditions => ['type in (?)', ['Folder', 'Blog']]) | 606 | self.articles.find(:all, :conditions => ['type in (?)', ['Folder', 'Blog']]) |
| 607 | end | 607 | end |
| 608 | 608 | ||
| 609 | + def blocks_to_expire_cache | ||
| 610 | + [] | ||
| 611 | + end | ||
| 612 | + | ||
| 613 | + def cache_keys(params = {}) | ||
| 614 | + [] | ||
| 615 | + end | ||
| 616 | + | ||
| 609 | end | 617 | end |
app/views/profile/friends.rhtml
| @@ -3,12 +3,16 @@ | @@ -3,12 +3,16 @@ | ||
| 3 | 3 | ||
| 4 | <h1><%= __("%s's friends") % profile.name %></h1> | 4 | <h1><%= __("%s's friends") % profile.name %></h1> |
| 5 | 5 | ||
| 6 | -<% cache do%> | 6 | +<% cache_timeout(profile.friends_cache_key(params), 4.hours.from_now) do %> |
| 7 | <ul class='profile-list'> | 7 | <ul class='profile-list'> |
| 8 | <% @friends.each do |friend| %> | 8 | <% @friends.each do |friend| %> |
| 9 | <%= profile_image_link(friend) %> | 9 | <%= profile_image_link(friend) %> |
| 10 | <% end%> | 10 | <% end%> |
| 11 | </ul> | 11 | </ul> |
| 12 | + | ||
| 13 | + <div id='pagination-profiles'> | ||
| 14 | + <%= pagination_links @friends, :param_name => 'npage' %> | ||
| 15 | + </div> | ||
| 12 | <% end %> | 16 | <% end %> |
| 13 | 17 | ||
| 14 | <% button_bar do %> | 18 | <% button_bar do %> |
config/environment.rb
| @@ -67,7 +67,7 @@ Rails::Initializer.run do |config| | @@ -67,7 +67,7 @@ Rails::Initializer.run do |config| | ||
| 67 | 67 | ||
| 68 | # don't load the sweepers while loading the database | 68 | # don't load the sweepers while loading the database |
| 69 | unless $PROGRAM_NAME =~ /rake$/ && ARGV.first == 'db:schema:load' | 69 | unless $PROGRAM_NAME =~ /rake$/ && ARGV.first == 'db:schema:load' |
| 70 | - config.active_record.observers = :article_sweeper | 70 | + config.active_record.observers = :article_sweeper, :role_assignment_sweeper, :friendship_sweeper |
| 71 | end | 71 | end |
| 72 | # Make Active Record use UTC-base instead of local time | 72 | # Make Active Record use UTC-base instead of local time |
| 73 | # config.active_record.default_timezone = :utc | 73 | # config.active_record.default_timezone = :utc |
public/stylesheets/controller_profile.css
| 1 | @import url(profile-list-block.css); | 1 | @import url(profile-list-block.css); |
| 2 | +@import url(pagination.css); | ||
| 2 | 3 | ||
| 3 | .button-bar { | 4 | .button-bar { |
| 4 | padding-top: 40px; | 5 | padding-top: 40px; |
| 5 | clear: both; | 6 | clear: both; |
| 6 | } | 7 | } |
| 8 | + | ||
| 9 | +#pagination-profiles .pagination span { | ||
| 10 | + display: inline; | ||
| 11 | +} |