nesquena / rabl

General ruby templating with json, bson, xml, plist and msgpack support
http://blog.codepath.com/2011/06/27/building-a-platform-api-on-rails/
MIT License
3.64k stars 333 forks source link

upgrading from 0.14.0 to 0.14.1 is a breaking change #724

Closed swiknaba closed 4 years ago

swiknaba commented 5 years ago

Having the following setup in an older app running on ruby 2.4.6 and rails 4.2.11.1

# index.json.rabl
node :foo do
  partial('foos/list', object: @foos)
end
# list.json.rabl
collection @foos, root: false, object_root: false
extends('foos/show')
# show.json.rabl
object @foo => nil
attributes :id, :name
# foos_controller.rb
def index
  @foos = Foo.all
  render 'foos/index'
end

With Rabl 0.14.0 this renders properly:

{
    "foos": [
        { "id": 1, "name": "foo one"},
        { "id": 2, "name": "foo two"},
    ]
}

Upgrading to 0.14.1 it just renders nothing, no error raised:

{
    "foos": {}
}

I am not very familiar with Rabl, to be honest, are the views here just basically wrong and working by chance in this setup, or did we find a breaking change that slipped in a minor version bump?

nesquena commented 5 years ago

I’ll look into this soon, sorry to hear about that. Once I can confirm, I’ll yank 0.14.1 until this is resolved

swiknaba commented 5 years ago

Thanks! I created a sample app to reproduce the issue: https://github.com/swiknaba/rabl_0140

dmke commented 5 years ago

Same problem here.

We're presenting an ActiveRecord::Relation object in our templates:

# controller
@tags = Tag.where(...)

# template
object @tags
attributes :id, :name, :position

The workaround for this is converting the collection to an array (but that's obviously bad for performance):

object @tags.to_a
dramalho commented 4 years ago

Yeah, I just bitten by this, locking the gem to 0.14.0 until either it's fixed or, presumably, I move to Rails > 4.2 and this is no longer an issue

tagliala commented 4 years ago

Confirm this issue.

Problem is in commit: 4cf2c667a8f05eb5e04abf371c0978f6604574ab

exactly this change: https://github.com/nesquena/rabl/commit/4cf2c667a8f05eb5e04abf371c0978f6604574ab#diff-5a71730235e40573daff6c167a927d5dR85-R89

because on Rails 4 an active record relation responds to both :map and :each but it is not an enumerable

[2] pry(main)> Document.all.respond_to?(:map)
=> true
[3] pry(main)> Document.all.respond_to?(:each)
=> true
[4] pry(main)> Document.all.is_a?(Enumerable)
=> false

Hope it helps

nesquena commented 4 years ago

Sorry to hear about this, I am unfortunately no longer able to actively maintain RABL (which I first created many years ago now) as you probably gathered, though I still happily use an old version on several of my own apps. It's honestly amazing to me that it's still used as much as it is!

If anyone can point me to a PR, and a recommended course of action, I'd be more than happy to push a 0.14.2 to resolve this

nesquena commented 4 years ago

@tagliala Sent me a PR, I merged and released the fix as 0.14.3! I've also marked you as a contributor @tagliala if you can help keep RABL alive and well in whatever capacity you can, I appreciate it. That goes for anyone else reading this thread too.

Sorry that I haven't been able to maintain this much in the last few years, it is genuinely humbling that people still use this library, and sorry it isn't even better! I do still use this library myself extensively, I just have it locked to old versions of Sinatra and haven't needed any new features in a long time.

tagliala commented 4 years ago

@nesquena apologies are not necessary. Thank you for all these years, I've been using rabl since forever 🙏🏼