Using dragonfly with globalize - ruby-on-rails

I'm trying to i18n the image_uid attribute in my model so I can have different images with different languages. I'm using globalize3 and dragonfly.
The problem is that is not working at all. It usually uploads the spanish image (which is the default locale in my app), but it doesn't work with other locales. I don't get any error or trace, it just doesn't work.
The model is quite simple:
image_accessor :image
translates :image_uid
Any idea?

:image_uid is the uniqe id to identify the image and therefore not suitable for translation. But if a :image_name attribute is present Dragonfly uses that as filename.
If you want to have a different image for every language because you e.g insert text, you have to watch out that the name or more precisely the url is always different, e.g. you could append the locale to the url: ?locale=en, or name your files translation.en.jpg. To assign the name you can use the helper methods #model.image.name, .basename, and .ext.
If you want only the filename to change and always have the same image served you need a url rewrite engine and remove the filename before your cache. Otherwise unneeded copies of the same image would be created and waste your disk space and processing power.
Using the rack-rewrite and rack-cache gems it would look something like that:
require 'dragonfly/rails/images'
Rails.application.middleware.insert_before(Rack::Cache, Rack::Rewrite) do
rewrite %r{/media/([^/]+)/[^?]*(.*)}, '/media/$1$2'
end

Related

How to make Carrierwave add random string to the file path?

I have this kind of hot fix:
= image_tag "#{current_candidate.profile.photo_url}?stamp=#{SecureRandom.hex}"
The reason for this is that I made a "crop" feature, and when users recrop their photo - old version gets displayed from browser cache.
Anyway.
This photo_url is called in many places, and rather than making a helper & call it everywhere I thought it would be nice to configure uploader to do that.
Is that possible?
Note: filename can't be random. For other reasons.
I would imagine it has something to do with overriding photo_url method.

Paperclip Rails url and asset_host

According to the Paperclip S3 Docs one can specify the :url option in the config which has four possible values. There are comments related to this options that say:
The fourth option for the S3 url is :asset_host, which uses Rails' built-in asset_host settings.
To get the full url from a paperclip'd object, use the image_path helper; this is what image_tag uses to generate the url for an img tag.
These two comments seem in conflict with each other (to me). If Paperclip can use the asset_host settings, it seems almost necessary that it would generate the full url (since the asset_host only specifies the start (host) of that url)
But it then goes on to say you need to use a helper to get the full url??
The reason I ask this is because I want full urls generated for image url serialization (ie if we're returning json with image_urls, we want those served from our CDN).
Right now I've created a helper module that extends extend ::Sprockets::Helpers::RailsHelper to manually generate full urls any time an image_url is being serialized, but it's manual (and someone could possibly forget to do it in the future)
Any thoughts?

Creating a shortened URL for all objects in the database

I would like to display a shortened URL besides the content items on my site for ease of sharing.
What would be the most efficient way of doing so, and are there any suitable gems / libraries?
I am using rails on a mongodb/mongoid stack
should be simple enough (regardless if you are on Mongo / MySQL or anything else). what you need is a small collection (mongo if i may) that holds some kind of an MD5 hash of the real url you are after and the real url itself, for example:
ShortLink.create(:hash_link => Digest::MD5.hexdigest(resource_url(#resource)), :real_link => resource_url(#resource))
I suggest adding another route that catches those like this:
match "l/:key", "ShortLinks#show"
should be easy.
I think you can use bitly gem to shorten your URL.
The following link helps you to configure bitly:
http://www.marketingformavens.com/blog/url-shortening-bitly-ruby-on-rails

Using Paperclip with FFmpeg and multiple output formats: how to store in the database and show in the view a specific output format?

Because I want to use HTML5 video I need to convert videos uploaded by the user into multiple codecs. I am following this tutorial: http://jimneath.org/2008/06/03/converting-videos-with-rails-converting-the-video/ but it only outputs FLV, a format I don't even want at all.
I know I can add more FFmpeg commands and convert multiple times, but the problem lies in the database and the view. How can I define multiple source_file_names in the DB, and how can I specify in #video.source.url which format I want? Should I subclass the Video model, add more table columns for each type or something else?
Paperclip allows you to specify multiple styles. Generally, this is used to specify multiple sizes for uploaded images; Paperclip processes the image once for each style and places them in the filesystem accordingly. By specifying different video formats for your styles and subclassing Paperclip::Processor, you can create your own video formats. In your model, you'd do something like this:
has_attached_file :video, :styles => { :mpeg, :ogg, :wmv }, :processors => [:my_custom_processor]
And then create a custom Processor that runs the correct FFmpeg command based on each style. See the documentation for more info, but here is a snippet:
Paperclip processors allow you to
modify attached files when they are
attached in any way you are able.
Paperclip itself uses command-line
programs for its included Thumbnail
processor, but custom processors are
not required to follow suit.
Processors are required to be defined
inside the Paperclip module and are
also required to be a subclass of
Paperclip::Processor. There is only
one method you must implement to
properly be a subclass: #make, but #initialize
may also be of use. Both methods accept 3 arguments: the file
that will be operated on (which is an
instance of File), a hash of options
that were defined in
has_attached_file’s style hash, and
the Paperclip::Attachment itself.
All #make needs to return is an
instance of File (Tempfile is
acceptable) which contains the results
of the processing.
See Paperclip.run for more
information about using command-line
utilities from within Processors.
When you create a link to a Paperclip attachment, you pass the style to determine which to link to:
<%= link_to "mpeg video", #model.video.url(:mpeg) %>
<%= link_to "ogg video", #model.video.url(:ogg) %>
I suggest you tryout paperclip-ffmpeg gem.

Browser Caching with Paperclip

I have a quite long slideshow of images, all rendered via the awesome Paperclip.
Those images won't change very often, so I'd like to make use of browser caching.
Problem is Paperclip appends a unique load id to the image url.
(eg: pancakes.png?1275839986)
Is there any way to prevent this from happening?
Thanks!
When specifying the image url using:
UrModel.image.url(:style)
# results in: /system/model/000/001/style/style.png?<atimestamp>
Pass in a second parameter which indicates whether or not to include the timestamp:
UrModel.image.url(:style, false)
# results in: /system/model/000/001/style/style.png
Read more about paperclips caching helper: https://github.com/thoughtbot/paperclip/wiki/Tips
That numeric suffix is stored in the database itself by paperclip and is not unique per page load. It's there for caching reasons, actually.

Resources