Commit 710f658c by Mike Burns

wip: Paperclip::Tasks::Attachments

parent 40bdd3d5
......@@ -180,6 +180,7 @@ module Paperclip
options = attachment_definitions[name] = Paperclip::AttachmentOptions.new(options)
Paperclip.classes_with_attachments << self.name
Paperclip.check_for_path_clash(name, options[:path], self.name)
after_save { send(name).send(:save) }
before_destroy { send(name).send(:queue_all_for_delete) }
......@@ -187,6 +188,8 @@ module Paperclip
define_paperclip_callbacks :post_process, :"#{name}_post_process"
Paperclip::Tasks::Attachments.add(self, name, options)
define_method name do |*args|
ivar = "@attachment_#{name}"
attachment = instance_variable_get(ivar)
......
......@@ -38,7 +38,7 @@ module Paperclip
Hash.new.tap do |current_styles|
Paperclip.classes_with_attachments.each do |klass_name|
klass = Paperclip.class_for(klass_name)
klass.attachment_definitions.each do |attachment_name, attachment_attributes|
Paperclip::Tasks::Attachments.definitions_for(klass).each do |attachment_name, attachment_attributes|
# TODO: is it even possible to take into account Procs?
next if attachment_attributes[:styles].kind_of?(Proc)
attachment_attributes[:styles].try(:keys).try(:each) do |style_name|
......
......@@ -9,11 +9,17 @@ module Paperclip
def self.obtain_attachments(klass)
klass = Paperclip.class_for(klass.to_s)
name = ENV['ATTACHMENT'] || ENV['attachment']
raise "Class #{klass.name} has no attachments specified" unless klass.respond_to?(:attachment_definitions)
if !name.blank? && klass.attachment_definitions.keys.map(&:to_s).include?(name.to_s)
attachment_names = Paperclip::Tasks::Attachments.names_for(klass)
if attachment_names.empty?
raise "Class #{klass.name} has no attachments specified"
end
if !name.blank? && attachment_names.map(&:to_s).include?(name.to_s)
[ name ]
else
klass.attachment_definitions.keys
attachment_names
end
end
end
......
......@@ -159,11 +159,6 @@ class PaperclipTest < Test::Unit::TestCase
end
end
should "be able to see the attachment definition from the subclass's class" do
assert_equal "tmp/:class/omg/:style.:extension",
SubDummy.attachment_definitions[:avatar][:path]
end
teardown do
SubDummy.delete_all
Object.send(:remove_const, "SubDummy") rescue nil
......
require './test/helper'
require 'tasks/attachments'
class AttachmentsTest < Test::Unit::TestCase
context '.names_for' do
should 'include attachment names for the given class' do
Foo = Class.new
Paperclip::Tasks::Attachments.add(Foo, :avatar, {})
assert_equal [:avatar], Paperclip::Tasks::Attachments.names_for(Foo)
end
should 'not include attachment names for other classes' do
Foo = Class.new
Bar = Class.new
Paperclip::Tasks::Attachments.add(Foo, :avatar, {})
Paperclip::Tasks::Attachments.add(Bar, :lover, {})
assert_equal [:lover], Paperclip::Tasks::Attachments.names_for(Bar)
end
end
context '.definitions_for' do
end
end
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