From 029964fe99a94c0c3f621df79f90a8eb36deac54 Mon Sep 17 00:00:00 2001 From: Darrick Wiebe Date: Mon, 9 Feb 2009 15:04:14 -0500 Subject: [PATCH] Removed all unnecessary &block variables since they cause serious memory damage and lots of subsequent gc runs. --- lib/will_paginate/collection.rb | 2 +- lib/will_paginate/finder.rb | 18 +++++++++++------- lib/will_paginate/named_scope.rb | 12 ++++++------ lib/will_paginate/named_scope_patch.rb | 14 +++++++------- 4 files changed, 25 insertions(+), 21 deletions(-) diff --git a/lib/will_paginate/collection.rb b/lib/will_paginate/collection.rb index ac0153a1b..e25357050 100644 --- a/lib/will_paginate/collection.rb +++ b/lib/will_paginate/collection.rb @@ -82,7 +82,7 @@ def initialize(page, per_page, total = nil) # # The Array#paginate API has since then changed, but this still serves as a # fine example of WillPaginate::Collection usage. - def self.create(page, per_page, total = nil, &block) + def self.create(page, per_page, total = nil) pager = new(page, per_page, total) yield pager pager diff --git a/lib/will_paginate/finder.rb b/lib/will_paginate/finder.rb index 090bf568b..d0e5668c4 100644 --- a/lib/will_paginate/finder.rb +++ b/lib/will_paginate/finder.rb @@ -61,7 +61,7 @@ module ClassMethods # # All other options (+conditions+, +order+, ...) are forwarded to +find+ # and +count+ calls. - def paginate(*args, &block) + def paginate(*args) options = args.pop page, per_page, total_entries = wp_parse_options(options) finder = (options[:finder] || 'find').to_s @@ -79,7 +79,7 @@ def paginate(*args, &block) args << find_options # @options_from_last_find = nil - pager.replace send(finder, *args, &block) + pager.replace(send(finder, *args) { |*a| yield(*a) if block_given? }) # magic counting for user convenience: pager.total_entries = wp_count(count_options, args, finder) unless pager.total_entries @@ -96,7 +96,7 @@ def paginate(*args, &block) # # See {Faking Cursors in ActiveRecord}[http://weblog.jamisbuck.org/2007/4/6/faking-cursors-in-activerecord] # where Jamis Buck describes this and a more efficient way for MySQL. - def paginated_each(options = {}, &block) + def paginated_each(options = {}) options = { :order => 'id', :page => 1 }.merge options options[:page] = options[:page].to_i options[:total_entries] = 0 # skip the individual count queries @@ -106,7 +106,7 @@ def paginated_each(options = {}, &block) collection = paginate(options) with_exclusive_scope(:find => {}) do # using exclusive scope so that the block is yielded in scope-free context - total += collection.each(&block).size + total += collection.each { |item| yield item }.size end options[:page] += 1 end until collection.size < collection.per_page @@ -161,10 +161,14 @@ def respond_to?(method, include_priv = false) #:nodoc: protected - def method_missing_with_paginate(method, *args, &block) #:nodoc: + def method_missing_with_paginate(method, *args) #:nodoc: # did somebody tried to paginate? if not, let them be unless method.to_s.index('paginate') == 0 - return method_missing_without_paginate(method, *args, &block) + if block_given? + return method_missing_without_paginate(method, *args) { |*a| yield(*a) } + else + return method_missing_without_paginate(method, *args) + end end # paginate finders are really just find_* with limit and offset @@ -177,7 +181,7 @@ def method_missing_with_paginate(method, *args, &block) #:nodoc: options[:finder] = finder args << options - paginate(*args, &block) + paginate(*args) { |*a| yield(*a) if block_given? } end # Does the not-so-trivial job of finding out the total number of entries diff --git a/lib/will_paginate/named_scope.rb b/lib/will_paginate/named_scope.rb index 21fc168e4..5a743d7fb 100644 --- a/lib/will_paginate/named_scope.rb +++ b/lib/will_paginate/named_scope.rb @@ -83,7 +83,7 @@ def scopes # # expected_options = { :conditions => { :colored => 'red' } } # assert_equal expected_options, Shirt.colored('red').proxy_options - def named_scope(name, options = {}, &block) + def named_scope(name, options = {}) name = name.to_sym scopes[name] = lambda do |parent_scope, *args| Scope.new(parent_scope, case options @@ -91,7 +91,7 @@ def named_scope(name, options = {}, &block) options when Proc options.call(*args) - end, &block) + end) { |*a| yield(*a) if block_given? } end (class << self; self end).instance_eval do define_method name do |*args| @@ -112,9 +112,9 @@ class Scope delegate :scopes, :with_scope, :to => :proxy_scope - def initialize(proxy_scope, options, &block) + def initialize(proxy_scope, options) [options[:extend]].flatten.each { |extension| extend extension } if options[:extend] - extend Module.new(&block) if block_given? + extend Module.new { |*args| yield(*args) } if block_given? @proxy_scope, @proxy_options = proxy_scope, options.except(:extend) end @@ -152,12 +152,12 @@ def proxy_found end private - def method_missing(method, *args, &block) + def method_missing(method, *args) if scopes.include?(method) scopes[method].call(self, *args) else with_scope :find => proxy_options do - proxy_scope.send(method, *args, &block) + proxy_scope.send(method, *args) { |*a| yield(*a) if block_given? } end end end diff --git a/lib/will_paginate/named_scope_patch.rb b/lib/will_paginate/named_scope_patch.rb index 685bb5775..7daff5922 100644 --- a/lib/will_paginate/named_scope_patch.rb +++ b/lib/will_paginate/named_scope_patch.rb @@ -1,7 +1,7 @@ ActiveRecord::Associations::AssociationProxy.class_eval do protected - def with_scope(*args, &block) - @reflection.klass.send :with_scope, *args, &block + def with_scope(*args) + @reflection.klass.send(:with_scope, *args) { |*a| yield(*a) if block_given? } end end @@ -10,11 +10,11 @@ def with_scope(*args, &block) klass.class_eval do protected alias :method_missing_without_scopes :method_missing_without_paginate - def method_missing_without_paginate(method, *args, &block) + def method_missing_without_paginate(method, *args) if @reflection.klass.scopes.include?(method) - @reflection.klass.scopes[method].call(self, *args, &block) + @reflection.klass.scopes[method].call(self, *args) { |*a| yield(*a) if block_given? } else - method_missing_without_scopes(method, *args, &block) + method_missing_without_scopes(method, *args) { |*a| yield(*a) if block_given? } end end end @@ -23,14 +23,14 @@ def method_missing_without_paginate(method, *args, &block) # Rails 1.2.6 ActiveRecord::Associations::HasAndBelongsToManyAssociation.class_eval do protected - def method_missing(method, *args, &block) + def method_missing(method, *args) if @target.respond_to?(method) || (!@reflection.klass.respond_to?(method) && Class.respond_to?(method)) super elsif @reflection.klass.scopes.include?(method) @reflection.klass.scopes[method].call(self, *args) else @reflection.klass.with_scope(:find => { :conditions => @finder_sql, :joins => @join_sql, :readonly => false }) do - @reflection.klass.send(method, *args, &block) + @reflection.klass.send(method, *args) { |*a| yield(*a) if block_given? } end end end