public
Rubygem
Description: A lightweight and opinionated but hackable library for attaching images to ActiveRecord models.
Homepage:
Clone URL: git://github.com/norman/has_image.git
fixed filesystem-cleanup in pic_test
introduced option to has_image to control thumbnail generation in a 
callback
Gerrit Kaiser (author)
Mon Oct 20 22:15:35 -0700 2008
commit  3fc98eb53c49f740a83edf77e9971abb8105e3a2
tree    2eddec718769cd99da4958b76d7b9af3b19778bd
parent  05a2527dd190e39d1ae60f7e48f40ad5c79002ab
...
52
53
54
 
55
56
57
...
52
53
54
55
56
57
58
0
@@ -52,6 +52,7 @@ module HasImage
0
       {
0
         :resize_to => "200x200",
0
         :thumbnails => {},
0
+        :auto_generate_thumbnails => true,
0
         :max_size => 12.megabytes,
0
         :min_size => 4.kilobytes,
0
         :path_prefix => klass.table_name,
...
84
85
86
87
 
88
89
90
...
169
170
171
 
172
173
174
...
191
192
193
 
 
 
 
 
194
195
196
...
84
85
86
 
87
88
89
90
...
169
170
171
172
173
174
175
...
192
193
194
195
196
197
198
199
200
201
202
0
@@ -84,7 +84,7 @@ module HasImage
0
     def install_images(object)
0
       generated_name = Storage.generated_file_name(object)
0
       install_main_image(object.has_image_id, generated_name)
0
-      generate_thumbnails(object.has_image_id, generated_name) unless options[:thumbnails].empty?
0
+      generate_thumbnails(object.has_image_id, generated_name) if thumbnails_needed?
0
       return generated_name
0
     ensure  
0
       @temp_file.close! if !@temp_file.closed?
0
@@ -169,6 +169,7 @@ module HasImage
0
     #
0
     #  /var/sites/example.org/production/public/photos/0000/0001
0
     def path_for(id)
0
+      debugger if $debug
0
       File.join(options[:base_path], options[:path_prefix], Storage.partitioned_path(id))
0
     end
0
     
0
@@ -191,6 +192,11 @@ module HasImage
0
       end
0
     end
0
     
0
+    # used in #install_images
0
+    def thumbnails_needed?
0
+      !options[:thumbnails].empty? && options[:auto_generate_thumbnails]
0
+    end
0
+    
0
     # Instantiates the processor using the options set in my contructor (if
0
     # not already instantiated), stores it in an instance variable, and
0
     # returns it.
...
118
119
120
 
 
 
 
 
 
 
 
 
 
121
122
123
...
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
0
@@ -118,6 +118,16 @@ class StorageTest < Test::Unit::TestCase
0
     assert @storage.remove_images(stub(:has_image_id => 1), @name)
0
   end
0
   
0
+  def test_install_images_doesnt_automatically_generate_thumbnails_if_that_option_is_set
0
+    @storage = HasImage::Storage.new(default_options.merge(
0
+      :thumbnails => {:two => "200x200"},
0
+      :auto_generate_thumbnails => false
0
+    ))
0
+    @storage.image_data = temp_file("image.jpg")
0
+    @storage.expects(:generate_thumbnails).never
0
+    @storage.install_images(stub(:has_image_id => 1))
0
+  end
0
+
0
   def test_image_not_too_small
0
     @storage = HasImage::Storage.new(default_options.merge(:min_size => 1.kilobyte))
0
     @storage.image_data = temp_file("image.jpg")
...
10
11
12
 
13
14
 
15
16
17
...
64
65
66
 
 
 
 
 
 
 
 
 
 
 
 
 
67
68
69
70
 
 
71
72
73
...
10
11
12
13
14
 
15
16
17
18
...
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
 
 
83
84
85
86
87
0
@@ -10,8 +10,9 @@ end
0
 
0
 class PicTest < Test::Unit::TestCase
0
   def setup
0
+    # Note: Be sure to not set the whole options hash in your tests below
0
     Pic.has_image_options = HasImage.default_options_for(Pic)
0
-    Pic.has_image_options[:base_path] = File.join(RAILS_ROOT, '/tmp')
0
+    Pic.has_image_options[:base_path] = File.join(RAILS_ROOT, 'tmp')
0
   end
0
   
0
   def teardown
0
@@ -64,10 +65,23 @@ class PicTest < Test::Unit::TestCase
0
   def test_default_options_respect_table_name
0
     assert_equal 'pics', HasImage.default_options_for(PicWithDifferentTableName)[:path_prefix]
0
   end
0
+  
0
+  def test_generate_thumbnails_on_create
0
+    Pic.has_image_options[:thumbnails] = {:tiny => "16x16"}
0
+    @pic = Pic.create!(:image_data => fixture_file_upload("/image.jpg", "image/jpeg"))
0
+    assert File.exist?(@pic.absolute_path(:tiny))
0
+  end
0
+  
0
+  def test_doesnt_generate_thumbnails_if_option_disabled
0
+    Pic.has_image_options[:thumbnails] = {:tiny => "16x16"}
0
+    Pic.has_image_options[:auto_generate_thumbnails] = false
0
+    @pic = Pic.create!(:image_data => fixture_file_upload("/image.jpg", "image/jpeg"))
0
+    assert !File.exist?(@pic.absolute_path(:tiny))
0
+  end
0
 
0
   def test_regenerate_thumbnails_succeeds
0
-    Pic.has_image_options = HasImage.default_options_for(Pic).merge(
0
-      :thumbnails => {:small => "100x100", :tiny => "16x16"})
0
+    Pic.has_image_options[:thumbnails] = {:small => "100x100", :tiny => "16x16"}
0
+    
0
     @pic = Pic.new(:image_data => fixture_file_upload("/image.jpg", "image/jpeg"))
0
     @pic.save!
0
     assert @pic.regenerate_thumbnails

Comments