Category: Default

FriendlyId Ruby on Rails gem – update (refresh) slug after each record change

If you want to have slugs that are refreshed (updated) each time you change corresponding field in your ActiveRecord models, put this at the end of your initializers/friendly_id.rb file:

module FriendlyId
  module Slugged
    def should_generate_new_friendly_id?
      return true if send(friendly_id_config.slug_column).nil? && !send(friendly_id_config.base).nil?

      change = :"#{friendly_id_config.base}_changed?"
      return true if respond_to?(change) && send(change)

      false
    end
  end
end

This will update slug each time related column (friendly_id_config.base) has changed. Keep in mind, that this method won't work if you use non-column slug method as a base for friendly ids, since it uses ActiveRecord::Dirty interface.

Rails + Paperclip + Open-Uri – Downloading files from the internet and saving them with Paperclip

I don't like hotlinking. Being dependent on someone’s else infrastructure makes me a bit worried. Especially when I don't know who this "someone" is. That's why I've decided to download all the images that users embed in comments. After download, I attach them with Paperclip to their comments. Thanks to that, I can be sure, that they will always be served from my own servers (also I can use them later for some fancy stuff).

Theoretically, to obtain this, we could do something like that:

# We will use open-uri to download embedded images
require "open-uri"

file = open(image_url)
current_comment.pictures.create!(file: file)

Unfortunately this will "almost" work. If we try to get the file url:

current_picture.file.url #=> '/images/pictures/12313/file.'

we'll get it without an file extension. This happens, because open-uri creates a Tempfile without a file extension. Unluckily Paperclip uses file name to determine extension. To get around this issue, we need to create a new Tempfile with a valid extension and copy open-uri Tempfile content to newly created one:

# We will use open-uri to download embedded images
require "open-uri"

file = Tempfile.new ['', ".#{image_url.split('.').last}"]
file.binmode # note that our tempfile must be in binary mode
file.write open(image_url).read
file.rewind
file
current_comment.pictures.create!(file: file)

Update - Paperclip can do this on its own!

Posted by Aditya Sanghi (thanks a lot!):

current_comments.pictures.create!(file: URI.parse(image_url))

Although keep in mind, that you still need to handle 500, 404, etc errors (Paperclip can raise them).

Copyright © 2024 Closer to Code

Theme by Anders NorenUp ↑