Attachment_fu, deferred thumbnail li


#1

Hi,

I’m using attachment_fu with great success, but have run into a
requirement that I’d like to solve elegantly – but I’ve outstripped my
ruby skills.
I have a class that returns a hash of thumbnail definitions, like so:
class Thumb
def self.get_thumbs(style)
return { :small => “100x50”, :large => “200x100” } if style
== ‘landscape’
return { :small => “50x100”, :large => “100x200” } if style
== ‘portrait’
end
end

My attachment model is defined like this:

class Photo < ActiveRecord::Base
     has_attachment :content_type => :image,
         :thumbnails => Thumb.get_thumbs(??????)
   [...more stuff ...]
end

My photos controller has the following line:

 @photo = Photo.new(params[:photo])

My question:

I’d like dynamically determine what value to set to Thumbnail.get_thumbs
based on values set in the Photo.new constructor. Is this possible?
I’ve fooled around with attr_accessors, lambdas, etc, but, I can’t seem
to do what I want. Is it possible to make such a run-time decision as
I’m trying to make?

Which is to say, if params[:photo][:style] = ‘portrait’, i’d like to cut
the thumbnails accordingly?

Does anyone have an elegant solution for this?


#2

Elliott B. wrote:

      return { :small => "50x100",  :large  => "100x200" }  if style
end

to do what I want. Is it possible to make such a run-time decision as
I’m trying to make?

Which is to say, if params[:photo][:style] = ‘portrait’, i’d like to cut
the thumbnails accordingly?

Does anyone have an elegant solution for this?

A non-thread-safe solution may be:

after_save ‘attachment_options[:thumbnails] = Thumb.get_thumbs(style)’
has_attachment :content_type => :image,
:thumbnails => Thumb.get_thumbs(‘landscape’)

but a better solution may be to add PortraitPhoto and LandscapePhoto
sub-classes.


Rails Wheels - Find Plugins, List & Sell Plugins -
http://railswheels.com


#3

Mark,

Wow, not one but two possible solutions. Thanks a lot. I think I like
the sub-classing solution.

Thank you very much,
Elliott

Mark Reginald J. wrote:

Elliott B. wrote:

A non-thread-safe solution may be:

after_save ‘attachment_options[:thumbnails] = Thumb.get_thumbs(style)’
has_attachment :content_type => :image,
:thumbnails => Thumb.get_thumbs(‘landscape’)

but a better solution may be to add PortraitPhoto and LandscapePhoto
sub-classes.


Rails Wheels - Find Plugins, List & Sell Plugins -
http://railswheels.com