Skip to content

Commit

Permalink
Use an Enumerator to move the ActiveRecord dependency into the schema…
Browse files Browse the repository at this point in the history
… migration class
  • Loading branch information
mike-burns committed Jul 30, 2012
1 parent 41eeb4f commit a83766f
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 18 deletions.
19 changes: 17 additions & 2 deletions lib/paperclip/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
19 changes: 5 additions & 14 deletions lib/paperclip/style_migration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion test/style_adder_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
3 changes: 2 additions & 1 deletion test/style_remover_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit a83766f

Please sign in to comment.