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

Configurations for json output #4

Closed
nesquena opened this issue Apr 12, 2011 · 17 comments
Closed

Configurations for json output #4

nesquena opened this issue Apr 12, 2011 · 17 comments
Labels

Comments

@nesquena
Copy link
Owner

On a project and template basis, there needs to be configuration. Namely,

  • Include a 'root' node at the base of the collection users : { ... }
  • Include a 'root' node at the base of the object: { 'user' : { ... } }

And probably others I can't think of right now.

@nesquena
Copy link
Owner Author

Right now you can do:

object @user => :person
collection @users => :people

and it will add a root node "people" BUT there will still be a "person" label inside for every person i.e:

{  "people" : [  { "person" : { age : 22 }  } ] }

That is how I prefer it, but I can see the need for the other way:

 {  "people" : [ { age : 22 } ] }

Currently that way isn't possible.

@jacquescrocker
Copy link

The defaults you have in there are good. If someone want's to override them, I really think this should be explicit in the rabl template. There shouldn't be any implicit setting that changes the defaults project wide.

I'd suggest the syntax

collection @users => :people, :shallow => true

Thanks!

@nesquena
Copy link
Owner Author

You are right after I thought about it a bit more. That syntax is probably a good way to do it. Will leave that for another day since I never need it in my apis. Thanks for your help with identifying these issues.

@jacquescrocker
Copy link

cool. it's not a blocking issue for me so no rush. if i have any spare time i'll maybe use this as an excuse to try to understand the rabl codebase and hopefully contribute a patch

@nesquena
Copy link
Owner Author

That'd be appreciated, thanks

@ognevsky
Copy link
Contributor

I wish this issue will be closed as soon as possible.
I've started develop an app using js-model and it requires include_root_in_json to be in false ;(

Thanks for this awesome gem, you are my hero now :)

@ognevsky
Copy link
Contributor

If somebody is looking for a quick dirty hack for this — I can suggest replace true with false here https://github.com/nesquena/rabl/blob/master/lib/rabl/engine.rb#L37

@nesquena
Copy link
Owner Author

Glad you like the gem so far, this is low priority for me since I don't need it for any of my projects. But I am happy to pull in any patches that address this.

@nesquena
Copy link
Owner Author

Bit of an update for this with a new option "child_root" which can be passed into the builder that affects this behavior. Essentially, we just need to close the loop by deciding on a syntax and exposing this functionality per-template. "child_root" here: 7065793

@nesquena
Copy link
Owner Author

nesquena commented May 9, 2011

Merging this ticket here:

He had:

[{"person":{"id":1,"firstname":"Zaphod","lastname":"BEEBLEBROX","gender":"Mr"}},{"person":{"id":2,"firstname":"Ford","lastname":"PREFECT","gender":"Mr"}}]

But wanted:

[{"id":1,"firstname":"Zaphod","lastname":"BEEBLEBROX","gender":"Mr"},{"id":2,"firstname":"Ford","lastname":"PREFECT","gender":"Mr"}]

Had have builder template index.json.rabl for People#index controller with

attributes :id, :firstname, :lastname, :gender

and the collection of objects => @people

@nesquena
Copy link
Owner Author

Ok this has been completed and I have added the concept of rabl global settings configuration.

@railsjedi @ognevsky @joel Check the README for more details on configuration. Can you guys all try this from the repo directly. If it works without issue, I will push a new gem version. Commits here and here

Configuration with:

# config/initializers/rabl_init.rb
Rabl.configure do |config|
  config.include_json_root = false
end

@ognevsky
Copy link
Contributor

Awesome! Yahoo!
Thanks

@nesquena
Copy link
Owner Author

Let me know if it works as expected. Thanks.

@joel
Copy link

joel commented May 12, 2011

this is very good but this does not cover all cases, we can not define the behavior for each rendering, the proposal of railsjedi seems to necessary.

@nesquena
Copy link
Owner Author

At least a step in the right direction, and opens up the possibility for future configuration options at a global level.

@joel
Copy link

joel commented May 12, 2011

exactly! and it's great!

@joel
Copy link

joel commented May 25, 2011

I tried to work with ActiveResource:: Base and json format. To activate self.format =: json you must remove the root! So thank you for adding this feature! If you don't use an builder just add self.include_root_in_json=false in ActiveRecord:: Base Model called.

kidpollo pushed a commit to satisfaction/rabl that referenced this issue Sep 27, 2013
Don't choke on symbol attribute values.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants