Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Organizing partials for a polymorphic resource

I'm looking for how others typically organize their partials for a polymorphic resource.

Example:

I have Image which is polymorphic and depending on what is imageable, I want to display things slightly different.

I have a partial images/_image and can call render imageable.images. My current mindset is to have my image partial check what type imageable is and then have another partial, specific to that case. My organization would be something along the lines of:

images/
  _image.html.haml
  _product.html.haml
  _post.html.haml
  _user.html.haml

My _image partial would look something like:

%div
  = render :partial => "images/#{imageable.type}"

Does this seem like a bad approach, or flat out the wrong approach? I think it would be much nicer to just call render imageable.images from anywhere than having to call render :partial => ... all over the place.

Any ideas would be greatly appreciated. How have you done it?

EDIT: A long time has gone by and I'm still wondering if anyone has any input on this. Throwing up a bounty to see if that draws some attention.

like image 304
theIV Avatar asked Aug 06 '10 16:08

theIV


4 Answers

In your code now it's unclear that _product actually is an _image.

I think the intention in Rails is to do it like this:

shared/
    _image.html.haml
    imageable/
      _product.html.haml
      _post.html.haml
      _user.html.haml

Then in the _image partial you call:

render :partial => imageable/#{image.type}", :image => image

Now it is clear from your directory structure what your polymorphism is.

(note: I used the shared directory, but ofcourse when your image partial isn't actually shared between views it should be in some view directory like if you actually have an images_controller the directory should be called images)

like image 99
Tinco Avatar answered Nov 12 '22 09:11

Tinco


I think you've got the right approach, but i think the final step for cleanliness is to eliminate the images partial that selects another partial by instead passign that behavior to a helper. something like...

module ImagesHelper
  def show_image(imageable)
    render :partial => "images/#{imageable.class.to_s.tableize}"
  end  
end

?

like image 26
MissingHandle Avatar answered Nov 12 '22 09:11

MissingHandle


I'm a bit under the weather so I can't test it. But you should just put in <%= render imageable.images %> and see what it says. It should work because it should be using .modelname or .class :) If it doesn't work I would do what you're doing as mentioned above. If it doesn't work though, please comment with what it says, because then I'll probably make a patch to the rails core for that, really should work as such.

like image 37
Mohammad El-Abid Avatar answered Nov 12 '22 11:11

Mohammad El-Abid


I reckon your original idea is on the right track, with a minor adjustment:

= render imageable.images

# images/_image.html.haml
= render image.imageable_type.underscore, :object => image.imageable, :image => image

# images/_product.html.haml
// Custom code for product image (with product and image local variables)
like image 1
Zubin Avatar answered Nov 12 '22 11:11

Zubin