Skip to content
This repository has been archived by the owner on Sep 9, 2019. It is now read-only.

Commit

Permalink
On the way to good-code-dom
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.thoughtbot.com/plugins/paperclip/trunk@241 7bbfaf0e-4d1d-0410-9690-a8bb5f8ef2aa
  • Loading branch information
jyurek committed Nov 9, 2007
1 parent d68a942 commit 26444ce
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
4 changes: 2 additions & 2 deletions init.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
require File.join(File.dirname(__FILE__), "lib", "paperclip")
ActiveRecord::Base.extend( Thoughtbot::Paperclip::ClassMethods )
File.send :include, Thoughtbot::Paperclip::Upfile
ActiveRecord::Base.extend( Paperclip::ClassMethods )
File.send :include, Paperclip::Upfile
18 changes: 11 additions & 7 deletions lib/paperclip.rb
Original file line number Diff line number Diff line change
Expand Up @@ -395,11 +395,12 @@ def has_attached_file *attachment_names
options = attachment_names.last.is_a?(Hash) ? attachment_names.pop : {}

include InstanceMethods
class_inheritable_hash :attachment_definitions
#class_inheritable_hash :attachment_definitions
@attachment_definitions ||= {}

attachment_names.each do |aname|
whine_about_columns_for aname
self.attachment_definitions[aname] = AttachmentDefinition.new(aname, options)
@attachment_definitions[aname] = AttachmentDefinition.new(aname, options)

define_method aname do
attachment_for(aname)
Expand All @@ -412,7 +413,11 @@ def has_attached_file *attachment_names
end

def attached_files
attachment_definitions.keys
@attachment_definitions.keys
end

def attachment_definition_for attachment
@attachment_definitions[attachment]
end

# Adds errors if the attachments you specify are either missing or had errors on them.
Expand All @@ -426,10 +431,9 @@ def validates_attached_file *attachment_names
end

def whine_about_columns_for attachment #:nodoc:
name = attachment[:name]
unless column_names.include?("#{name}_file_name") && column_names.include?("#{name}_content_type")
error = "Class #{self.name} does not have the necessary columns to have an attachment named #{name}. " +
"(#{name}_file_name and #{name}_content_type)"
unless column_names.include?("#{attachment}_file_name") && column_names.include?("#{attachment}_content_type")
error = "Class #{name} does not have the necessary columns to have an attachment named #{attachment}. " +
"(#{attachment}_file_name and #{attachment}_content_type)"
raise PaperclipError, error
end
end
Expand Down
4 changes: 3 additions & 1 deletion test/paperclip_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'test/unit'
require File.dirname(__FILE__) + "/test_helper.rb"
require File.dirname(__FILE__) + "/simply_shoulda.rb"
require File.dirname(__FILE__) + "/../lib/paperclip-c.rb"
require File.dirname(__FILE__) + "/../init.rb"

class PaperclipTest < Test::Unit::TestCase

Expand Down Expand Up @@ -29,6 +29,7 @@ class PaperclipTest < Test::Unit::TestCase
table.column :document_content_type, :string
table.column :document_file_size, :integer
end
Object.send(:remove_const, :Foo) rescue nil
class ::Foo < ActiveRecord::Base; end
end

Expand All @@ -51,6 +52,7 @@ class ::Foo < ActiveRecord::Base; end
should "be able to set options on attachments" do
assert Foo.has_attached_file :image, :thumbnails => {:thumb => "100x100"}
assert_equal [:image], Foo.attached_files
assert_equal( {:thumb => "100x100"}, Foo.attachment_definition_for(:image).thumbnails )
end
end

Expand Down

0 comments on commit 26444ce

Please sign in to comment.