Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customize Node Names in Ruby's Builder

I'm building a tool that generates dynamic xml. As a result my Models have pretty generic names:

Project 
  has_many    :Groups

Group
  has_many    :Items
  has_many    :Groups
  belongs_to  :Project

Item
  has_many    :Params
  belongs_to  :Group

Param
  belongs_to  :Project
  belongs_to  :Group
  belongs_to  :Item

So when I build the xml from the project controller, the project node name is the root node of the xml. But I don't want it called "project". I want the node to be whatever the @project.params['name'] value is.

The problem I'm having is that the structure of builder is making this difficult... When I do:

xml.project do
  ~some code
end

...It's always going to create "project" as the root node name. I can't find a way to override it to use a different name. I was hoping something like the following would work:

xml.send(@project.params.name) {
  ...some code
}

..but that obviously isn't working. So I'm essentially trying to find a way to alias the element names that are configured in my params model. Any suggestions would be greatly appreciated.

like image 661
Robert DiNicolas Avatar asked Dec 05 '25 10:12

Robert DiNicolas


1 Answers

Instead of doing xml.project, try:

xml.tag! @project.params.name do

That should also be used if there is a hyphen in the element name.

like image 174
Jimmy Baker Avatar answered Dec 06 '25 23:12

Jimmy Baker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!