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/models/page.rb | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) (limited to 'app/models') 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", "") -- cgit v1.3