Skip to content

Commit

Permalink
Removed all unnecessary &block variables since they cause serious mem…
Browse files Browse the repository at this point in the history
…ory damage and lots of subsequent gc runs.
  • Loading branch information
Darrick Wiebe committed Feb 9, 2009
1 parent 62b9f6c commit 029964f
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 21 deletions.
2 changes: 1 addition & 1 deletion lib/will_paginate/collection.rb
Expand Up @@ -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
Expand Down
18 changes: 11 additions & 7 deletions lib/will_paginate/finder.rb
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
12 changes: 6 additions & 6 deletions lib/will_paginate/named_scope.rb
Expand Up @@ -83,15 +83,15 @@ 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
when Hash
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|
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down
14 changes: 7 additions & 7 deletions 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

Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 029964f

Please sign in to comment.