Commit cfdaea60 by jyurek

Added attachments method to assist automation.

git-svn-id: https://svn.thoughtbot.com/plugins/paperclip/trunk@211 7bbfaf0e-4d1d-0410-9690-a8bb5f8ef2aa
parent 0c4f5d47
...@@ -164,11 +164,9 @@ module Thoughtbot #:nodoc: ...@@ -164,11 +164,9 @@ module Thoughtbot #:nodoc:
def has_attached_file *attachment_names def has_attached_file *attachment_names
options = attachment_names.last.is_a?(Hash) ? attachment_names.pop : {} options = attachment_names.last.is_a?(Hash) ? attachment_names.pop : {}
options = DEFAULT_ATTACHMENT_OPTIONS.merge(options) options = DEFAULT_ATTACHMENT_OPTIONS.merge(options)
@attachment_names ||= []
@attachment_names += attachment_names
include InstanceMethods include InstanceMethods
attachments ||= {} attachments = (@attachments ||= {})
attachment_names.each do |attr| attachment_names.each do |attr|
attachments[attr] = (attachments[attr] || {:name => attr}).merge(options) attachments[attr] = (attachments[attr] || {:name => attr}).merge(options)
...@@ -272,8 +270,12 @@ module Thoughtbot #:nodoc: ...@@ -272,8 +270,12 @@ module Thoughtbot #:nodoc:
end end
end end
def attachments def attachment_names
@attachment_names @attachments.keys
end
def attachment name
@attachments[name]
end end
# Adds errors if the attachments you specify are either missing or had errors on them. # Adds errors if the attachments you specify are either missing or had errors on them.
......
def obtain_class
class_name = ENV['CLASS'] || ENV['class']
@klass = eval(class_name)
end
def obtain_attachments
name = ENV['ATTACHMENT'] || ENV['attachment']
if name.blank? || @klass.attachment_names.include?(name)
[ name ]
else
@klass.attachment_names
end
end
namespace :paperclip do namespace :paperclip do
# desc "Regenerates thumbnails for a given CLASS (and optional ATTACHMENT)" desc "Regenerates thumbnails for a given CLASS (and optional ATTACHMENT)"
# task :refresh do task :refresh do
# klass = obtain_class klass = obtain_class
# attachment = ENV['ATTACHMENT'] instances = klass.find(:all)
# attachments = attachment.blank? ? klass.attachments : [attachment] names = obtain_attachments
# instances = klass.find(:all)
# puts "Regenerating thumbnails for #{instances.length} instances:" puts "Regenerating thumbnails for #{instances.length} instances:"
# instances.each do |instance| instances.each do |instance|
# attachments.each do |attach| names.each do |names|
# instance.send("process_#{attach}_thumbnails") instance.send("process_#{name}_thumbnails")
# end end
# print instance.save ? "." : "x"; $stdout.flush print instance.save ? "." : "x"; $stdout.flush
# end end
# end puts " Done."
end
# desc "Cleans out unused attachments for the given CLASS (and optional ATTACHMENT)" # desc "Cleans out unused attachments for the given CLASS (and optional ATTACHMENT)"
# task :clean do # task :clean do
# klass = obtain_class
# instances = klass.find(:all)
# names = obtain_attachments
#
# puts "Finding thumbnails for #{instances.length} instances:"
# files = instances.map do |instance|
# names.map do |name|
# styles = instance.attachment(name)[:thumbnails].keys
# styles << :original
# styles.map do |style|
# instance.send("#{name}_file_name", style)
# end
# end
# end
#
# pp files
# end # end
end end
\ No newline at end of file
...@@ -13,7 +13,7 @@ class PaperclipImagesTest < Test::Unit::TestCase ...@@ -13,7 +13,7 @@ class PaperclipImagesTest < Test::Unit::TestCase
end end
def test_should_supply_all_attachment_names def test_should_supply_all_attachment_names
assert_equal %w( image ), Foo.attachments.map{|a| a.to_s }.sort assert_equal %w( image ), Foo.attachment_names.map{|a| a.to_s }.sort
end end
def test_should_validate_before_save def test_should_validate_before_save
......
...@@ -13,7 +13,7 @@ class PaperclipNonStandardTest < Test::Unit::TestCase ...@@ -13,7 +13,7 @@ class PaperclipNonStandardTest < Test::Unit::TestCase
end end
def test_should_supply_all_attachment_names def test_should_supply_all_attachment_names
assert_equal %w( avatar resume ), NonStandard.attachments.map{|a| a.to_s }.sort assert_equal %w( avatar resume ), NonStandard.attachment_names.map{|a| a.to_s }.sort
end end
def test_should_validate_before_save def test_should_validate_before_save
......
...@@ -11,7 +11,7 @@ class PaperclipTest < Test::Unit::TestCase ...@@ -11,7 +11,7 @@ class PaperclipTest < Test::Unit::TestCase
end end
def test_should_supply_all_attachment_names def test_should_supply_all_attachment_names
assert_equal %w( document ), Bar.attachments.map{|a| a.to_s }.sort assert_equal %w( document ), Bar.attachment_names.map{|a| a.to_s }.sort
end end
def test_should_validate_before_save def test_should_validate_before_save
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment