4

I have a project using Paperclip gem for attachments and Globalize3 for attribute translation. Records need to have a different attachment for each locale.

I though about moving Paperclip attributes to translation table, and that might work, but I don't think that would work when Paperclip needs to delete attachments.

What's the best way to achieve something like that?

UPDATE: to be clear, I want this because my client wants to upload different images for each locale.

Slobodan Kovacevic
  • 6,848
  • 3
  • 29
  • 33
  • i don't know globalize3 and this is not exactly a solution, but I think it would be a lot easier to implement whith [Carrierwave](https://github.com/jnicklas/carrierwave) than with Paperclip. Carrierwave uses seperate classes ("Uploaders") to perform its job, and just needs an attribute on your model to be mounted on - which globalize3 seems to provide. – m_x Mar 21 '13 at 08:33
  • I could do the same thing with Paperclip too (it also uses few string columns that could be translated), but either one would have issues with updating and/or destroying attachments. – Slobodan Kovacevic Mar 23 '13 at 08:49
  • maybe add your own callbacks then? – m_x Mar 23 '13 at 18:52
  • I have something of the sort implemented for Carrierwave + Globalize3. If that is something of worth to you I can share that if you want. Let me know. – Hugo Mar 25 '13 at 21:03
  • @Hugo yes, it would be great if you could share it. I'm realizing that I'll have to do this on my own, i.e. just create a separate class with locale attribute and Paperclip attachment. Then in main class do the has_many attachments. – Slobodan Kovacevic Mar 27 '13 at 16:42
  • Ok I saw this only now, give me a day to post back what I have. – Hugo Mar 28 '13 at 15:20

3 Answers3

3

Unfortunately I didn't find a way to do this using Globalize3. In theory, I could have added a separate model for image and add image_id to list of translated columns (to have something like MainModel -> Translation -> Image), but it seems that Globalize has some migration issues with non-string columns.

Instead of using Globalize3, I did this with a separate Image model with locale attribute and main model which accepts nested attributes for it. Something along the lines of:

class MainModel < ActiveRecord::Base
  has_many :main_model_images
  accepts_nested_attributes_for :main_model_images

  # return image for locale or any other as a fallback
  def localized_image(locale)
    promo_box_images.where(:locale => locale).first || promo_box_images.first
  end
end

class MainModelImage < ActiveRecord::Base
  belongs_to :main_model
  has_attached_file :image

  validates :locale,
    :presence => true,
    :uniqueness => { :scope => :main_model_id }
end

Tricky part was getting form to accept nested attributes only for one image, instead of all images in has_many relation.

=f.fields_for :main_model_images, @main_model.image_for_locale(I18n.locale) do |f_image|
  =f_image.hidden_field :locale
  =f_image.label :image
Murkantilism
  • 1,060
  • 1
  • 16
  • 31
Slobodan Kovacevic
  • 6,848
  • 3
  • 29
  • 33
1

You could also try the paperclip-globalize3 gem, it should handle the case you describe. https://github.com/emjot/paperclip-globalize3

Max
  • 19
  • 1
  • The paperclip-globalize3 gem is totally outdated. I think the solution of Slobodan Kovacevic in the answer above is a future stable one! – Simon Franzen Nov 27 '16 at 19:26
0

Ok since you asked me to share my solution to this problem even though I am using Carrierwave as a library for uploading here is it:

Ok so I would have a model setup like this:

class MyModel < ActiveRecord::Base
  # ...

  translates :attr_one, :attr_two, :uploaded_file

Now what I need for CarrierWave to work is place to attach the uploader to and that can be done on the Translation model

  Translation.mount_uploader :uploaded_file, FileUploader
end

Now for your question about deleting, I think though I haven't needed to do it but it should work as the README says it should but on the translation model. https://github.com/jnicklas/carrierwave#removing-uploaded-files

MyModel.first.translation.remove_uploaded_file!

I haven't taken a look at paperclip for a good 2 years and if this is not applicable knowledge I suggest you try out carrierwave.

Hugo
  • 2,913
  • 1
  • 20
  • 21