Skip to content

Commit 63244d5

Browse files
authored
Fixed a bug where #501 broke compatibility with Enumerable (#531)
1 parent cac0dfe commit 63244d5

File tree

3 files changed

+8
-1
lines changed

3 files changed

+8
-1
lines changed

lib/jbuilder/collection_renderer.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,11 @@ def collection_with_template(view, template)
106106
end
107107
end
108108
end
109+
110+
class EnumerableCompat < ::SimpleDelegator
111+
# Rails 6.1 requires this.
112+
def size(*args, &block)
113+
__getobj__.count(*args, &block)
114+
end
115+
end
109116
end

lib/jbuilder/jbuilder_template.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ def _render_partial_with_options(options)
145145
collection = options.delete(:collection) || []
146146
partial = options.delete(:partial)
147147
options[:locals].merge!(json: self)
148+
collection = EnumerableCompat.new(collection) if collection.respond_to?(:count) && !collection.respond_to?(:size)
148149

149150
if options.has_key?(:layout)
150151
raise ::NotImplementedError, "The `:layout' option is not supported in collection rendering."

test/jbuilder_template_test.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,6 @@ class JbuilderTemplateTest < ActiveSupport::TestCase
294294
test "works with an enumerable object" do
295295
enumerable_class = Class.new do
296296
include Enumerable
297-
alias length count # Rails 6.1 requires this.
298297

299298
def each(&block)
300299
[].each(&block)

0 commit comments

Comments
 (0)