Embedding using a custom view

6 views
Skip to first unread message

andrea.s...@gmail.com

unread,
Jun 14, 2016, 5:24:47 AM6/14/16
to praxis-support
Given this resource media-type:

class User < Praxis::MediaType
attribute :cf, String
attribute :firstname, String
attribute :gender, String
attribute :id, Integer

view :default do
attribute :cf
attribute :firstname
attribute :gender
attribute :id
end

view :minimal do
attribute :id
attribute :firstname
end
end

It would be great to be able to define something like this:

class Activity < Praxis::MediaType
...
attribute :operators, Praxis::Collection.of(User), view: :minimal
end

That is, to exploit the power to the view filters to reduce / customize the output of embedding resources.

Is it possible (or there is at least another way to achieve the same goal)?

Thank you!

Josep Blanquer

unread,
Jun 14, 2016, 2:08:25 PM6/14/16
to Salicetti Andrea, praxis-support
Andrea,

 Yes you can totally do that, basically almost like you wrote it in your example above...but not quite.

So, instead of adding the "view: :minimal" in the attribute definition within the schema (i.e. inside the attributes do ... block), you need to define that in the view itself. That is because the schema just defines structure, not presentation.

So, in your example above, you can have a view in the Activity MediaType that says to render the :operators attribute using the :minimal view of the User. Something like this:

class Activity < Praxis::MediaType
  attributes do
       ...
       attribute :operators, Praxis::Collection.of(User)
   end

  view :myview do
     ...
     attribute :operators, view: :minimal
  end
end

This works the same for Collections or not. In this case I created a view named :myview but that could perfectly have been the :default one.

The docs talk about that a little bit here: http://praxis-framework.io/reference/media-types/ (look at the :compact view example)

Also, if you haven't looked at it yet, I'd highly recommend looking at the FieldSelector and Rendering set of helpers. It is a completely optional piece but if you enable it, it would allow you to use a very powerful functionality in which you can specify on a per-call basis which fields you would like the system to render (including nested objects). In a way, instead of specifying an already defined view, you can 'define' the view with the exact fields you need for any request support.
Those features were introduced in 0.19 (https://github.com/rightscale/praxis/blob/master/CHANGELOG.md#0190) and there is some info for the field selector here (http://praxis-framework.io/reference/extensions/field_selection/) but you can see it in action our praxis-example-app (for example: https://github.com/rightscale/praxis-example-app/blob/master/app/v1/controllers/posts.rb )

Cheers,

Josep M.


--
You received this message because you are subscribed to the Google Groups "praxis-support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to praxis-suppor...@googlegroups.com.
To post to this group, send email to praxis-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/praxis-support/63ce502b-c2df-4c9d-a40c-4217be9a4aad%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

andrea.s...@gmail.com

unread,
Jun 17, 2016, 5:27:25 AM6/17/16
to praxis-support, andrea.s...@gmail.com
I see... it worked: I'll keep in mind this important distinction.
Thank you Josep, clear and quick as always!
Reply all
Reply to author
Forward
0 new messages