From d687fca146b507753eb4627207d0a76e9df869d2 Mon Sep 17 00:00:00 2001 From: Mike Burns Date: Fri, 25 Jan 2013 11:29:56 +0100 Subject: [PATCH] Push attachment definitions into a list of attachments for the rake task --- lib/paperclip.rb | 1 + lib/paperclip/missing_attachment_styles.rb | 3 +- lib/paperclip/tasks/attachments.rb | 35 ++++++++++++++++++++++ lib/tasks/paperclip.rake | 2 ++ test/tasks/attachments_test.rb | 28 ++++++++++++----- 5 files changed, 60 insertions(+), 9 deletions(-) create mode 100644 lib/paperclip/tasks/attachments.rb diff --git a/lib/paperclip.rb b/lib/paperclip.rb index de834a56b..a3c7e213b 100644 --- a/lib/paperclip.rb +++ b/lib/paperclip.rb @@ -51,6 +51,7 @@ require 'paperclip/validators' require 'paperclip/logger' require 'paperclip/helpers' +require 'paperclip/tasks/attachments' require 'mime/types' require 'logger' require 'cocaine' diff --git a/lib/paperclip/missing_attachment_styles.rb b/lib/paperclip/missing_attachment_styles.rb index 549f3f4c0..23afce111 100644 --- a/lib/paperclip/missing_attachment_styles.rb +++ b/lib/paperclip/missing_attachment_styles.rb @@ -1,5 +1,6 @@ - +require 'paperclip/tasks/attachments' require 'set' + module Paperclip class << self attr_accessor :classes_with_attachments diff --git a/lib/paperclip/tasks/attachments.rb b/lib/paperclip/tasks/attachments.rb new file mode 100644 index 000000000..fefe41def --- /dev/null +++ b/lib/paperclip/tasks/attachments.rb @@ -0,0 +1,35 @@ +require 'singleton' + +module Paperclip + module Tasks + class Attachments + include Singleton + + def self.add(klass, attachment_name, attachment_options) + instance.add(klass, attachment_name, attachment_options) + end + + def self.names_for(klass) + instance.names_for(klass) + end + + def self.definitions_for(klass) + instance.definitions_for(klass) + end + + def add(klass, attachment_name, attachment_options) + @attachments ||= {} + @attachments[klass] ||= {} + @attachments[klass][attachment_name] = attachment_options + end + + def names_for(klass) + @attachments[klass].keys + end + + def definitions_for(klass) + @attachments[klass] + end + end + end +end diff --git a/lib/tasks/paperclip.rake b/lib/tasks/paperclip.rake index 68b4c8213..cbe40e9d4 100644 --- a/lib/tasks/paperclip.rake +++ b/lib/tasks/paperclip.rake @@ -1,3 +1,5 @@ +require 'paperclip/tasks/attachments' + module Paperclip module Task def self.obtain_class diff --git a/test/tasks/attachments_test.rb b/test/tasks/attachments_test.rb index c62e13772..8408461c7 100644 --- a/test/tasks/attachments_test.rb +++ b/test/tasks/attachments_test.rb @@ -4,23 +4,35 @@ 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, {}) + foo = Class.new + Paperclip::Tasks::Attachments.add(foo, :avatar, {}) - assert_equal [:avatar], Paperclip::Tasks::Attachments.names_for(Foo) + 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, {}) + 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) + assert_equal [:lover], Paperclip::Tasks::Attachments.names_for(bar) end end context '.definitions_for' do + should 'produce the attachment name and options' do + expected_definitions = { + avatar: { yo: 'greeting' }, + greeter: { ciao: 'greeting' } + } + foo = Class.new + Paperclip::Tasks::Attachments.add(foo, :avatar, { yo: 'greeting' }) + Paperclip::Tasks::Attachments.add(foo, :greeter, { ciao: 'greeting' }) + definitions = Paperclip::Tasks::Attachments.definitions_for(foo) + + assert_equal expected_definitions, definitions + end end end