Last active
December 11, 2015 20:59
-
-
Save mendelgusmao/4659230 to your computer and use it in GitHub Desktop.
Caching Dropbox URLs for paperclip-dropbox (not fully tested; not handling "missing" properly)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AddAttachmentAvatarToUsers < ActiveRecord::Migration | |
def self.up | |
change_table :users do |t| | |
t.attachment :avatar | |
end | |
add_column :users, :avatar_dropbox, :string, :default => {}.to_yaml | |
end | |
def self.down | |
drop_attached_file :users, :avatar | |
remove_column :users, :avatar_dropbox | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ApplicationHelper | |
def dropbox_attachment_url(object, attribute, style = :original) | |
base = "#{attribute}_dropbox" | |
cache = object.send(base) || {} | |
cache[style] ||= {} | |
cache[style][:last_request] ||= Time.now - 4.hours | |
if Time.now - cache[style][:last_request] > 4.hours - 1.minute | |
attachment = object.send(attribute, style) | |
cache[style][:url] = attachment | |
cache[style][:last_request] = Time.now | |
object.update_attributes({base => cache}) | |
cache[style][:url] | |
else | |
object.send(base)[style][:url] | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User < ActiveRecord::Base | |
serialize :avatar_dropbox | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%= tag(:img, { :src => dropbox_attachment_url(@user, :avatar) }, false) %> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment