Browse files

add_style works

  • Loading branch information...
1 parent 29b497b commit 59b2c69fe6165a3c08acee0585de48e8878eb00a @mike-burns mike-burns committed with jyurek Jul 17, 2012
Showing with 42 additions and 9 deletions.
  1. +23 −5 lib/paperclip/style_adder.rb
  2. +19 −4 test/schema_test.rb
View
28 lib/paperclip/style_adder.rb
@@ -12,15 +12,13 @@ def initialize(model_class, attachment_name, styles)
end
def run
- p @model_class
- p @model_class.all
@model_class.find_each do |model|
- attachment = model.send(@attachment_name)
+ attachment = attachment_for(model)
file = Paperclip.io_adapters.for(attachment)
attachment.instance_variable_set('@queued_for_write', {:original => file})
- attachment.send(:post_process, *@styles.keys)
+ attachment.send(:post_process, *styles_for(attachment))
model.save
end
@@ -33,8 +31,28 @@ def self.model_class(table_name)
begin
model_class_name.constantize
rescue NameError
- p "here"
raise ArgumentError, "found no model named #{model_class_name}"
end
end
+
+ def attachment_for(model)
+ begin
+ model.send(@attachment_name)
+ rescue NoMethodError
+ raise ArgumentError, "found no attachment named #{@attachment_name} on #{model}"
+ end
+ end
+
+ def styles_for(attachment)
+ expected_styles = attachment.send(:styles).keys
+ if subset?(@styles.keys, expected_styles)
+ @styles.keys
+ else
+ raise ArgumentError, "unsupported styles; excepted any of #{expected_styles}"
+ end
+ end
+
+ def subset?(smaller, larger)
+ (smaller - larger).empty?
+ end
end
View
23 test/schema_test.rb
@@ -6,10 +6,12 @@ class SchemaTest < Test::Unit::TestCase
include ActiveSupport::Testing::Deprecation
def setup
+ super
rebuild_class
end
def teardown
+ super
Dummy.connection.drop_table :dummies rescue nil
end
@@ -199,10 +201,7 @@ def teardown
context '#add_style' do
should 'process the specific style' do
- rebuild_model style: { thumbnail: '24x24', large: '124x124' }, processors: [:recording]
- dummy = Dummy.new
- dummy.avatar = File.new(fixture_file("50x50.png"), 'rb')
- dummy.save
+ dummy_with_avatar(thumbnail: '24x24', large: '124x124')
Dummy.connection.add_style :dummies, :avatar, large: '124x124'
@@ -211,12 +210,16 @@ def teardown
end
should 'raise if the style is missing' do
+ dummy_with_avatar(thumbnail: '24x24')
+
assert_raise ArgumentError do
Dummy.connection.add_style :dummies, :avatar, missing_style: '24x24'
end
end
should 'raise if the attachment is missing' do
+ dummy_with_avatar(thumbnail: '24x24')
+
assert_raise ArgumentError do
Dummy.connection.add_style :dummies, :missng_attachment, thumbnail: '24x24'
end
@@ -229,4 +232,16 @@ def teardown
end
end
end
+
+ def dummy_with_avatar(styles)
+ rebuild_model styles: styles, processors: [:recording]
+
+ file = File.new(fixture_file("50x50.png"), 'rb')
+ dummy = Dummy.new
+ dummy.avatar = file
+ dummy.save
+ file.close
+
+ RecordingProcessor.clear
+ end
end

0 comments on commit 59b2c69

Please sign in to comment.