使用acts_as_tree的完全限定树的to_xml

发布于 2024-08-29 19:09:26 字数 1301 浏览 10 评论 0原文

我有一个使用acts_as_tree 的ActiveRecord 类。我正在尝试更新 to_xml 方法,以便如果调用子记录的 to_xml ,它将返回嵌套在父/祖先 xml 中的 xml,以提供该资源的完全限定路径。作为一个例子,我有编译器,它是编译器/版本的父级。编译器应将其呈现为 xml:

虽然编译器/版本应呈现为

我尝试通过传递 full_qualified 标志来做到这一点,但它因 'Builder::XmlMarkup#to_ary should return Array'

def to_xml(options={}, &block) 而终止 选项[:完全限定] ||= true 选项[:缩进] ||= 2 options[:builder] ||= Builder::XmlMarkup.new(:indent => options[:indent])

if options[:fully_qualified] and not parent.nil?

55:parent.to_xml(options) do |foo| 相对选项=选项.dup 相对选项[:完全限定] = false 相对选项[:跳过指令] = true relative_options.delete(:builder)

    foo << to_xml(relative_options)
  end
else
  xml = options[:builder]
  xml.instruct! unless options[:skip_instruct]

66: xml.parameter(:name => name, &block) 结尾 该

方法对于编译器的情况工作正常,但对于编译器/版本失败:

/usr/lib/ruby/1.8/builder/xmlbase.rb:133:in method_missing' /usr/lib/ruby/1.8/builder/xmlbase.rb:133:in调用' /usr/lib/ruby/1.8/builder/xmlbase.rb:133:in _nested_structs' /usr/lib/ruby/1.8/builder/xmlbase.rb:57:inmethod_missing' app/models/parameter.rb:66:in to_xml' app/models/parameter.rb:55:into_xml'

I have an ActiveRecord class that uses acts_as_tree. I'm trying to update the to_xml method so that if a child record's to_xml is called it will return it's xml nested in the parent/ancestor xml to give the fully-qualified path to that resource. As an example I have Compiler which is a parent of Compiler/Version. Compiler should be rendered as xml:

While Compiler/Version should render as

I've tried to do this by passing around a fully_qualified flag, but it dies with 'Builder::XmlMarkup#to_ary should return Array'

def to_xml(options={}, &block)
options[:fully_qualified] ||= true
options[:indent] ||= 2
options[:builder] ||= Builder::XmlMarkup.new(:indent => options[:indent])

if options[:fully_qualified] and not parent.nil?

55: parent.to_xml(options) do |foo|
relative_options = options.dup
relative_options[:fully_qualfied] = false
relative_options[:skip_instruct] = true
relative_options.delete(:builder)

    foo << to_xml(relative_options)
  end
else
  xml = options[:builder]
  xml.instruct! unless options[:skip_instruct]

66: xml.parameter(:name => name, &block)
end
end

The method works fine for the case of Compiler, but fails for Compiler/Version:

/usr/lib/ruby/1.8/builder/xmlbase.rb:133:in method_missing'
/usr/lib/ruby/1.8/builder/xmlbase.rb:133:in
call'
/usr/lib/ruby/1.8/builder/xmlbase.rb:133:in _nested_structures'
/usr/lib/ruby/1.8/builder/xmlbase.rb:57:in
method_missing'
app/models/parameter.rb:66:in to_xml'
app/models/parameter.rb:55:in
to_xml'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苏璃陌 2024-09-05 19:09:26

看来您无法在任何单一关联上调用 to_xml 。父级和参数上的 to_xml (has_one 关系)失败,但如果我使用 find_by_id 进行查找,它会起作用:

def to_xml(options={}, &block)
my_options = 选项.dup
my_options[:全限定] = true 除非 my_options.has_key?(:全限定)
my_options[:only] = [:name]

if my_options[:fully_qualified] and not parent.nil?
   # do block here fails with 'Builder::XmlMarkup#to_ary should return Array'
   # if called as parent.to_xml, so call on explicit lookup of parent and
   # it works
   p = self.class.find_by_id(parent_id)
   p.to_xml(my_options) do |xml|
     relative_options = my_options.dup
     relative_options[:builder] = xml
     relative_options[:fully_qualified] = false
     relative_options[:skip_instruct] = true

     to_xml(relative_options, &block)
   end
else
  super(my_options, &block)
end

结束

It appears that you can't call to_xml on any singular-association. to_xml on parent and parameter (both has_one relationships) failed, but if I did the lookup with find_by_id it worked:

def to_xml(options={}, &block)
my_options = options.dup
my_options[:fully_qualified] = true unless my_options.has_key?(:fully_qualified)
my_options[:only] = [:name]

if my_options[:fully_qualified] and not parent.nil?
   # do block here fails with 'Builder::XmlMarkup#to_ary should return Array'
   # if called as parent.to_xml, so call on explicit lookup of parent and
   # it works
   p = self.class.find_by_id(parent_id)
   p.to_xml(my_options) do |xml|
     relative_options = my_options.dup
     relative_options[:builder] = xml
     relative_options[:fully_qualified] = false
     relative_options[:skip_instruct] = true

     to_xml(relative_options, &block)
   end
else
  super(my_options, &block)
end

end

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文