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

Commit

Permalink
Push attachment definitions into a list of attachments for the rake task
Browse files Browse the repository at this point in the history
  • Loading branch information
mike-burns committed Jun 14, 2013
1 parent 710f658 commit d687fca
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 9 deletions.
1 change: 1 addition & 0 deletions lib/paperclip.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
require 'paperclip/validators'
require 'paperclip/logger'
require 'paperclip/helpers'
require 'paperclip/tasks/attachments'
require 'mime/types'
require 'logger'
require 'cocaine'
Expand Down
3 changes: 2 additions & 1 deletion lib/paperclip/missing_attachment_styles.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

require 'paperclip/tasks/attachments'
require 'set'

module Paperclip
class << self
attr_accessor :classes_with_attachments
Expand Down
35 changes: 35 additions & 0 deletions lib/paperclip/tasks/attachments.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions lib/tasks/paperclip.rake
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'paperclip/tasks/attachments'

module Paperclip
module Task
def self.obtain_class
Expand Down
28 changes: 20 additions & 8 deletions test/tasks/attachments_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit d687fca

Please sign in to comment.