Permalink
Browse files

More progress on add_style

  • Loading branch information...
1 parent 6decb1e commit 29b497b2e3bf94f22c8c6b12bd725e3eb8736850 @mike-burns mike-burns committed with jyurek Jul 16, 2012
@@ -46,7 +46,7 @@ class User < ActiveRecord::Base
user.should_not be_nil
visit "/users/#{user.to_param}"
- page.source =~ %r{img alt="5k" src="([^"]+)?.*"}
+ page.source =~ %r{img alt="5k" src="/([^"]+)\?.*"}
image_path = $1
image_path.should_not be_blank
@@ -68,7 +68,7 @@ class #{migration_name.classify} < ActiveRecord::Migration
end
When /^I run the down database migration$/ do
- migration_filename = Dir[Rails.root.join('db', 'migrate', '*')].last
+ migration_filename = Dir[Rails.root.join('db', 'migrate', '*')].sort.last
migration_filename =~ %r{.*/(\d+)_[^/]+.rb}
version = $1
version.should_not be_blank
@@ -80,7 +80,7 @@ class #{migration_name.classify} < ActiveRecord::Migration
user.should_not be_nil
visit "/users/#{user.to_param}"
- page.source =~ %r{img alt="5k" src="([^"]+)?.*"}
+ page.source =~ %r{img alt="5k" src="/([^"]+)\?.*"}
image_path = $1
image_path.should_not be_blank
View
@@ -8,4 +8,13 @@
Before do
@aruba_timeout_seconds = 120
+
+ if ENV['DEBUG']
+ @puts = true
+ @announce_stdout = true
+ @announce_stderr = true
+ @announce_cmd = true
+ @announce_dir = true
+ @announce_env = true
+ end
end
@@ -1,6 +1,40 @@
+require 'active_support/inflector'
+
class StyleAdder
def self.run(table_name, attachment_name, styles)
- model = table_name.to_s.???
- new(model, attachment_name, styles).run
+ new(model_class(table_name), attachment_name, styles).run
+ end
+
+ def initialize(model_class, attachment_name, styles)
+ @model_class = model_class
+ @attachment_name = attachment_name
+ @styles = styles
+ end
+
+ def run
+ p @model_class
+ p @model_class.all
+ @model_class.find_each do |model|
+ attachment = model.send(@attachment_name)
+
+ file = Paperclip.io_adapters.for(attachment)
+ attachment.instance_variable_set('@queued_for_write', {:original => file})
+
+ attachment.send(:post_process, *@styles.keys)
+
+ model.save
+ end
+ end
+
+ private
+
+ def self.model_class(table_name)
+ model_class_name = table_name.to_s.singularize.camelize
+ begin
+ model_class_name.constantize
+ rescue NameError
+ p "here"
+ raise ArgumentError, "found no model named #{model_class_name}"
+ end
end
end
View
@@ -30,6 +30,7 @@ class AttachmentTest < Test::Unit::TestCase
end
should "process :original style first" do
+ rebuild_model
file = File.new(fixture_file("50x50.png"), 'rb')
rebuild_class :styles => { :small => '100x>', :original => '42x42#' }
dummy = Dummy.new
@@ -43,6 +44,7 @@ class AttachmentTest < Test::Unit::TestCase
end
should "not delete styles that don't get reprocessed" do
+ rebuild_model
file = File.new(fixture_file("50x50.png"), 'rb')
rebuild_class :styles => { :small => '100x>',
:large => '500x>',
@@ -1358,5 +1360,4 @@ def do_after_all; end
assert_file_not_exists(@path)
end
end
-
end
View
@@ -199,7 +199,10 @@ def teardown
context '#add_style' do
should 'process the specific style' do
- rebuild_class thumbnail: '24x24', large: '124x124', processors: [:recording]
+ rebuild_model style: { thumbnail: '24x24', large: '124x124' }, processors: [:recording]
+ dummy = Dummy.new
+ dummy.avatar = File.new(fixture_file("50x50.png"), 'rb')
+ dummy.save
Dummy.connection.add_style :dummies, :avatar, large: '124x124'
View
@@ -0,0 +1,23 @@
+require './test/helper'
+require 'paperclip/style_adder'
+
+class StyleAdderTest < Test::Unit::TestCase
+ should 'process the specific style' do
+ Dummy = rebuild_model styles: { thumbnail: '24x24' }, processors: [:recording]
+ dummy = Dummy.new
+ dummy.avatar = File.new(fixture_file("50x50.png"), 'rb')
+ dummy.save
+
+ Dummy.class_eval do
+ has_attached_file :avatar, styles: { thumbnail: '24x24', large: '124x124' }, processors: [:recording]
+ Paperclip.reset_duplicate_clash_check!
+ end
+
+ RecordingProcessor.clear
+
+ StyleAdder.run :dummies, :avatar, large: '124x124'
+
+ assert RecordingProcessor.has_processed?(large: '124x124')
+ assert !RecordingProcessor.has_processed?(thumbnail: '24x24')
+ end
+end
@@ -0,0 +1,22 @@
+class RecordingProcessor
+ def self.make(file, options, attachment)
+ @style_hashes ||= []
+ @style_hashes << options
+ File.new('/etc/passwd')
+ end
+
+ def self.clear
+ @style_hashes = []
+ end
+
+ def self.has_processed?(expected_style_hash)
+ expected_geometries = expected_style_hash.values
+ @style_hashes && @style_hashes.any? do |style_hash|
+ expected_geometries.include?(style_hash[:geometry])
+ end
+ end
+end
+
+Paperclip.configure do |c|
+ c.register_processor :recording, RecordingProcessor
+end

0 comments on commit 29b497b

Please sign in to comment.