Permalink
Browse files

remove_style migration

  • Loading branch information...
mike-burns authored and jyurek committed Jul 17, 2012
1 parent 59b2c69 commit 18191cbd04467bf46ced9a49f34a3ef2cfdf99f9
View
@@ -1,5 +1,6 @@
require 'active_support/deprecation'
require 'paperclip/style_adder'
+require 'paperclip/style_remover'
module Paperclip
# Provides helper methods that can be used in migrations.
@@ -48,6 +49,10 @@ def drop_attached_file(*args)
def add_style(table_name, attachment_name, styles)
StyleAdder.run(table_name, attachment_name, styles)
end
+
+ def remove_style(table_name, attachment_name, style_name)
+ StyleRemover.run(table_name, attachment_name, style_name)
+ end
end
module TableDefinition
@@ -1,58 +1,32 @@
-require 'active_support/inflector'
+require 'paperclip/style_migration'
+require 'paperclip/style_adder'
-class StyleAdder
- def self.run(table_name, attachment_name, styles)
- 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
+module Paperclip
+ class StyleAdder < StyleMigration
+ def run(styles)
+ each_attachment do |attachment|
+ file = Paperclip.io_adapters.for(attachment)
+ attachment.instance_variable_set('@queued_for_write', {:original => file})
- def run
- @model_class.find_each do |model|
- attachment = attachment_for(model)
+ attachment.send(:post_process, *styles_for(attachment, styles))
- file = Paperclip.io_adapters.for(attachment)
- attachment.instance_variable_set('@queued_for_write', {:original => file})
-
- attachment.send(:post_process, *styles_for(attachment))
-
- model.save
+ attachment.save
+ end
end
- end
- private
+ private
- 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}"
+ def styles_for(attachment, styles)
+ 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
- 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}"
+ def subset?(smaller, larger)
+ (smaller - larger).empty?
end
end
-
- def subset?(smaller, larger)
- (smaller - larger).empty?
- end
end
@@ -0,0 +1,39 @@
+require 'active_support/inflector'
+
+module Paperclip
+ class StyleMigration
+ def self.run(table_name, attachment_name, *style_info)
+ new(model_class(table_name), attachment_name).run(*style_info)
+ end
+
+ def initialize(model_class, attachment_name)
+ @model_class = model_class
+ @attachment_name = attachment_name
+ end
+
+ private
+
+ 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|
+ block.call(attachment_for(model))
+ 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
+ end
+end
@@ -0,0 +1,13 @@
+require 'paperclip/style_migration'
+require 'paperclip/style_remover'
+
+module Paperclip
+ class StyleRemover < StyleMigration
+ def run(style_name)
+ each_attachment do |attachment|
+ attachment.clear(style_name)
+ attachment.save
+ end
+ end
+ end
+end
View
@@ -231,6 +231,19 @@ def teardown
end
end
end
+
+ context '#remove_style' do
+ should 'remove files for the specific style' do
+ dummy_with_avatar(large: '24x24')
+ large_path = Dummy.first.avatar.path(:large)
+ original_path = Dummy.first.avatar.path(:original)
+
+ Dummy.connection.remove_style :dummy, :avatar, :large
+
+ assert !File.exist?(large_path)
+ assert File.exist?(original_path)
+ end
+ end
end
def dummy_with_avatar(styles)
View
@@ -15,7 +15,7 @@ class StyleAdderTest < Test::Unit::TestCase
RecordingProcessor.clear
- StyleAdder.run :dummies, :avatar, large: '124x124'
+ Paperclip::StyleAdder.run :dummies, :avatar, large: '124x124'
assert RecordingProcessor.has_processed?(large: '124x124')
assert !RecordingProcessor.has_processed?(thumbnail: '24x24')
View
@@ -0,0 +1,19 @@
+require './test/helper'
+require 'paperclip/style_remover'
+
+class StyleRemoverTest < Test::Unit::TestCase
+ should 'process the specific style' do
+ Dummy = rebuild_model styles: { large: '24x24' }, processors: [:recording]
+ dummy = Dummy.new
+ dummy.avatar = File.new(fixture_file("50x50.png"), 'rb')
+ dummy.save
+
+ large_path = dummy.avatar.path(:large)
+ original_path = dummy.avatar.path(:original)
+
+ Paperclip::StyleRemover.run(:dummy, :avatar, :large)
+
+ assert !File.exist?(large_path)
+ assert File.exist?(original_path)
+ end
+end

0 comments on commit 18191cb

Please sign in to comment.