# Attachment\_fu, deferred thumbnail li

**URL:** <https://www.ruby-forum.com/t/attachment-fu-deferred-thumbnail-li/157071>\
**Category:** Rails\
**Created:** [January 7, 2009, 3:28am UTC](https://www.ruby-forum.com/t/attachment-fu-deferred-thumbnail-li/157071 "2009-01-07T03:28:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![eblatt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7bcc69/32.png) [@eblatt](https://www.ruby-forum.com/u/eblatt)\
**Post date:** [January 7, 2009, 3:28am UTC](https://www.ruby-forum.com/t/attachment-fu-deferred-thumbnail-li/157071/1 "2009-01-07T03:28:03Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![eblatt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7bcc69/32.png) [@eblatt](https://www.ruby-forum.com/u/eblatt)\
**Post date:** [January 7, 2009, 1:34pm UTC](https://www.ruby-forum.com/t/attachment-fu-deferred-thumbnail-li/157071/2 "2009-01-07T13:34:01Z")

</div>

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](http://railswheels.com)

---

<div class="post-metadata">

**Author:** ![eblatt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7bcc69/32.png) [@eblatt](https://www.ruby-forum.com/u/eblatt)\
**Post date:** [January 7, 2009, 2:55pm UTC](https://www.ruby-forum.com/t/attachment-fu-deferred-thumbnail-li/157071/3 "2009-01-07T14:55:20Z")

</div>

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](http://railswheels.com)
