norman-has_image 0.1.5 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,5 +1,13 @@
1
+ 2008-08-1 Norman Clarke <norman@randomba.org>
2
+
3
+ * Fixed a bug where overwriting a previous image triggered callbacks more
4
+ than once, causing errors.
5
+
1
6
  2008-07-29 Norman Clarke <norman@randomba.org>
2
7
 
8
+ * Downcased generated file names to avoid potential issues on
9
+ case-insensitive filesystems.
10
+ * Added "absolute path" method to model instances.
3
11
  * Made image deletion nullify the "has_image_file" field.
4
12
  * Added "has_image?" method to model instances.
5
13
  * Fixed ENONENT error with record update when there are no images yet.
data/README CHANGED
@@ -65,7 +65,25 @@ Image with some thumbnails:
65
65
 
66
66
  It also provides a view helper to make displaying the images extremely simple:
67
67
 
68
- <%= image_tag_for(@photo, :thumb => :square) %>
68
+ <%= image_tag_for(@photo) # show the full-sized image %>
69
+ <%= image_tag_for(@photo, :thumb => :square) # show the square thumbnail %>
70
+
71
+ The image_tag_for helper calls Rails' image_tag, so you can pass in all the
72
+ regular options to set the alt property, CSS class, etc:
73
+
74
+ <%= image_tag_for(@photo, :alt => "my cool picture", :class => "photo") %>
75
+
76
+ Setting up forms for has_image is simple, too:
77
+
78
+ <% form_for(@photo, :html => {:multipart => true}) do |f| %>
79
+ <p>
80
+ <%= f.label :image_data %>
81
+ <%= f.file_field :image_data %>
82
+ </p>
83
+ <p>
84
+ <%= f.submit %>
85
+ </p>
86
+ <% end %>
69
87
 
70
88
  == Getting it
71
89
 
@@ -27,7 +27,7 @@ module HasImage
27
27
  # helps prevent a possibly undesirable sitation where the uploaded images
28
28
  # have offensive names.
29
29
  def random_file_name
30
- Zlib.crc32(Time.now.to_s + rand(10e10).to_s).to_s(36)
30
+ Zlib.crc32(Time.now.to_s + rand(10e10).to_s).to_s(36).downcase
31
31
  end
32
32
 
33
33
  end
data/lib/has_image.rb CHANGED
@@ -200,14 +200,27 @@ module HasImage
200
200
  def public_path(thumbnail = nil)
201
201
  storage.public_path_for(self, thumbnail)
202
202
  end
203
+
204
+ # Gets the absolute filesystem path for the image, or optionally, its
205
+ # thumbnail.
206
+ def absolute_path(thumbnail = nil)
207
+ storage.filesystem_path_for(self, thumbnail)
208
+ end
203
209
 
204
210
  # Deletes the image from the storage.
205
211
  def remove_images
206
212
  return if has_image_file.blank?
207
- storage.remove_images(self.id)
208
- update_attribute(:has_image_file, nil)
209
- rescue Errno::ENOENT
210
- logger.warn("Could not delete files for #{self.class.to_s} #{to_param}")
213
+ self.class.transaction do
214
+ begin
215
+ # Resorting to SQL here to avoid triggering callbacks. There must be
216
+ # a better way to do this.
217
+ self.connection.execute("UPDATE #{self.class.table_name} SET has_image_file = NULL WHERE id = #{id}")
218
+ self.has_image_file = nil
219
+ storage.remove_images(self.id)
220
+ rescue Errno::ENOENT
221
+ logger.warn("Could not delete files for #{self.class.to_s} #{to_param}")
222
+ end
223
+ end
211
224
  end
212
225
 
213
226
  # Creates new images and removes the old ones when image_data has been
@@ -36,6 +36,8 @@ class PicTest < Test::Unit::TestCase
36
36
  def test_create
37
37
  @pic = Pic.new(:image_data => fixture_file_upload("/image.jpg", "image/jpeg"))
38
38
  assert @pic.save!
39
+ assert_not_nil @pic.public_path
40
+ assert_not_nil @pic.absolute_path
39
41
  end
40
42
 
41
43
  def test_update
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: norman-has_image
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Norman Clarke
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-07-29 00:00:00 -07:00
12
+ date: 2008-08-01 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency