Skip to content

Commit

Permalink
Fixed a bug where #501 broke compatibility with Enumerable (#531)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuki24 committed Feb 17, 2022
1 parent cac0dfe commit 63244d5
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
7 changes: 7 additions & 0 deletions lib/jbuilder/collection_renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,11 @@ def collection_with_template(view, template)
end
end
end

class EnumerableCompat < ::SimpleDelegator
# Rails 6.1 requires this.
def size(*args, &block)
__getobj__.count(*args, &block)
end
end
end
1 change: 1 addition & 0 deletions lib/jbuilder/jbuilder_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ def _render_partial_with_options(options)
collection = options.delete(:collection) || []
partial = options.delete(:partial)
options[:locals].merge!(json: self)
collection = EnumerableCompat.new(collection) if collection.respond_to?(:count) && !collection.respond_to?(:size)

if options.has_key?(:layout)
raise ::NotImplementedError, "The `:layout' option is not supported in collection rendering."
Expand Down
1 change: 0 additions & 1 deletion test/jbuilder_template_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,6 @@ class JbuilderTemplateTest < ActiveSupport::TestCase
test "works with an enumerable object" do
enumerable_class = Class.new do
include Enumerable
alias length count # Rails 6.1 requires this.

def each(&block)
[].each(&block)
Expand Down

0 comments on commit 63244d5

Please sign in to comment.