Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Partial/extends question #58

Closed
gammons opened this Issue · 7 comments

5 participants

@gammons

Hi, I'm having a little trouble wrapping my head around how to use partials and extends correctly. I have a list of people, and I'd like to include pagination info with the JSON, so my frontend can tell what page I'm on, and get to the next/previous page of data.

my json.rabl file looks like this:

object false
child(@people => :entries) do 
  attributes :id, :first_name
end
node(:pagination) do
  {
    :url => @pagination_url,
    :current_page => @people.current_page,
    :per_page => @people.per_page,
    :total_entries => @people.total_entries
  }
end

I'd like to push the pagination section into a shared partial, and possibly call it like this:

object false
child(@people => :entries) do 
  attributes :id, :first_name
end
partial "shared/pagination", :object => @people, :url => @pagination_url

That doesn't work. I've been struggling to land at the correct syntax to do this.

..and then have the shared/pagination.json.rabl look like this:

node(:pagination) do
  {
    :url => url,
    :current_page => object.current_page,
    :per_page => object.per_page,
    :total_entries => object.total_entries
  }
end

I guess my question is, how do I get where I want to be using rabl? I'd be happy to add a wiki entry to further explain partials and extends.. that is, after I understand how they work. Thanks!

@gammons

I guess the root of my issue is that you cannot use partial unless you're inside of node or code. Why is that?

Workaround #1:

The following did not work.

# index.json.rabl
object false
child(@people => :entries) do
  ...
end

node(:pagination) do
  partial "shared/pagination", :object => @people
end

and the shared/pagination partial looks like this:

# shared/pagination.json.rabl
{:count => @people.count}
{"pagination":[{}],"entries":[{"entry":{...}}]}

Ok, so now onto workaround #2.

# index.json.rabl
object false
child(@people => :entries) do
  ...
end

partial "shared/pagination", :object => @people
# shared/pagination.json.rabl
node(:pagination) do
  {:count => @people.count}
end

The resulting JSON does not have a pagination node at all:

{"entries":[{"entry":{...}, ...}]}

The only way I have gotten this to work is by including the partial inside of a node.

# index.json.rabl
object false
child(@people => :entries) do
  ...
end

node(:pagination) do
  partial "shared/pagination", :object => @people
end
# shared/pagination.json.rabl
node(:pagination) do
  {:count => @people.count}
end

But, as you would expect, then I have 2 pagination nodes in the resulting JSON:

{"pagination":[{"pagination":{"count":1}}],"entries":[{"entry":{...},...}]}

So my question is, how can I use partials so that I will only have a single pagination node?

Thanks for your help!

@nesquena
Owner

I readily admit the "partial" business is confusing. It evolved out of my own need to render a hash inside of a "node" block. It only works in a node block because that's all I set it up to do at the time.

I feel like given

# shared/pagination.json.rabl
node(:pagination) do
  {:count => @people.count}
end

you should be able to achieve what you want with extends:

# index.json.rabl
object false

extends "shared/pagination"

child(@people => :entries) do
  ...
end

because extends does what you want, it "mixes in" the other template into the parent template and adds all the nodes and attributes. you can "extend" from multiple documents if needed as well. I would look at it very simply:

If you are inside a node and need a hash representation of a record, use partial to get that
In other cases or if you need to "mixin" behavior to a document, use extends to do that

Hope that helps a bit, this should all be clarified on the wiki, I haven't gotten around to that yet.

@gammons

Thanks for all of your help nesquena.

When I extend shared/pagination, I'm still not seeing my pagination node in the response however.

# index.json.rabl
object false
extends 'shared/pagination'
child(@people => :entries) do
  ...
end
# shared/pagination.json.rabl
node(:pagination) do
  {:this => "that"}
end

The response I see does not have the pagination node in it. Just the entries.

{"entries":[{"entry":{...},...}]}
@nesquena
Owner

I think I may have figured it out, if you look into the entries themselves, I suspect you will see the pagination node as part of each entry item. extends is for adding to the representation of an item, so I can see why this is not the behavior you are looking for either. You want to append additional items to the root (in the object false) case that are not part of the item represenation.

I think this is a legitimate limitation in the sharing system of RABL. In that to append to the root node, you need to build the nodes inside the template and it becomes hard to share code in this case. I can't think of a solution off hand for reusing the root nodes from another template except the sloppy:

# index.json.rabl
object false
child(@people => :entries) do
  ...
end

node(:pagination) do
  partial("shared/pagination", :object => @people)[:pagination]
  # simply traverses into the pagination hash to skip the duplicate node
end

I can think of a few ways to fix this (allow partials at root, allow you to send :root => false to partial as an option, etc) and I will leave this ticket open as a reminder to fix this when I can.

@mtrudel

I had a similar problem and ended up solving it using the ability of node to not take any arguments (causing anything defined in the block to be merged with the parent). My problem was slightly different. I have two models (I've substituted users and projects here, where users have many projects), and I want to embed the JSON of each users' project in the user's JSON:

  • app/views/users/show.json.rabl:

    object @user
    attributes :email, :name
    child :projects do
      node do |project|
        partial('project/_show', :object => project)
      end
    end
    
  • app/views/projects/show.json.rabl:

    object false
    node(:project) { partial('project/_show', :object => @project) }
    
  • app/views/projects/_show.json.rabl:

    attributes :name, :due_date, :notes
    

The general gist of this approach is to wrap the partial call in a node call with no parameter (other than the block) This will cause the partial to render directly into the scope containing the node. I think that should do what you want.

@nesquena nesquena closed this
@tilo

I have the same issue as @gammons . I need to call a shared/pagination partial from several different index views to keep things dry.

@thawatchai

having the same issue, then I've found this: http://metabates.com/2012/02/22/adding-pagination-to-an-api/. He suggests putting meta data in the headers instead. I think it's a cleaner solution for pagination.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.