From a03dc433dc0c45d84130ed108298b3e02dc8fd36 Mon Sep 17 00:00:00 2001 From: Daniela Soares Feitosa Date: Sat, 23 May 2009 18:24:48 -0300 Subject: [PATCH] ActionItem990: Added pagination and cache to friends --- app/controllers/public/profile_controller.rb | 7 ++++++- app/helpers/profile_helper.rb | 5 +++++ app/models/person.rb | 24 +++++++++++++++++++++++- app/models/profile.rb | 8 ++++++++ app/views/profile/friends.rhtml | 6 +++++- config/environment.rb | 2 +- public/stylesheets/controller_profile.css | 5 +++++ 7 files changed, 53 insertions(+), 4 deletions(-) diff --git a/app/controllers/public/profile_controller.rb b/app/controllers/public/profile_controller.rb index 9a89675..5501e0d 100644 --- a/app/controllers/public/profile_controller.rb +++ b/app/controllers/public/profile_controller.rb @@ -28,7 +28,9 @@ class ProfileController < PublicController end def friends - @friends= profile.friends + if is_cache_expired?(profile.friends_cache_key(params)) + @friends = profile.friends.paginate(:per_page => per_page, :page => params[:npage]) + end end def members @@ -82,4 +84,7 @@ class ProfileController < PublicController end end + def per_page + 10 + end end diff --git a/app/helpers/profile_helper.rb b/app/helpers/profile_helper.rb index f1fe755..6b00a53 100644 --- a/app/helpers/profile_helper.rb +++ b/app/helpers/profile_helper.rb @@ -12,4 +12,9 @@ module ProfileHelper end end + def pagination_links(collection, options={}) + options = {:prev_label => '« ' + _('Previous'), :next_label => _('Next') + ' »'}.merge(options) + will_paginate(collection, options) + end + end diff --git a/app/models/person.rb b/app/models/person.rb index ac9e617..df0b484 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -38,7 +38,7 @@ class Person < Profile end def remove_friend(friend) - friends.delete(friend) + Friendship.find(:first, :conditions => {:friend_id => friend, :person_id => id}).destroy end FIELDS = %w[ @@ -238,4 +238,26 @@ class Person < Profile refused_communities << community end + def blocks_to_expire_cache + [CommunitiesBlock] + end + + def cache_keys(params = {}) + [communities_cache_key] + end + + def communities_cache_key(params = {}) + page = params[:npage] || '1' + identifier + '-communities-page-' + page + end + + def friends_cache_key(params = {}) + page = params[:npage] || '1' + identifier + '-friends-page-' + page + end + + def manage_friends_cache_key(params = {}) + page = params[:npage] || '1' + identifier + '-manage-friends-page-' + page + end end diff --git a/app/models/profile.rb b/app/models/profile.rb index 85dfbd2..9da0e24 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -606,4 +606,12 @@ class Profile < ActiveRecord::Base self.articles.find(:all, :conditions => ['type in (?)', ['Folder', 'Blog']]) end + def blocks_to_expire_cache + [] + end + + def cache_keys(params = {}) + [] + end + end diff --git a/app/views/profile/friends.rhtml b/app/views/profile/friends.rhtml index a515ea2..b0dbc66 100644 --- a/app/views/profile/friends.rhtml +++ b/app/views/profile/friends.rhtml @@ -3,12 +3,16 @@

<%= __("%s's friends") % profile.name %>

-<% cache do%> +<% cache_timeout(profile.friends_cache_key(params), 4.hours.from_now) do %> + +
+ <%= pagination_links @friends, :param_name => 'npage' %> +
<% end %> <% button_bar do %> diff --git a/config/environment.rb b/config/environment.rb index c30f881..f2167bf 100644 --- a/config/environment.rb +++ b/config/environment.rb @@ -67,7 +67,7 @@ Rails::Initializer.run do |config| # don't load the sweepers while loading the database unless $PROGRAM_NAME =~ /rake$/ && ARGV.first == 'db:schema:load' - config.active_record.observers = :article_sweeper + config.active_record.observers = :article_sweeper, :role_assignment_sweeper, :friendship_sweeper end # Make Active Record use UTC-base instead of local time # config.active_record.default_timezone = :utc diff --git a/public/stylesheets/controller_profile.css b/public/stylesheets/controller_profile.css index 2c51f11..8843504 100644 --- a/public/stylesheets/controller_profile.css +++ b/public/stylesheets/controller_profile.css @@ -1,6 +1,11 @@ @import url(profile-list-block.css); +@import url(pagination.css); .button-bar { padding-top: 40px; clear: both; } + +#pagination-profiles .pagination span { + display: inline; +} -- libgit2 0.21.2