paperclip 2.3.6 → 2.3.7

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of paperclip might be problematic. Click here for more details.

@@ -113,6 +113,12 @@ module Paperclip
113
113
  processor
114
114
  end
115
115
 
116
+ def each_instance_with_attachment(klass, name)
117
+ Object.const_get(klass).all.each do |instance|
118
+ yield(instance) if instance.send(:"#{name}?")
119
+ end
120
+ end
121
+
116
122
  # Log a paperclip-specific line. Uses ActiveRecord::Base.logger
117
123
  # by default. Set Paperclip.options[:log] to false to turn off.
118
124
  def log message
@@ -1,3 +1,3 @@
1
1
  module Paperclip
2
- VERSION = "2.3.6" unless defined? Paperclip::VERSION
2
+ VERSION = "2.3.7" unless defined? Paperclip::VERSION
3
3
  end
@@ -1,7 +1,6 @@
1
1
  def obtain_class
2
2
  class_name = ENV['CLASS'] || ENV['class']
3
3
  raise "Must specify CLASS" unless class_name
4
- @klass = Object.const_get(class_name)
5
4
  end
6
5
 
7
6
  def obtain_attachments
@@ -14,25 +13,6 @@ def obtain_attachments
14
13
  end
15
14
  end
16
15
 
17
- def for_all_attachments
18
- klass = obtain_class
19
- names = obtain_attachments
20
- ids = klass.connection.select_values(klass.send(:construct_finder_sql, :select => 'id'))
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
-
36
16
  namespace :paperclip do
37
17
  desc "Refreshes both metadata and thumbnails."
38
18
  task :refresh => ["paperclip:refresh:metadata", "paperclip:refresh:thumbnails"]
@@ -41,24 +21,31 @@ namespace :paperclip do
41
21
  desc "Regenerates thumbnails for a given CLASS (and optional ATTACHMENT)."
42
22
  task :thumbnails => :environment do
43
23
  errors = []
44
- for_all_attachments do |instance, name|
45
- result = instance.send(name).reprocess!
46
- errors << [instance.id, instance.errors] unless instance.errors.blank?
47
- result
24
+ names = obtain_attachments
25
+ klass = obtain_class
26
+ names.each do |name|
27
+ Paperclip.each_instance_with_attachment(klass, name) do |instance|
28
+ result = instance.send(name).reprocess!
29
+ errors << [instance.id, instance.errors] unless instance.errors.blank?
30
+ end
48
31
  end
49
32
  errors.each{|e| puts "#{e.first}: #{e.last.full_messages.inspect}" }
50
33
  end
51
34
 
52
35
  desc "Regenerates content_type/size metadata for a given CLASS (and optional ATTACHMENT)."
53
36
  task :metadata => :environment do
54
- for_all_attachments do |instance, name|
55
- if file = instance.send(name).to_file
56
- instance.send("#{name}_file_name=", instance.send("#{name}_file_name").strip)
57
- instance.send("#{name}_content_type=", file.content_type.strip)
58
- instance.send("#{name}_file_size=", file.size) if instance.respond_to?("#{name}_file_size")
59
- instance.save(false)
60
- else
61
- true
37
+ names = obtain_attachments
38
+ klass = obtain_class
39
+ names.each do |name|
40
+ Paperclip.each_instance_with_attachment(klass, name) do |instance|
41
+ if file = instance.send(name).to_file
42
+ instance.send("#{name}_file_name=", instance.send("#{name}_file_name").strip)
43
+ instance.send("#{name}_content_type=", file.content_type.strip)
44
+ instance.send("#{name}_file_size=", file.size) if instance.respond_to?("#{name}_file_size")
45
+ instance.save(false)
46
+ else
47
+ true
48
+ end
62
49
  end
63
50
  end
64
51
  end
@@ -66,13 +53,17 @@ namespace :paperclip do
66
53
 
67
54
  desc "Cleans out invalid attachments. Useful after you've added new validations."
68
55
  task :clean => :environment do
69
- for_all_attachments do |instance, name|
70
- instance.send(name).send(:validate)
71
- if instance.send(name).valid?
72
- true
73
- else
74
- instance.send("#{name}=", nil)
75
- instance.save
56
+ names = obtain_attachments
57
+ klass = obtain_class
58
+ names.each do |name|
59
+ Paperclip.each_instance_with_attachment(klass, name) do |instance|
60
+ instance.send(name).send(:validate)
61
+ if instance.send(name).valid?
62
+ true
63
+ else
64
+ instance.send("#{name}=", nil)
65
+ instance.save
66
+ end
76
67
  end
77
68
  end
78
69
  end
@@ -43,6 +43,23 @@ class PaperclipTest < Test::Unit::TestCase
43
43
  end
44
44
  end
45
45
 
46
+ context "Paperclip.each_instance_with_attachment" do
47
+ setup do
48
+ @file = File.new(File.join(FIXTURES_DIR, "5k.png"), 'rb')
49
+ d1 = Dummy.create(:avatar => @file)
50
+ d2 = Dummy.create
51
+ d3 = Dummy.create(:avatar => @file)
52
+ @expected = [d1, d3]
53
+ end
54
+ should "yield every instance of a model that has an attachment" do
55
+ actual = []
56
+ Paperclip.each_instance_with_attachment("Dummy", "avatar") do |instance|
57
+ actual << instance
58
+ end
59
+ assert_same_elements @expected, actual
60
+ end
61
+ end
62
+
46
63
  should "raise when sent #processor and the name of a class that exists but isn't a subclass of Processor" do
47
64
  assert_raises(Paperclip::PaperclipError){ Paperclip.processor(:attachment) }
48
65
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: paperclip
3
3
  version: !ruby/object:Gem::Version
4
- hash: 15
4
+ hash: 13
5
5
  prerelease: false
6
6
  segments:
7
7
  - 2
8
8
  - 3
9
- - 6
10
- version: 2.3.6
9
+ - 7
10
+ version: 2.3.7
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jon Yurek
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-11-23 00:00:00 -05:00
18
+ date: 2010-12-14 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency