Forum: Ruby on Rails attachment_fu, deferred thumbnail li

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
419c9cc258895bed66c89892721c2971?d=identicon&s=25 Elliott Blatt (eblatt)
on 2009-01-07 03:28
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?
C64e63b70be7dfed8b0742540b8b27e5?d=identicon&s=25 Mark Reginald James (Guest)
on 2009-01-07 13:34
(Received via mailing list)
Elliott Blatt 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
419c9cc258895bed66c89892721c2971?d=identicon&s=25 Elliott Blatt (eblatt)
on 2009-01-07 14:55
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 James wrote:
> Elliott Blatt 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
This topic is locked and can not be replied to.