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

How to set node root on mapped partial renders #466

Open
praveensharma opened this issue Jun 18, 2013 · 7 comments
Open

How to set node root on mapped partial renders #466

praveensharma opened this issue Jun 18, 2013 · 7 comments

Comments

@praveensharma
Copy link

Hi everyone! I have a questions about structuring my RABL JSON output.

I have the following snippet of RABL:

node :subviews do |obj|
  obj.subviews.map do |subview|
    partial 'kb_view/base', object: subview
  end
end

This outputs the following JSON:

"subviews": [
        {
            "frame": "{{1024, 1024}, {1024, 1024}}", 
            "key": "testView", 
            "subviews": [], 
            "type": "View"
        }, 
        {
            "frame": "{{1024, 1024}, {1024, 1024}}", 
            "key": "testView", 
            "subviews": [], 
            "type": "View"
        },...

I need to change this to the following output, using the "subview.key" property:

 "subviews": [
        "key1":{
            "frame": "{{1024, 1024}, {1024, 1024}}", 
            "key": "testView", 
            "subviews": [], 
            "type": "View"
        }, 
        "key2":{
            "frame": "{{1024, 1024}, {1024, 1024}}", 
            "key": "testView", 
            "subviews": [], 
            "type": "View"
        },...

Any ideas? In my partial i have: object :subview => :subview.key, :root => :subview.key

Which I thought should set the root of the partial to be the subview.key property.

@nesquena
Copy link
Owner

How about this:

node :subviews do |obj|
  obj.subviews.map do |subview|
    { subview.key => partial('kb_view/base', object: subview) }
  end
end

?

@praveensharma
Copy link
Author

Ah! Thanks for the quick response. This is extremely close - instead of an array of dictionaries though:

"subviews": [
        {
            "testView1": {
                "frame": "{{1024, 1024}, {1024, 1024}}", 
                "key": "testView", 
                "subviews": [], 
                "type": "View"
            }
        },

How would I create a dictionary of dictionaries? Is this even possible?

{
"subviews": {
    "subview": {
       "frame": "{{1024, 1024}, {1024, 1024}}", 
        "key": "testView",
        "subviews": [],
        "type": "View"
    },
    "subview2": {
        "frame": "{{1024, 1024}, {1024, 1024}}", 
        "key": "testView",
        "subviews": [],
        "type": "View"
    },...
  }
}

Sorry for the multiple somewhat lame questions - theres an extremely specific output structure i've been struggling creating using RABL and I don't have much flexibility in doing it differently.

@nesquena
Copy link
Owner

Definitely possible with each_with_object:

node :subviews do |obj|
  obj.subviews.each_with_object do |subview, result|
    result[subview.key] = partial('kb_view/base', object: subview)
  end
end

See how close that gets you. I don't mind the questions, I readily admit rabl works much better with "standard-ish" responses and it can get a little tricky with these super custom ones. But almost anything is do-able.

@praveensharma
Copy link
Author

Ah that makes sense!

Currently, your snippet is causing a "wrong number of arguments" error on my base RABL class. Might be due to some incorrect extending of RABL templates. Looking into it - I'll let you know if this works.

Thanks again.

@nesquena
Copy link
Owner

Ok I think that should get you pretty close. Let me know if you get it working. 

Nathan Esquenazi
CodePath Co-founder
http://thecodepath.com

On Tue, Jun 18, 2013 at 12:25 PM, Praveen Sharma notifications@github.com
wrote:

Ah that makes sense!
Currently, your snippet is causing a "wrong number of arguments" error on my base RABL class. Might be due to some incorrect extending of RABL templates. Looking into it - I'll let you know if this works.

Thanks again.

Reply to this email directly or view it on GitHub:
#466 (comment)

@praveensharma
Copy link
Author

Thanks for all your help Nesquena - the final solution was:

node(:subviews) do |object|
  object.subviews.each_with_object({}) do |subview, result|
    result[subview.key] = partial('kb_view/base', :object => subview)
  end 
end 

The only issue was missing the ({}) at the end of each_with_object

@nesquena
Copy link
Owner

Ah yeah right, I aircoded it glad you found that missing piece

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