← Back Home

Grape Roar Presenter Issues

  1. ruby
  2. grape-roar
  3. representable
  4. roar
  5. presenter

grape-roar is an amazing Ruby gem that makes it easy to parse and render REST API documents using Presenters in Grape. While upgrading from a pretty old version, it broke one of the tests. Took me quite a bit to figure out what actually was going wrong so putting this out for easier recall.

The breakdown was something like this. We had a presenter class:

1
2
3
4
5
6
7
class SongPresenter < Grape::Roar::Decorator
    include Representable::Hash
    include Representable::Hash::AllowSymbols

    property :title
    property :artist
end

We were calling it in this manner:

1
2
3
4
5
6
7
desc 'create a song' do
...
end
post :create_song do
    song_params = params.merge record_created_by: logged_in_user
    song_representation = SongPresenter.new(song_params)
end

After the upgrade to the latest grape-roar, representable and roar versions, this broke the POST API with the following error:

NoMethodError: undefined method `title` for <ActiveSupport::HashWithIndifferentAccess:0x...>

which looks like a very weird error. After racking my brains for quite some time, it occurred to me that in the documentation for all these gems they’ve always specified a class as the input to the SongPresenter.new constructor.

I changed the line

1
2
3
4
5
6
7
desc 'create a song' do
...
end
post :create_song do
    song_params = params.merge record_created_by: logged_in_user
    song_representation = SongPresenter.new(song_params)
end
to
1
2
3
4
5
6
7
desc 'create a song' do
...
end
post :create_song do
    song_params = OpenStruct.new(params.merge record_created_by: logged_in_user)
    song_representation = SongPresenter.new(song_params)
end
and it started working again. There is an open issue on the grape-roar repo since July 2015 that talks about the exact same problem!!! Might be a good candidate for some PRs… 😎🤓

At the same time, it looks like there hasn’t been any activity on the grape-roar repo in a while, so might be a good idea to migrate away from that dependency to avoid the same pains in the future.