Coder Social home page Coder Social logo

Partial/extends question about rabl HOT 7 CLOSED

nesquena avatar nesquena commented on July 19, 2024
Partial/extends question

from rabl.

Comments (7)

gammons avatar gammons commented on July 19, 2024

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!

from rabl.

nesquena avatar nesquena commented on July 19, 2024

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.

from rabl.

gammons avatar gammons commented on July 19, 2024

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":{...},...}]}

from rabl.

nesquena avatar nesquena commented on July 19, 2024

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.

from rabl.

mtrudel avatar mtrudel commented on July 19, 2024

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.

from rabl.

tilo avatar tilo commented on July 19, 2024

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

from rabl.

thawatchai avatar thawatchai commented on July 19, 2024

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.

from rabl.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.