Permalink
Browse files

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

… migration class
  • Loading branch information...
1 parent 088c88d commit 876582ad892877c1943b841e211b642502803990 @mike-burns mike-burns committed with jyurek Jul 30, 2012
Showing with 27 additions and 18 deletions.
  1. +17 −2 lib/paperclip/schema.rb
  2. +5 −14 lib/paperclip/style_migration.rb
  3. +3 −1 test/style_adder_test.rb
  4. +2 −1 test/style_remover_test.rb
View
@@ -47,11 +47,26 @@ def drop_attached_file(*args)
end
def add_style(table_name, attachment_name, styles)
- StyleAdder.run(table_name, attachment_name, styles)
+ StyleAdder.run(model_enumerator(table_name), attachment_name, styles)
end
def remove_style(table_name, attachment_name, style_name)
- StyleRemover.run(table_name, attachment_name, style_name)
+ StyleRemover.run(model_enumerator(table_name), attachment_name, style_name)
+ end
+
+ private
+
+ def model_enumerator(table_name)
+ model_class(table_name).enum_for(:find_each)
+ end
+
+ def model_class(table_name)
+ model_class_name = table_name.to_s.singularize.camelize
+ begin
+ model_class_name.constantize
+ rescue NameError
+ raise ArgumentError, "found no model named #{model_class_name}"
+ end
end
end
@@ -2,28 +2,19 @@
module Paperclip
class StyleMigration
- def self.run(table_name, attachment_name, *style_info)
- new(model_class(table_name), attachment_name).run(*style_info)
+ def self.run(model_enumerator, attachment_name, *style_info)
+ new(model_enumerator, attachment_name).run(*style_info)
end
- def initialize(model_class, attachment_name)
- @model_class = model_class
+ def initialize(model_enumerator, attachment_name)
+ @model_enumerator = model_enumerator
@attachment_name = attachment_name
end
protected
- def self.model_class(table_name)
- model_class_name = table_name.to_s.singularize.camelize
- begin
- model_class_name.constantize
- rescue NameError
- raise ArgumentError, "found no model named #{model_class_name}"
- end
- end
-
def each_attachment(&block)
- @model_class.find_each do |model|
+ @model_enumerator.each do |model|
block.call(attachment_for(model))
end
end
View
@@ -17,9 +17,11 @@ class StyleAdderTest < Test::Unit::TestCase
Paperclip.reset_duplicate_clash_check!
end
+ dummy_enumerator = Dummy.all
+
RecordingProcessor.clear
- Paperclip::StyleAdder.run :dummies, :avatar, large: '124x124'
+ Paperclip::StyleAdder.run dummy_enumerator, :avatar, large: '124x124'
assert RecordingProcessor.has_processed?(large: '124x124')
assert !RecordingProcessor.has_processed?(thumbnail: '24x24')
@@ -14,8 +14,9 @@ class StyleRemoverTest < Test::Unit::TestCase
large_path = dummy.avatar.path(:large)
original_path = dummy.avatar.path(:original)
+ dummy_enumerator = Dummy.all
- Paperclip::StyleRemover.run(:dummy, :avatar, :large)
+ Paperclip::StyleRemover.run dummy_enumerator, :avatar, :large
assert !File.exist?(large_path)
assert File.exist?(original_path)

0 comments on commit 876582a

Please sign in to comment.