Serve up paperclip attachments in a cache friendly way
authorTom Hughes <tom@compton.nu>
Mon, 20 Feb 2012 00:38:05 +0000 (00:38 +0000)
committerTom Hughes <tom@compton.nu>
Tue, 21 Feb 2012 12:31:38 +0000 (12:31 +0000)
.gitignore
app/assets/images/users/images/large.png [moved from app/assets/images/anon_large.png with 100% similarity]
app/assets/images/users/images/small.png [moved from app/assets/images/anon_small.png with 100% similarity]
app/helpers/application_helper.rb
app/models/user.rb
config/example.application.yml
config/initializers/paperclip.rb [new file with mode: 0644]

index 2b8b7bd..021bc9b 100644 (file)
@@ -1,4 +1,5 @@
 log
 public/assets
+public/attachments
 tmp
 .DS_Store
index 2d78a5b..ce0c5d6 100644 (file)
@@ -106,21 +106,13 @@ module ApplicationHelper
   def user_image(user, options = {})
     options[:class] ||= "user_image"
 
-    if user.image.file?
-      image_tag user.image.url, options
-    else
-      image_tag "anon_large.png", options
-    end
+    image_tag user.image.url(:large), options
   end
 
   def user_thumbnail(user, options = {})
     options[:class] ||= "user_thumbnail"
 
-    if user.image.file?
-      image_tag user.image.url, options
-    else
-      image_tag "anon_small.png", options
-    end
+    image_tag user.image.url(:small), options
   end
 
   def preferred_editor
index 1f8d326..9a23089 100644 (file)
@@ -44,9 +44,9 @@ class User < ActiveRecord::Base
   after_initialize :set_creation_time
   before_save :encrypt_password
 
-  has_attached_file :image, :styles => { :thumb => "100x100>" }, 
-    :path => "#{ATTACHMENTS_DIR}/user/image/:id/:filename",
-    :url => "/user/image/:id/:filename"
+  has_attached_file :image, 
+    :default_url => "/assets/:class/:attachment/:style.png",
+    :styles => { :large => "100x100>", :small => "50x50>" }
 
   def self.authenticate(options)
     if options[:username] and options[:password]
index 763eda8..420278a 100644 (file)
@@ -56,7 +56,7 @@ defaults: &defaults
   gpx_trace_dir: "/home/osm/traces"
   gpx_image_dir: "/home/osm/images"
   # Location of data for attachments
-  attachments_dir: ":rails_root/public"
+  attachments_dir: ":rails_root/public/attachments"
   # Log file to use
   #log_path: ""
   # List of memcache servers to use for caching
diff --git a/config/initializers/paperclip.rb b/config/initializers/paperclip.rb
new file mode 100644 (file)
index 0000000..6467348
--- /dev/null
@@ -0,0 +1,20 @@
+module Paperclip
+  class AssetUrlGenerator < UrlGenerator
+    include Sprockets::Helpers::IsolatedHelper
+    include Sprockets::Helpers::RailsHelper
+
+    def for(style_name, options)
+      url = super(style_name, options)
+
+      if url =~ /^\/assets\/(.*)$/
+        asset_path($1)
+      else
+        url
+      end
+    end
+  end
+end
+
+Paperclip::Attachment.default_options[:url] = "/attachments/:class/:attachment/:id_partition/:style/:fingerprint.:extension"
+Paperclip::Attachment.default_options[:path] = "#{ATTACHMENTS_DIR}/:class/:attachment/:id_partition/:style/:fingerprint.:extension"
+Paperclip::Attachment.default_options[:url_generator] = Paperclip::AssetUrlGenerator