Collecting all attachments

2 views
Skip to first unread message

Trevor Turk

unread,
Jan 13, 2009, 6:19:37 PM1/13/09
to Paperclip Plugin
I need to collect all of the files on the filesystem for an
attachment. For example, if a user has an avatar, I'd like to be able
to get the paths to all avatars in an array. I'm doing something like
this:

def avatars
avatars = []
styles = self.avatar.styles.keys << :original
styles.each do |style|
avatars << { :style => style, :path => self.avatar.path
(style), :url => self.avatar.url(style) }
end
avatars
end

I'm wondering if there's any easier way to accomplish this. I'm
thinking that if you have User.avatar, you could also have
User.avatars to get all of them.

Does something like this already exist? If not, I'd be happy to put
together a patch to accomplish something along these lines.

As an aside, I was surprised that "original" isn't included in the
styles list. I think it should be included, but perhaps there's a
reason it wasn't...?

Thanks,
- Trevor

Jonathan Yurek

unread,
Jan 13, 2009, 6:28:38 PM1/13/09
to papercli...@googlegroups.com
There isn't anything like User.avatars this at the moment, but if you
write up a patch with tests that sounds like something worth applying.

Original isn't included in the style list because it's not technically
a style. It should be if you actually put it into the styles
definition (to modify it).
--
Jonathan Yurek, Founder and CTO
thoughtbot, inc.
organic brains. digital solutions.

617.482.1300 x114
http://www.thoughtbot.com/

Trevor Turk

unread,
Jan 14, 2009, 12:06:42 AM1/14/09
to Paperclip Plugin
On Jan 13, 5:28 pm, Jonathan Yurek <jyu...@thoughtbot.com> wrote:
> There isn't anything like User.avatars this at the moment, but if you  
> write up a patch with tests that sounds like something worth applying.
>
> Original isn't included in the style list because it's not technically  
> a style. It should be if you actually put it into the styles  
> definition (to modify it).

OK, I've got some commits that are ready to pull:

http://github.com/trevorturk/paperclip/commits/master

I made something that would define User#avatars via a new
User#attachments_for method. I'm not sure if you'll agree with the
placement of this or the tests, but I'm happy to move things around if
you like. I did my best to find the appropriate spots.

I included the "original" in the hash that's returned by User#avatars.
This just has the :url and :path for each file, which is all I need
for my purposes. We could make this more fancy if you like. This is
good enough for me, though, and I didn't want to get too
imaginative :)

A couple of small things: I wasn't able to get all the tests around
PDFs passing. Is there something I need to do for those? In any case,
I made sure all of the relevant stuff passed, so you should be fine.
Also, I wasn't sure where to add documentation, but I'm happy to put
something together if you'd like to point me in the right direction.

Thanks again for this plugin. It's been a pleasure to use.

- Trevor

Jonathan Yurek

unread,
Jan 19, 2009, 7:52:18 PM1/19/09
to papercli...@googlegroups.com
I think I misunderstood you when you originally posted about this. I
expected the plural method to be on the class an return all the
attachments of all the instances, not just return the path and url of
all of one instance's styles. To be honest I'm not sure I see the
utility of this method, as any aggregation will be able to specify
names of styles. Sorry about that, I read you wrong originally.

Trevor Turk

unread,
Jan 19, 2009, 9:15:44 PM1/19/09
to Paperclip Plugin
On Jan 19, 6:52 pm, Jonathan Yurek <jyu...@thoughtbot.com> wrote:
> I think I misunderstood you when you originally posted about this. I  
> expected the plural method to be on the class an return all the  
> attachments of all the instances, not just return the path and url of  
> all of one instance's styles. To be honest I'm not sure I see the  
> utility of this method, as any aggregation will be able to specify  
> names of styles. Sorry about that, I read you wrong originally.

Hmm... Yeah, basically I just need a way to collect all of the actual
files (i.e. the styles) for shuffling them around later on. For
example, I'm allowing photo uploads to go to our local server first
(for speed) and then sending them up to S3 at a later time. So, I
wanted an easy way to collect all of the files (styles) that need to
be uploaded.

In short, my problem is that I saw no easy way to get the paths to all
of the attached files (styles). So, that's what I wanted this method
to do.

Is there an easy way to do this that I missed? Would you accept a
patch for this in another form? I can leave it as application-specific
code on my end, but I thought it would be useful for other people as
well.

Thanks,
- Trevor

Trevor Turk

unread,
Jan 31, 2009, 6:03:24 PM1/31/09
to Paperclip Plugin
On Jan 19, 8:15 pm, Trevor Turk <trevort...@gmail.com> wrote:
> Is there an easy way to do this that I missed? Would you accept a
> patch for this in another form? I can leave it as application-specific
> code on my end, but I thought it would be useful for other people as
> well.

I'm getting rid of my fork, but I did a little write-up about this in
case anyone is interested:

http://almosteffortless.com/2009/01/31/collecting-all-attachments-using-paperclip/

- Trevor
Reply all
Reply to author
Forward
0 new messages