In that case I'd probably have it iterate and make sure it responds_to? :to_csv
Maybe you'll have other objects you'd like to dump later.
If you'd like an example of something like this take a look at both
the assertion and helper model adn the assertions and helpers
controllers in http://thar.be/svn/projects/assertions/
ex.
def show
@user = User.find(params[:user_id])
@helper = @user.helpers.find(params[:id])
respond_to do |wants|
wants.html
wants.rb { send_data @helper.to_ruby, :type => 'text/plain',
:disposition => 'inline' }
end
end
On 8/29/06, Chris Abad <[EMAIL PROTECTED]> wrote:
Wow... just my luck. The one day I'm out is the day a thread I
started completely runs away without me. =) First of all, thanks for
all the great feedback both relevant to my situation and not (though
equally interesting).
I guess this is my fault for not giving more context as to what I'm
using these for... so here it is. I'm writing a plugin to add a
to_csv method to a collection of AR objects.... I have a couple
different projects where I've need to export as CSV and I was
inspired by the simplicity of being able to do something like this:
wants.xml => { render :xml => @users.to_xml }
so with my plugin, my goal is to do something like this:
wants.csv => { render :text => @users.to_csv }
I have something right now that works... I just think it can be
cleaner and a little more elegant as far as how I'm doing it. Because
of the nature of the CSV format, it really only makes sense for me to
represent a single AR class with each object in a new row. Here's the
basic idea:
1. Make sure I have an array of AR objects and they are all of the
same AR class.
2. Grab the first one in the array and pull its attribute_names to
use those as the header row
3. Walk through each of the items in the array, creating a new row
each time.
to_xml extends AR::Base, as well as Array and Hash (through special
rails modules). I really only have a use for extending Array right
now, and am simply doing so by adding the method to the same module
that to_xml uses.
So far, I think Tom's suggestion is the most relevant to this
situation, and is also prettier than the way I am currently doing
this. Hopefully this helped clear some things up. If anyone else
feels they have any further suggestions based on this new info, feel
free to let me know.
I actually have one more question regarding attribute_names, but I'll
start a new thread rather than hijacking this one. =)
_______________________________________________
Sdruby mailing list
[email protected]
http://lists.sdruby.com/mailman/listinfo/sdruby
--
Kevin Clark
http://glu.ttono.us
_______________________________________________
Sdruby mailing list
[email protected]
http://lists.sdruby.com/mailman/listinfo/sdruby