jcnetdev-paperclip 1.0.20080704 → 1.1

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -12,7 +12,7 @@ task :default => [:clean, :test]
12
12
  desc 'Test the paperclip plugin.'
13
13
  Rake::TestTask.new(:test) do |t|
14
14
  t.libs << 'lib' << 'profile'
15
- t.pattern = 'test/**/test_*.rb'
15
+ t.pattern = 'test/**/*_test.rb'
16
16
  t.verbose = true
17
17
  end
18
18
 
@@ -46,6 +46,7 @@ task :clean do |t|
46
46
  end
47
47
 
48
48
  spec = Gem::Specification.new do |s|
49
+ s.rubygems_version = "1.2.0"
49
50
  s.name = "paperclip"
50
51
  s.version = Paperclip::VERSION
51
52
  s.author = "Jon Yurek"
@@ -65,6 +66,9 @@ spec = Gem::Specification.new do |s|
65
66
  s.extra_rdoc_files = ["README"]
66
67
  s.rdoc_options << '--line-numbers' << '--inline-source'
67
68
  s.requirements << "ImageMagick"
69
+ s.add_runtime_dependency 'right_aws'
70
+ s.add_development_dependency 'Shoulda'
71
+ s.add_development_dependency 'mocha'
68
72
  end
69
73
 
70
74
  Rake::GemPackageTask.new(spec) do |pkg|
@@ -1,5 +1,5 @@
1
1
  Usage:
2
2
 
3
- script/generate attachment Class attachment1 attachment2
3
+ script/generate paperclip Class attachment1 (attachment2 ...)
4
4
 
5
5
  This will create a migration that will add the proper columns to your class's table.
data/lib/paperclip.rb CHANGED
@@ -133,7 +133,7 @@ module Paperclip
133
133
  end
134
134
 
135
135
  validates_each(name) do |record, attr, value|
136
- value.send(:flush_errors)
136
+ value.send(:flush_errors) unless value.valid?
137
137
  end
138
138
  end
139
139
 
@@ -190,7 +190,7 @@ module Paperclip
190
190
  unless options[:content_type].blank?
191
191
  content_type = instance[:"#{name}_content_type"]
192
192
  unless valid_types.any?{|t| t === content_type }
193
- options[:message] || ActiveRecord::Errors.default_error_messages[:inclusion]
193
+ options[:message] || "is not one of the allowed file types."
194
194
  end
195
195
  end
196
196
  end
@@ -93,6 +93,7 @@ module Paperclip
93
93
 
94
94
  # Returns true if there are any errors on this attachment.
95
95
  def valid?
96
+ validate
96
97
  errors.length == 0
97
98
  end
98
99
 
@@ -159,14 +160,15 @@ module Paperclip
159
160
  # again.
160
161
  def reprocess!
161
162
  new_original = Tempfile.new("paperclip-reprocess")
162
- old_original = to_file(:original)
163
- new_original.write( old_original.read )
164
- new_original.rewind
163
+ if old_original = to_file(:original)
164
+ new_original.write( old_original.read )
165
+ new_original.rewind
165
166
 
166
- @queued_for_write = { :original => new_original }
167
- post_process
167
+ @queued_for_write = { :original => new_original }
168
+ post_process
168
169
 
169
- old_original.close if old_original.respond_to?(:close)
170
+ old_original.close if old_original.respond_to?(:close)
171
+ end
170
172
  end
171
173
 
172
174
  private
@@ -182,6 +184,7 @@ module Paperclip
182
184
  end.flatten.compact.uniq
183
185
  @errors += @validation_errors
184
186
  end
187
+ @validation_errors
185
188
  end
186
189
 
187
190
  def normalize_style_definition
@@ -202,6 +205,7 @@ module Paperclip
202
205
  @styles.each do |name, args|
203
206
  begin
204
207
  dimensions, format = args
208
+ dimensions = dimensions.call(instance) if dimensions.respond_to? :call
205
209
  @queued_for_write[name] = Thumbnail.make(@queued_for_write[:original],
206
210
  dimensions,
207
211
  format,
@@ -6,12 +6,15 @@ module Paperclip
6
6
 
7
7
  # Infer the MIME-type of the file from the extension.
8
8
  def content_type
9
- type = self.path.match(/\.(\w+)$/)[1] rescue "octet-stream"
9
+ type = (self.path.match(/\.(\w+)$/)[1] rescue "octet-stream").downcase
10
10
  case type
11
- when "jpg", "png", "gif" then "image/#{type}"
12
- when "txt" then "text/plain"
13
- when "csv", "xml", "html", "htm", "css", "js" then "text/#{type}"
14
- else "x-application/#{type}"
11
+ when %r"jpe?g" then "image/jpeg"
12
+ when %r"tiff?" then "image/tiff"
13
+ when %r"png", "gif", "bmp" then "image/#{type}"
14
+ when "txt" then "text/plain"
15
+ when %r"html?" then "text/html"
16
+ when "csv", "xml", "css", "js" then "text/#{type}"
17
+ else "application/x-#{type}"
15
18
  end
16
19
  end
17
20
 
@@ -31,3 +34,4 @@ end
31
34
  class File #:nodoc:
32
35
  include Paperclip::Upfile
33
36
  end
37
+
data/paperclip.gemspec CHANGED
@@ -1,7 +1,7 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'paperclip'
3
- s.version = '1.0.20080704'
4
- s.date = '2008-07-04'
3
+ s.version = '1.1'
4
+ s.date = '2008-07-14'
5
5
 
6
6
  s.summary = "Allows easy file uploading for Rails"
7
7
  s.description = "Paperclip is intended as an easy file attachment library for ActiveRecord. The intent behind it was to keep setup as easy as possible and to treat files as much like other attributes as possible. This means they aren't saved to their final locations on disk, nor are they deleted if set to nil, until ActiveRecord::Base#save is called. It manages validations based on size and presence, if required. It can transform its assigned image into thumbnails if needed, and the prerequisites are as simple as installing ImageMagick (which, for most modern Unix-based systems, is as easy as installing the right packages). Attached files are saved to the filesystem and referenced in the browser by an easily understandable specification, which has sensible and useful defaults."
@@ -35,21 +35,19 @@ Gem::Specification.new do |s|
35
35
  "rails/init.rb",
36
36
  "tasks/paperclip_tasks.rake"]
37
37
 
38
- s.test_files = ["test/.gitignore",
38
+ s.test_files = ["test/attachment_test.rb",
39
39
  "test/database.yml",
40
40
  "test/fixtures/12k.png",
41
41
  "test/fixtures/50x50.png",
42
42
  "test/fixtures/5k.png",
43
43
  "test/fixtures/bad.png",
44
44
  "test/fixtures/text.txt",
45
+ "test/geometry_test.rb",
45
46
  "test/helper.rb",
46
- "test/test_attachment.rb",
47
- "test/test_geometry.rb",
48
- "test/test_integration.rb",
49
- "test/test_iostream.rb",
50
- "test/test_paperclip.rb",
51
- "test/test_storage.rb",
52
- "test/test_thumbnail.rb"]
53
-
47
+ "test/integration_test.rb",
48
+ "test/iostream_test.rb",
49
+ "test/paperclip_test.rb",
50
+ "test/storage_test.rb",
51
+ "test/thumbnail_test.rb"]
54
52
  end
55
53
 
@@ -14,25 +14,62 @@ def obtain_attachments
14
14
  end
15
15
  end
16
16
 
17
+ def for_all_attachments
18
+ klass = obtain_class
19
+ names = obtain_attachments
20
+ ids = klass.connection.select_values("SELECT id FROM #{klass.table_name}")
21
+
22
+ ids.each do |id|
23
+ instance = klass.find(id)
24
+ names.each do |name|
25
+ result = if instance.send("#{ name }?")
26
+ yield(instance, name)
27
+ else
28
+ true
29
+ end
30
+ print result ? "." : "x"; $stdout.flush
31
+ end
32
+ end
33
+ puts " Done."
34
+ end
35
+
17
36
  namespace :paperclip do
18
- desc "Regenerates thumbnails for a given CLASS (and optional ATTACHMENT)"
19
- task :refresh => :environment do
20
- klass = obtain_class
21
- names = obtain_attachments
22
- instances = klass.find(:all)
23
-
24
- puts "Regenerating thumbnails for #{instances.length} instances of #{klass.name}:"
25
- instances.each do |instance|
26
- names.each do |name|
27
- result = if instance.send("#{ name }?")
28
- instance.send(name).reprocess!
29
- instance.send(name).save
37
+ desc "Refreshes both metadata and thumbnails."
38
+ task :refresh => ["paperclip:refresh:metadata", "paperclip:refresh:thumbnails"]
39
+
40
+ namespace :refresh do
41
+ desc "Regenerates thumbnails for a given CLASS (and optional ATTACHMENT)."
42
+ task :thumbnails => :environment do
43
+ for_all_attachments do |instance, name|
44
+ instance.send(name).reprocess!
45
+ end
46
+ end
47
+
48
+ desc "Regenerates content_type/size metadata for a given CLASS (and optional ATTACHMENT)."
49
+ task :metadata => :environment do
50
+ for_all_attachments do |instance, name|
51
+ if file = instance.send(name).to_file
52
+ instance.send("#{name}_file_name=", instance.send("#{name}_file_name").strip)
53
+ instance.send("#{name}_content_type=", file.content_type.strip)
54
+ instance.send("#{name}_file_size=", file.size) if instance.respond_to?("#{name}_file_size")
55
+ instance.save(false)
30
56
  else
31
57
  true
32
58
  end
33
- print result ? "." : "x"; $stdout.flush
34
59
  end
35
60
  end
36
- puts " Done."
61
+ end
62
+
63
+ desc "Cleans out invalid attachments. Useful after you've added new validations."
64
+ task :clean => :environment do
65
+ for_all_attachments do |instance, name|
66
+ instance.send(name).send(:validate)
67
+ if instance.send(name).valid?
68
+ true
69
+ else
70
+ instance.send("#{name}=", nil)
71
+ instance.save
72
+ end
73
+ end
37
74
  end
38
75
  end
File without changes
File without changes
File without changes
File without changes
@@ -64,6 +64,38 @@ class PaperclipTest < Test::Unit::TestCase
64
64
  assert Dummy.new.respond_to?(:avatar=)
65
65
  end
66
66
 
67
+ context "that is valid" do
68
+ setup do
69
+ @dummy = Dummy.new
70
+ @dummy.avatar = @file
71
+ end
72
+
73
+ should "be valid" do
74
+ assert @dummy.valid?
75
+ end
76
+
77
+ context "then has a validation added that makes it invalid" do
78
+ setup do
79
+ assert @dummy.save
80
+ Dummy.class_eval do
81
+ validates_attachment_content_type :avatar, :content_type => ["text/plain"]
82
+ end
83
+ @dummy2 = Dummy.find(@dummy.id)
84
+ end
85
+
86
+ should "be invalid when reloaded" do
87
+ assert ! @dummy2.valid?, @dummy2.errors.inspect
88
+ end
89
+
90
+ should "be able to call #valid? twice without having duplicate errors" do
91
+ @dummy2.avatar.valid?
92
+ first_errors = @dummy2.avatar.errors
93
+ @dummy2.avatar.valid?
94
+ assert_equal first_errors, @dummy2.avatar.errors
95
+ end
96
+ end
97
+ end
98
+
67
99
  [[:presence, nil, "5k.png", nil],
68
100
  [:size, {:in => 1..10240}, "5k.png", "12k.png"],
69
101
  [:size2, {:in => 1..10240}, nil, "12k.png"],
@@ -102,21 +134,6 @@ class PaperclipTest < Test::Unit::TestCase
102
134
  assert_equal 1, @dummy.avatar.errors.length
103
135
  end
104
136
  end
105
-
106
- # context "and an invalid file with :message" do
107
- # setup do
108
- # @file = args[3] && File.new(File.join(FIXTURES_DIR, args[3]))
109
- # end
110
- #
111
- # should "have errors" do
112
- # if args[1] && args[1][:message] && args[4]
113
- # @dummy.avatar = @file
114
- # assert ! @dummy.avatar.valid?
115
- # assert_equal 1, @dummy.avatar.errors.length
116
- # assert_equal args[4], @dummy.avatar.errors[0]
117
- # end
118
- # end
119
- # end
120
137
  end
121
138
  end
122
139
  end
File without changes
File without changes
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jcnetdev-paperclip
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.20080704
4
+ version: "1.1"
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jon Yurek
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-07-04 00:00:00 -07:00
12
+ date: 2008-07-14 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -76,18 +76,17 @@ signing_key:
76
76
  specification_version: 2
77
77
  summary: Allows easy file uploading for Rails
78
78
  test_files:
79
- - test/.gitignore
79
+ - test/attachment_test.rb
80
80
  - test/database.yml
81
81
  - test/fixtures/12k.png
82
82
  - test/fixtures/50x50.png
83
83
  - test/fixtures/5k.png
84
84
  - test/fixtures/bad.png
85
85
  - test/fixtures/text.txt
86
+ - test/geometry_test.rb
86
87
  - test/helper.rb
87
- - test/test_attachment.rb
88
- - test/test_geometry.rb
89
- - test/test_integration.rb
90
- - test/test_iostream.rb
91
- - test/test_paperclip.rb
92
- - test/test_storage.rb
93
- - test/test_thumbnail.rb
88
+ - test/integration_test.rb
89
+ - test/iostream_test.rb
90
+ - test/paperclip_test.rb
91
+ - test/storage_test.rb
92
+ - test/thumbnail_test.rb
data/test/.gitignore DELETED
@@ -1 +0,0 @@
1
- debug.log