Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

Commit

Permalink
More progress on add_style
Browse files Browse the repository at this point in the history
  • Loading branch information
mike-burns authored and Jon Yurek committed Nov 2, 2012
1 parent 6decb1e commit 29b497b
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 7 deletions.
6 changes: 3 additions & 3 deletions features/step_definitions/mvc_steps.rb
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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

Expand Down
9 changes: 9 additions & 0 deletions features/support/env.rb
Expand Up @@ -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
38 changes: 36 additions & 2 deletions lib/paperclip/style_adder.rb
@@ -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
3 changes: 2 additions & 1 deletion test/attachment_test.rb
Expand Up @@ -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
Expand All @@ -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>',
Expand Down Expand Up @@ -1358,5 +1360,4 @@ def do_after_all; end
assert_file_not_exists(@path)
end
end

end
5 changes: 4 additions & 1 deletion test/schema_test.rb
Expand Up @@ -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'

Expand Down
23 changes: 23 additions & 0 deletions test/style_adder_test.rb
@@ -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
22 changes: 22 additions & 0 deletions test/support/recording_processor.rb
@@ -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.