Skip to content

Instantly share code, notes, and snippets.

@plentz
Created March 15, 2011 20:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save plentz/871346 to your computer and use it in GitHub Desktop.
Save plentz/871346 to your computer and use it in GitHub Desktop.
rails don't call .to_xml on each element *always*

gurizei.

quando tu faz no rails "render :xml => @any_collection", ele chama o to_xml passando uma série de parâmetros no options, logo, se tu implementa teu to_xml assim:

    def to_xml(options = {})
        super(:include => [:children])
    end

tu não vai estar repassando pro super o "options" - que nesse caso vem com algo do tipo:

    {:indent=>2, :builder=>#<Builder::XmlMarkup:0x00000102eba2a0 @indent=2, @level=1, @target="<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<families type=\"array\">\n">, :root=>"family", :skip_instruct=>true}

logo, a forma "certa" de fazer aquilo seria algo como:

    def to_xml(options = {}, &block)
        options[:include] = [:children] unless options[:include]
        super
    end

"don't blame the tools", mais uma vez me pegou.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment