Permalink
Browse files

Removed all unnecessary &block variables since they cause serious mem…

…ory damage and lots of subsequent gc runs.
  • Loading branch information...
1 parent 62b9f6c commit 029964fe99a94c0c3f621df79f90a8eb36deac54 Darrick Wiebe committed Feb 9, 2009
@@ -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
@@ -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? })
@mattetti

mattetti Jan 21, 2011

Contributor

this code causes issues, it should probably read:

pager.replace [send(finder, *args) { |*a| block_given? ? yield(*a) : a }]

Otherwise the returned object sent to replace won't be an array and the super call in collection blows up.

# 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
@@ -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|
@@ -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
@@ -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

0 comments on commit 029964f

Please sign in to comment.