product_image(@product), definition?

2,348 views
Skip to first unread message

Chad Pierce

unread,
Aug 11, 2011, 12:57:55 AM8/11/11
to Spree
product_image(@product) is called in the _image.html.erb partial.
However, I am unable to find the actual method definition. Can anyone
point me in the direction of where this is? I would like to make my
second image show first in product#show. I am using Rails 3.1 and
Spree 0.70.99.

Chad Pierce

unread,
Aug 11, 2011, 3:18:45 PM8/11/11
to Spree
I hate to bump this, but I have continued searching and still cannot
find where this is declared. I know I have to be missing something
here. Any help at all would be greatly appreciated!

Adam Jahn

unread,
Aug 11, 2011, 3:38:43 PM8/11/11
to spree...@googlegroups.com
I think this method is declared on the fly via some meta programming. I think it is in app/helpers/spree/base_helper.rb around line 45.

Search for: define_method "#{style}_image"

> --
> You received this message because you are subscribed to the Google Groups "Spree" group.
> To post to this group, send email to spree...@googlegroups.com.
> To unsubscribe from this group, send email to spree-user+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/spree-user?hl=en.
>

BW

unread,
Aug 11, 2011, 7:00:07 PM8/11/11
to Spree
Yeah I was curious about this too. I couldn't find the def for it. I'm
thinking it's something dynamic?

Anyways my solution was to do this (which may not be the most ideal):

@product.images[0].attachment.url(:original)

Paperclip has it's own way of grabbing attachments which is what that
is doing.. but Spree may have another way.

Damon Aw

unread,
Apr 12, 2012, 8:01:31 AM4/12/12
to spree...@googlegroups.com
hi guys, 

I was googling for this too and couldn't find any answer. 

Here's what I've learned after poking around. 

The helper method product_image is defined in 

/spree/core/app/helpers/spree/base_helper.rb
Spree::Image.attachment_definitions[:attachment][:styles].each do |style, v|
      define_method "#{style}_image" do |product, *options|
        options = options.first || {}
        if product.images.empty?
          image_tag "noimage/#{style}.png", options
        else
          image = product.images.first
          options.reverse_merge! :alt => image.alt.blank? ? product.name : image.alt
          image_tag image.attachment.url(style), options
        end
      end
    end

If you look at the define_method call, it means that there is a 'on-the-fly' method definition according to the styles you defined in the Spree::Image Model (or the decorator). 

You end up having specific methods like <%=product_image%>, <%=large_image %> that gives you back images of those sizes. I think a method named product_image tends to be misleading because, hehe, on first look, I thought that's a helper that displays the images of a product and I have to somehow supply parameter for the size version that I want. 

Ryan Bigg

unread,
Apr 12, 2012, 9:33:55 AM4/12/12
to spree...@googlegroups.com
Correct, this is where it's defined.
--
You received this message because you are subscribed to the Google Groups "Spree" group.
To view this discussion on the web visit https://groups.google.com/d/msg/spree-user/-/5GDJ7sDm9MQJ.
Reply all
Reply to author
Forward
0 new messages