From b8717e4fe005a8df13a4983943184881f87d97be Mon Sep 17 00:00:00 2001 From: hukl Date: Sat, 15 Aug 2009 22:29:57 +0200 Subject: added pagination for aggregators. costed me the whole day. crappy plugins ARGH! More work to do! --- app/helpers/content_helper.rb | 4 ++- app/models/page.rb | 30 +++++++++++++++------- .../public/no_date_and_author.html.erb | 4 ++- .../public/standard_template.html.erb | 4 ++- 4 files changed, 30 insertions(+), 12 deletions(-) diff --git a/app/helpers/content_helper.rb b/app/helpers/content_helper.rb index 5f6143b..6309c8b 100644 --- a/app/helpers/content_helper.rb +++ b/app/helpers/content_helper.rb @@ -77,9 +77,11 @@ module ContentHelper # Takes the parameters from the aggregate? method and renders the collection # from Page.aggregate(options) with a given partial def render_collection options + @content_collection = Page.aggregate(options, params[:page]) + render( :partial => options[:partial], - :collection => Page.aggregate(options), + :collection => @content_collection, :as => :page ) end diff --git a/app/models/page.rb b/app/models/page.rb index 0a978ab..ef92d4e 100644 --- a/app/models/page.rb +++ b/app/models/page.rb @@ -45,8 +45,8 @@ class Page < ActiveRecord::Base # partially or entirely overwritten by the options hash. Afterwards the merged # parameters are used to query the DB for Pages matching these parameters. # The aggregation only takes published pages into account. - def self.aggregate options - + def self.aggregate options, page=1 + defaults = { :tags => "", :limit => 20, @@ -56,17 +56,29 @@ class Page < ActiveRecord::Base options = defaults.merge options - pages = Page.find_all_tagged_with( - options[:tags].gsub(/\s/, ", "), - :match_all => true, - :order => "#{options[:order_by]} #{options[:order_direction]}", - :include => [:node, :globalize_translations], - :conditions => ["nodes.head_id = pages.id"], - :limit => options[:limit] + pages = Page.heads.paged_find_tagged_with( + options[:tags].gsub(/\s/, ", "), + :page => page, + :per_page => 23, + :include => [:globalize_translations], + :order => "#{options[:order_by]} #{options[:order_direction]}" ) end + def self.paged_find_tagged_with(tags, args = {}) + if tags.blank? + paginate args + else + options = find_options_for_find_tagged_with(tags, :match_all => true) + options.merge!(args) + # FIXME The select {} looks really bad !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + # The default count query generated by paginate includes COUNT(DISTINCT Posts.*) which errors, at least on mysql + # Below we override the default select statement used to perform the count so that it becomes COUNT(DISTINCT Posts.id) + paginate(options.merge(:count => { :select => options[:select].gsub('*', 'id') })) + end + end + def self.custom_templates files = Dir.entries(FULL_PUBLIC_TEMPLATE_PATH).select do |x| x if x.gsub!(".html.erb", "") diff --git a/app/views/custom/page_templates/public/no_date_and_author.html.erb b/app/views/custom/page_templates/public/no_date_and_author.html.erb index 4eda744..254fe45 100644 --- a/app/views/custom/page_templates/public/no_date_and_author.html.erb +++ b/app/views/custom/page_templates/public/no_date_and_author.html.erb @@ -3,4 +3,6 @@

<%= @page.abstract %>

<%= aggregate?(@page.body) %> - \ No newline at end of file + + +

<%= will_paginate(@content_collection) if @content_collection %>

\ No newline at end of file diff --git a/app/views/custom/page_templates/public/standard_template.html.erb b/app/views/custom/page_templates/public/standard_template.html.erb index 4c21667..1822491 100644 --- a/app/views/custom/page_templates/public/standard_template.html.erb +++ b/app/views/custom/page_templates/public/standard_template.html.erb @@ -4,4 +4,6 @@

<%= @page.abstract %>

<%= aggregate?(@page.body) %> - \ No newline at end of file + + +

<%= will_paginate(@content_collection) if @content_collection %>

\ No newline at end of file -- cgit v1.3