Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Use an Enumerator to move the ActiveRecord dependency into the schema…

… migration class
  • Loading branch information...
commit e9ed374f616cb1ae0ac6b845d76c92445722f7e6 1 parent 7fb2a81
Mike Burns authored July 30, 2012 jyurek committed November 02, 2012
19  lib/paperclip/schema.rb
@@ -47,11 +47,26 @@ def drop_attached_file(*args)
47 47
       end
48 48
 
49 49
       def add_style(table_name, attachment_name, styles)
50  
-        StyleAdder.run(table_name, attachment_name, styles)
  50
+        StyleAdder.run(model_enumerator(table_name), attachment_name, styles)
51 51
       end
52 52
 
53 53
       def remove_style(table_name, attachment_name, style_name)
54  
-        StyleRemover.run(table_name, attachment_name, style_name)
  54
+        StyleRemover.run(model_enumerator(table_name), attachment_name, style_name)
  55
+      end
  56
+
  57
+      private
  58
+
  59
+      def model_enumerator(table_name)
  60
+        model_class(table_name).enum_for(:find_each)
  61
+      end
  62
+
  63
+      def model_class(table_name)
  64
+        model_class_name = table_name.to_s.singularize.camelize
  65
+        begin
  66
+          model_class_name.constantize
  67
+        rescue NameError
  68
+          raise ArgumentError, "found no model named #{model_class_name}"
  69
+        end
55 70
       end
56 71
     end
57 72
 
19  lib/paperclip/style_migration.rb
@@ -2,28 +2,19 @@
2 2
 
3 3
 module Paperclip
4 4
   class StyleMigration
5  
-    def self.run(table_name, attachment_name, *style_info)
6  
-      new(model_class(table_name), attachment_name).run(*style_info)
  5
+    def self.run(model_enumerator, attachment_name, *style_info)
  6
+      new(model_enumerator, attachment_name).run(*style_info)
7 7
     end
8 8
 
9  
-    def initialize(model_class, attachment_name)
10  
-      @model_class = model_class
  9
+    def initialize(model_enumerator, attachment_name)
  10
+      @model_enumerator = model_enumerator
11 11
       @attachment_name = attachment_name
12 12
     end
13 13
 
14 14
     protected
15 15
 
16  
-    def self.model_class(table_name)
17  
-      model_class_name = table_name.to_s.singularize.camelize
18  
-      begin
19  
-        model_class_name.constantize
20  
-      rescue NameError
21  
-        raise ArgumentError, "found no model named #{model_class_name}"
22  
-      end
23  
-    end
24  
-
25 16
     def each_attachment(&block)
26  
-      @model_class.find_each do |model|
  17
+      @model_enumerator.each do |model|
27 18
         block.call(attachment_for(model))
28 19
       end
29 20
     end
4  test/style_adder_test.rb
@@ -17,9 +17,11 @@ class StyleAdderTest < Test::Unit::TestCase
17 17
       Paperclip.reset_duplicate_clash_check!
18 18
     end
19 19
 
  20
+    dummy_enumerator = Dummy.all
  21
+
20 22
     RecordingProcessor.clear
21 23
 
22  
-    Paperclip::StyleAdder.run :dummies, :avatar, large: '124x124'
  24
+    Paperclip::StyleAdder.run dummy_enumerator, :avatar, large: '124x124'
23 25
 
24 26
     assert RecordingProcessor.has_processed?(large: '124x124')
25 27
     assert !RecordingProcessor.has_processed?(thumbnail: '24x24')
3  test/style_remover_test.rb
@@ -14,8 +14,9 @@ class StyleRemoverTest < Test::Unit::TestCase
14 14
 
15 15
     large_path = dummy.avatar.path(:large)
16 16
     original_path = dummy.avatar.path(:original)
  17
+    dummy_enumerator = Dummy.all
17 18
 
18  
-    Paperclip::StyleRemover.run(:dummy, :avatar, :large)
  19
+    Paperclip::StyleRemover.run dummy_enumerator, :avatar, :large
19 20
 
20 21
     assert !File.exist?(large_path)
21 22
     assert File.exist?(original_path)

0 notes on commit e9ed374

Please sign in to comment.
Something went wrong with that request. Please try again.