Commit a03dc433dc0c45d84130ed108298b3e02dc8fd36
Committed by
Antonio Terceiro
1 parent
27667670
Exists in
master
and in
29 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 | 28 | end |
29 | 29 | |
30 | 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 | 34 | end |
33 | 35 | |
34 | 36 | def members |
... | ... | @@ -82,4 +84,7 @@ class ProfileController < PublicController |
82 | 84 | end |
83 | 85 | end |
84 | 86 | |
87 | + def per_page | |
88 | + 10 | |
89 | + end | |
85 | 90 | end | ... | ... |
app/helpers/profile_helper.rb
... | ... | @@ -12,4 +12,9 @@ module ProfileHelper |
12 | 12 | end |
13 | 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 | 20 | end | ... | ... |
app/models/person.rb
... | ... | @@ -38,7 +38,7 @@ class Person < Profile |
38 | 38 | end |
39 | 39 | |
40 | 40 | def remove_friend(friend) |
41 | - friends.delete(friend) | |
41 | + Friendship.find(:first, :conditions => {:friend_id => friend, :person_id => id}).destroy | |
42 | 42 | end |
43 | 43 | |
44 | 44 | FIELDS = %w[ |
... | ... | @@ -238,4 +238,26 @@ class Person < Profile |
238 | 238 | refused_communities << community |
239 | 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 | 263 | end | ... | ... |
app/models/profile.rb
... | ... | @@ -606,4 +606,12 @@ class Profile < ActiveRecord::Base |
606 | 606 | self.articles.find(:all, :conditions => ['type in (?)', ['Folder', 'Blog']]) |
607 | 607 | end |
608 | 608 | |
609 | + def blocks_to_expire_cache | |
610 | + [] | |
611 | + end | |
612 | + | |
613 | + def cache_keys(params = {}) | |
614 | + [] | |
615 | + end | |
616 | + | |
609 | 617 | end | ... | ... |
app/views/profile/friends.rhtml
... | ... | @@ -3,12 +3,16 @@ |
3 | 3 | |
4 | 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 | 7 | <ul class='profile-list'> |
8 | 8 | <% @friends.each do |friend| %> |
9 | 9 | <%= profile_image_link(friend) %> |
10 | 10 | <% end%> |
11 | 11 | </ul> |
12 | + | |
13 | + <div id='pagination-profiles'> | |
14 | + <%= pagination_links @friends, :param_name => 'npage' %> | |
15 | + </div> | |
12 | 16 | <% end %> |
13 | 17 | |
14 | 18 | <% button_bar do %> | ... | ... |
config/environment.rb
... | ... | @@ -67,7 +67,7 @@ Rails::Initializer.run do |config| |
67 | 67 | |
68 | 68 | # don't load the sweepers while loading the database |
69 | 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 | 71 | end |
72 | 72 | # Make Active Record use UTC-base instead of local time |
73 | 73 | # config.active_record.default_timezone = :utc | ... | ... |