Mike Zornek on 30 Mar 2007 06:17:05 -0000


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

[PhillyOnRails] XML Builder question


So I have a Rails project where we track lots of objects. Let's say one of
them is Town and when I'm viewing a town object I need the ability to
generate an xml configuration for this town. I have all the basics working,
this is a Builder question.

The XML I want is something like:

<town name="Philadelphia">
  <people>
    <policemen>
      <policeman name="bill"/>
      <policeman name="steve"/>
      <policeman name="gus"/>
    </policemen>
    <firemen>
      <fireman name="gus"/>
      <fireman name="al"/>
      <fireman name="buzz"/>
    </firemen>
  </people>
</town>

One way I could do it in Builder...

xml.town(:name => @town.name) do
  xml.people do
    xml.policemen do
      Person.find(:all).each do |person|
        unless person.is_policeman? xml.policeman(:name => person.name)
      end
    end
    xml.firemen do
      Person.find(:all).each do |person|
        unless person.is_fireman? xml.fireman(:name => person.name)
      end
    end
  end
end

First I'll point out I need to go through the person list for each job cause
a person might have more than one job.

But the bigger issue is that the list of jobs in this town can and will
change. It is dynamic but I'm too inexperienced with builder on how I would
implement a dynamic job list here. In particular I'm really lost as to how I
would start a do block with a dynamic method name.

Imagine their is an array called jobs = ['policeman', 'fireman', etc.] ..
how do i write a builder template to accommodate that?

Thanks for your help.

~ Mike
-- 
Clickable Bliss, makers of:

Billable: Simple Service and Invoice Tracking
http://clickablebliss.com/billable

_______________________________________________
To unsubscribe or change your settings, visit:
http://lists.phillyonrails.org/mailman/listinfo/talk