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

Suggestion for dsl #3

Open
jeroenvandijk opened this issue Jul 29, 2011 · 4 comments
Open

Suggestion for dsl #3

jeroenvandijk opened this issue Jul 29, 2011 · 4 comments

Comments

@jeroenvandijk
Copy link

I've created this hack for hypertemplate: https://gist.github.com/1113898

It allows for having something like the following:

notifications {
  members(:collection => @notifications, :root => false) do |notification|
    id           notification.id
    title        notification.title
  end
}

This will return something like the following

{ "notifications":
  [
    {
      "id": 2,
      "title": "some title",
    }
  ]
}

Would je be willing to add something like this?

@guilhermesilveira
Copy link
Member

Of course! Is it backwards compatible?
The patch for json is also welcome!

On Fri, Jul 29, 2011 at 11:25 AM, jeroenvandijk <
[email protected]>wrote:

I've created this hack for hypertemplate: https://gist.github.com/1113898

It allows for having something like the following:

notifications {
members(:collection => @notifications, :root => false) do
|notification|
id notification.id
title notification.title
end
}

This will return something like the following

{ "notifications":
[
{
"id": 2,
"title": "some title",
}
]
}

Would je be willing to add something like this?

Reply to this email directly or view it on GitHub:
#3

@kshahkshah
Copy link

Interesting, glad I read this first.

I had an issue, perhaps I was using the normal syntax improperly.

parent_model_name {
  id parent_model.id
  name parent_model.name

  @child_models.each do |child|
    child_model_name {
      id      child.id
      attr1   child.attr1
    }
  end
}

was producing JSON like this:

{ "child_model_name":
  [
    {
      "id": 1,
      "attr1": "some attr 1",
    },
    {
      "id": 2,
      "attr1": "another attr 1",
    }
  ]
}

Which was as expected. But if there was only one "child model" I would receive this (JSON)

{ "child_model_name":
  {
    "id": 1,
    "attr1": "some attr 1",
  }
}

Not as a JSON array. I was expecting it to return me an Array regardless if it only had one element. This of course works fine in XML.

Switching my hypertemplate syntax to:

id parent_model.id
name parent_model.name

members(:collection => @child_models, :root => 'child_model_name') do |child|
  id      child.id
  attr1   child.attr1
end

Seemed to address the issue, properly wrapping the single child in an array.

@kshahkshah
Copy link

I excluded the parent model wrapper stuff in my late code samples.

@kshahkshah
Copy link

I think this is my misunderstanding, what I needed could be accomplished regardless of the hack, I believe.

I just needed to use the members call regardless, though I was under the impression that 'members' is a deprecated method. If that is true, then how would you get this behavior? AR:Collection?

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

3 participants