Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Collection representor not using property constraints of representer #77

Open
ianks opened this issue Sep 23, 2014 · 16 comments
Open

Collection representor not using property constraints of representer #77

ianks opened this issue Sep 23, 2014 · 16 comments

Comments

@ianks
Copy link

ianks commented Sep 23, 2014

After some digging trying to find the best way to respond_with a collection of landers... in my app I have:

module LanderRepresenter
  include Roar::Representer::JSON
  include Roar::Representer::Feature::Hypermedia

  property :name
  property :location

  link :self do
    v1_lander_url self
  end
end
module LanderCollectionRepresenter
  include Representable::JSON::Collection

  items extend: LanderRepresenter, class: Lander
end
module Api
  module V1
    class LandersController < ApplicationController
      include Roar::Rails::ControllerAdditions
      before_action :set_lander, only: [:show, :edit, :update, :destroy]
      respond_to :json

      def index
        @landers = Lander.all
        respond_with @landers, :represent_with => LanderCollectionRepresenter
      end

      def show
        respond_with @lander, :represent_with => LanderRepresenter
      end


      private

      def set_lander
        @lander = Lander.find params[:id]
      end
    end
  end
end

Is this the proper way to respond_with a collection? Rendering the LanderRepresenter works perfectly; however, when I render the collection, it spits out the entire object (without limiting the response to the property constraints on :name, :location, :link). i.e:

[{"id":1,"name":"Dr. Jeffery Skiles1","location":"http://schusterbeahan.com/eddie/1","default_offer_id":null,"created_at":"2014-09-22T20:06:26.296Z","updated_at":"2014-09-22T20:06:26.296Z"},{...},{...}]
@apotonick
Copy link
Member

That collection document you're getting is because your collection representer is not used at all, it's Rails #to_json (hooray to Rails' magic!).

It looks as if the represent_with: option is not picked up in your respond_with call.

Can you add this, please?

module LanderCollectionRepresenter
  include Representable::JSON::Collection

  def to_json
    raise "hey, i am called!"
  end
end

@ianks
Copy link
Author

ianks commented Sep 23, 2014

Looks like the error was indeed raised.

wrong number of arguments (1 for 0)

def create_response(model)
  render_method = "to_#{format}"
  media_format  = Mime[format]

  # stuck on this line
  Response.new(model.send(render_method, options), media_format)
end

@apotonick
Copy link
Member

That's great, so :represent_with works! 😄

Can you please convert LanderRepresenter into a decorator and see if that helps?

class LanderRepresenter < Roar::Decorator
  include Roar::Representer::JSON
  include Roar::Representer::Feature::Hypermedia

  property :name
  property :location

  link :self do
    v1_lander_url self
  end
end

@ianks
Copy link
Author

ianks commented Sep 23, 2014

Thanks for the quick response! Just tried to make it a decorator. Same issue.

I am on Rails 4.1.5 and Ruby 2.1.3 if that helps 😄

@apotonick
Copy link
Member

Is 2.1.3 already out? 😁 I'll have a look in a couple of hours, not sure what's going on.

@ianks
Copy link
Author

ianks commented Sep 23, 2014

@apotonick I forgot to mention, my Lander model is not namespaced in Api::V1 like my controller. Just thought I would let you know in case that could be a source of complication.

@apotonick
Copy link
Member

Hm, since you define it explicitely in :represent_with, it shouldn't matter where it sits.

What representable version? And what roar version?

@summera
Copy link

summera commented Sep 23, 2014

We are using

representable (1.8.5)
roar (0.12.9)

@ianks
Copy link
Author

ianks commented Sep 25, 2014

Update: confirmed this behavior on another Rails 4.1.5 app.

@summera
Copy link

summera commented Sep 30, 2014

@apotonick
Any word on this? @ianks and I would like to help to debug this issue.

@summera
Copy link

summera commented Sep 30, 2014

Turns out the issue disappeared when we namespaced our representers.

@ianks
Copy link
Author

ianks commented Sep 30, 2014

** namespaced our representers AND used the decorator class pattern.

@apotonick
Copy link
Member

Does the LandersRepresenter constant exist multiple times in your app, in different namespaces?

@ianks
Copy link
Author

ianks commented Oct 1, 2014

No it does not.

@Danielpk
Copy link

+1

I got same issue here.

@apotonick
Copy link
Member

I don't really know what to do here! This seems to be an autoloading/constant-referencing problem, and I still don't understand why Rails doesn't crash but uses the internal to_json instead.

Do we have any way to provoke that problem in a minimal Rails app?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants