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

[Question] Nesting 2 or more collections into 1 #121

Open
rpristopan opened this issue Oct 7, 2015 · 2 comments
Open

[Question] Nesting 2 or more collections into 1 #121

rpristopan opened this issue Oct 7, 2015 · 2 comments

Comments

@rpristopan
Copy link

I have the below Roar::Decorator and I want to join 2 collections into 1 to have object.images an array with all the original and suggested images from a json file. Is it possible? Right know the last definition of collection images will always override the collection.

class ObjectRepresenter < Roar::Decorator
  include Roar::JSON

  nested :images do
    collection :images, as: :originals, extend: Images::OriginalRepresenter, class: Image
    collection :images, as: :suggested, extend: Images::SuggestedRepresenter, class: Image
  end
end

JSON file:

{
 ...
 images: [
   originals: [
     { file_name: "foo1.jpg" },
     { file_name: "bar1.jpg" },
   ],
   suggested: [
     { file_name: "foo2.jpg" },
     { file_name: "bar2.jpg" },
   ]
 ]
...
}
@apotonick
Copy link
Member

Sorry, this is not possible, you need to map your data before you pass it into the representer. Remember, Representable/Roar is a document-transformation tool and not a data mapper (even though many people misuse it as this). 😜

The reason this doesn't work is that when parsing, Roar wouldn't know where to add the images, to originals or to suggested?

@rpristopan
Copy link
Author

@apotonick, yeah, that's what I was thinking.

I change the json file to have another format and I used only 1 collection for images with 1 representer.

{
 ...
 images: [
     { 
         file_name: "foo1.jpg", 
         type: "original"
     },
     { 
         file_name: "bar1.jpg",
         type: "original"
      },
     { 
         file_name: "foo2.jpg",
         type: "suggested"
      },
     { 
         file_name: "bar2.jpg",
         type: "suggested"
    },
 ]
...
}

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

2 participants