OSDN Git Service

Change icon's location according to Rails.env
authoreagletmt <eagletmt@gmail.com>
Tue, 6 Nov 2012 09:38:39 +0000 (18:38 +0900)
committereagletmt <eagletmt@gmail.com>
Tue, 6 Nov 2012 09:41:10 +0000 (18:41 +0900)
CutenServer/app/models/teacher.rb
CutenServer/config/initializers/paperclip.rb
CutenServer/test/test_helper.rb

index 03ada63..b797f1c 100644 (file)
@@ -11,14 +11,16 @@ class Teacher < ActiveRecord::Base
   has_many :lectures
   attr_accessible :name
 
-  has_attached_file :icon, :url => '/:attachment/:sha1.:extension', :use_timestamp => false
   attr_accessible :icon, :icon_file_name
+  has_attached_file :icon,
+    :url => ':env_prefix/:attachment/:sha1.:extension',
+    :use_timestamp => false
 
   include JsonWithoutTimestamps
   def as_json(options = {})
     icon_keys = [:icon_file_name, :icon_file_size, :icon_content_type, :icon_updated_at]
     j = super options.merge({:except => (options[:except] || []) + icon_keys})
-    j['icon_path'] = icon.url
+    j['icon_path'] = icon.exists? ? icon.url : nil
     j
   end
 
index f297c55..99adb8b 100644 (file)
@@ -2,3 +2,11 @@ Paperclip.interpolates :sha1 do |attachment, style|
   teacher = attachment.instance
   Digest::SHA1.hexdigest "#{teacher.id}##{teacher.created_at.to_i}"
 end
+
+Paperclip.interpolates :env_prefix do |attachment, style|
+  if Rails.env.production?
+    ''
+  else
+    "/#{Rails.env}"
+  end
+end
index 3551197..c8e7546 100644 (file)
@@ -35,6 +35,7 @@ Spork.prefork do
 
     teardown do
       FileUtils.rm_rf Apk.directory
+      FileUtils.rm_rf 'public/test'
     end
   end