public
Description: Treat an ActiveRecord model as a file attachment, storing its patch, size, content type, etc.
Homepage: http://weblog.techno-weenie.net
Clone URL: git://github.com/technoweenie/attachment_fu.git
Search Repo:
Click here to lend your support to: attachment_fu and make a donation at www.pledgie.com !
tests

git-svn-id: 
http://svn.techno-weenie.net/projects/plugins/attachment_fu@2549 
567b1171-46fb-0310-a4c9-b4bef9110e78
technoweenie (author)
Wed Dec 13 16:59:38 -0800 2006
commit  941233203ad0ddd85929e91625cfa98bdaaa334b
tree    c7a90c76f5f3a1e753383698f0b805469c91eee7
parent  1e91f6545a38dc16bf7ba35292841dbe8f860fb0
...
5
6
7
8
 
9
10
11
12
13
14
15
 
16
17
18
...
5
6
7
 
8
9
10
11
12
13
14
 
15
16
17
18
0
@@ -5,14 +5,14 @@
0
 desc 'Default: run unit tests.'
0
 task :default => :test
0
 
0
-desc 'Test the acts_as_attachment plugin.'
0
+desc 'Test the attachment_fu plugin.'
0
 Rake::TestTask.new(:test) do |t|
0
   t.libs << 'lib'
0
   t.pattern = 'test/**/*_test.rb'
0
   t.verbose = true
0
 end
0
 
0
-desc 'Generate documentation for the acts_as_attachment plugin.'
0
+desc 'Generate documentation for the attachment_fu plugin.'
0
 Rake::RDocTask.new(:rdoc) do |rdoc|
0
   rdoc.rdoc_dir = 'rdoc'
0
   rdoc.title = 'ActsAsAttachment'
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
0
@@ -1 +1,19 @@
0
+sqlite:
0
+ :adapter: sqlite
0
+ :dbfile: attachment_fu_plugin.sqlite.db
0
+sqlite3:
0
+ :adapter: sqlite3
0
+ :dbfile: attachment_fu_plugin.sqlite3.db
0
+postgresql:
0
+ :adapter: postgresql
0
+ :username: postgres
0
+ :password: postgres
0
+ :database: attachment_fu_plugin_test
0
+ :min_messages: ERROR
0
+mysql:
0
+ :adapter: mysql
0
+ :host: localhost
0
+ :username: rails
0
+ :password:
0
+ :database: attachment_fu_plugin_test
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
0
@@ -1 +1,85 @@
0
+class Attachment < ActiveRecord::Base
0
+ @@saves = 0
0
+ cattr_accessor :saves
0
+ has_attachment
0
+ validates_as_attachment
0
+ after_attachment_saved do |record|
0
+ self.saves += 1
0
+ end
0
+end
0
+
0
+class SmallAttachment < Attachment
0
+ has_attachment :max_size => 1.kilobyte
0
+end
0
+
0
+class BigAttachment < Attachment
0
+ has_attachment :size => 1.megabyte..2.megabytes
0
+end
0
+
0
+class PdfAttachment < Attachment
0
+ has_attachment :content_type => 'pdf'
0
+end
0
+
0
+class DocAttachment < Attachment
0
+ has_attachment :content_type => %w(pdf doc txt)
0
+end
0
+
0
+class ImageAttachment < Attachment
0
+ has_attachment :content_type => :image, :resize_to => [50,50]
0
+end
0
+
0
+class ImageOrPdfAttachment < Attachment
0
+ has_attachment :content_type => ['pdf', :image], :resize_to => 'x50'
0
+end
0
+
0
+class ImageWithThumbsAttachment < Attachment
0
+ has_attachment :thumbnails => { :thumb => [50, 50], :geometry => 'x50' }, :resize_to => [55,55]
0
+ after_resize do |record, img|
0
+ record.aspect_ratio = img.columns.to_f / img.rows.to_f
0
+ end
0
+end
0
+
0
+class FileAttachment < ActiveRecord::Base
0
+ has_attachment :file_system_path => 'vendor/plugins/attachment_fu/test/files'
0
+ validates_as_attachment
0
+end
0
+
0
+class ImageFileAttachment < FileAttachment
0
+ has_attachment :file_system_path => 'vendor/plugins/attachment_fu/test/files',
0
+ :content_type => :image, :resize_to => [50,50]
0
+end
0
+
0
+class ImageWithThumbsFileAttachment < FileAttachment
0
+ has_attachment :file_system_path => 'vendor/plugins/attachment_fu/test/files',
0
+ :thumbnails => { :thumb => [50, 50], :geometry => 'x50' }, :resize_to => [55,55]
0
+ after_resize do |record, img|
0
+ record.aspect_ratio = img.columns.to_f / img.rows.to_f
0
+ end
0
+end
0
+
0
+class ImageWithThumbsClassFileAttachment < FileAttachment
0
+ has_attachment :file_system_path => 'vendor/plugins/attachment_fu/test/files',
0
+ :thumbnails => { :thumb => [50, 50] }, :resize_to => [55,55],
0
+ :thumbnail_class => 'ImageThumbnail'
0
+end
0
+
0
+class ImageThumbnail < FileAttachment
0
+ has_attachment :file_system_path => 'vendor/plugins/attachment_fu/test/files/thumbnails'
0
+end
0
+
0
+# no parent
0
+class OrphanAttachment < ActiveRecord::Base
0
+ has_attachment
0
+ validates_as_attachment
0
+end
0
+
0
+# no filename, no size, no content_type
0
+class MinimalAttachment < ActiveRecord::Base
0
+ has_attachment :file_system_path => 'vendor/plugins/attachment_fu/test/files'
0
+ validates_as_attachment
0
+
0
+ def filename
0
+ "#{id}.file"
0
+ end
0
+end
...
 
...
1
0
@@ -1 +1,2 @@
0
+foo
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
0
@@ -1 +1,42 @@
0
+ActiveRecord::Schema.define(:version => 0) do
0
+ create_table :attachments, :force => true do |t|
0
+ t.column :db_file_id, :integer
0
+ t.column :parent_id, :integer
0
+ t.column :thumbnail, :string
0
+ t.column :filename, :string, :limit => 255
0
+ t.column :content_type, :string, :limit => 255
0
+ t.column :size, :integer
0
+ t.column :width, :integer
0
+ t.column :height, :integer
0
+ t.column :aspect_ratio, :float
0
+ end
0
+
0
+ create_table :file_attachments, :force => true do |t|
0
+ t.column :parent_id, :integer
0
+ t.column :thumbnail, :string
0
+ t.column :filename, :string, :limit => 255
0
+ t.column :content_type, :string, :limit => 255
0
+ t.column :size, :integer
0
+ t.column :width, :integer
0
+ t.column :height, :integer
0
+ t.column :type, :string
0
+ t.column :aspect_ratio, :float
0
+ end
0
+
0
+ create_table :orphan_attachments, :force => true do |t|
0
+ t.column :db_file_id, :integer
0
+ t.column :filename, :string, :limit => 255
0
+ t.column :content_type, :string, :limit => 255
0
+ t.column :size, :integer
0
+ end
0
+
0
+ create_table :minimal_attachments, :force => true do |t|
0
+ t.column :size, :integer
0
+ t.column :content_type, :string, :limit => 255
0
+ end
0
+
0
+ create_table :db_files, :force => true do |t|
0
+ t.column :data, :binary
0
+ end
0
+end
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
0
@@ -1 +1,104 @@
0
+$:.unshift(File.dirname(__FILE__) + '/../lib')
0
+
0
+require 'test/unit'
0
+require File.expand_path(File.join(File.dirname(__FILE__), '../../../../config/environment.rb'))
0
+require 'breakpoint'
0
+require 'active_record/fixtures'
0
+require 'action_controller/test_process'
0
+
0
+config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
0
+ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
0
+ActiveRecord::Base.establish_connection(config[ENV['DB'] || 'sqlite'])
0
+
0
+load(File.dirname(__FILE__) + "/schema.rb")
0
+
0
+Test::Unit::TestCase.fixture_path = File.dirname(__FILE__) + "/fixtures/"
0
+$LOAD_PATH.unshift(Test::Unit::TestCase.fixture_path)
0
+
0
+class Test::Unit::TestCase #:nodoc:
0
+ include ActionController::TestProcess
0
+ def create_fixtures(*table_names)
0
+ if block_given?
0
+ Fixtures.create_fixtures(Test::Unit::TestCase.fixture_path, table_names) { yield }
0
+ else
0
+ Fixtures.create_fixtures(Test::Unit::TestCase.fixture_path, table_names)
0
+ end
0
+ end
0
+
0
+ def setup
0
+ FileUtils.rm_rf File.join(File.dirname(__FILE__), 'files')
0
+ attachment_model self.class.attachment_model
0
+ end
0
+
0
+ self.use_transactional_fixtures = true
0
+ self.use_instantiated_fixtures = false
0
+
0
+ def self.attachment_model(klass = nil)
0
+ @attachment_model = klass if klass
0
+ @attachment_model
0
+ end
0
+
0
+ def self.test_against_class(test_method, klass, subclass = false)
0
+ define_method("#{test_method}_on_#{:sub if subclass}class") do
0
+ klass = Class.new(klass) if subclass
0
+ attachment_model klass
0
+ send test_method, klass
0
+ end
0
+ end
0
+
0
+ def self.test_against_subclass(test_method, klass)
0
+ test_against_class test_method, klass, true
0
+ end
0
+
0
+ protected
0
+ def upload_file(options = {})
0
+ att = attachment_model.create :uploaded_data => fixture_file_upload(options[:filename], options[:content_type] || 'image/png')
0
+ att.reload unless att.new_record?
0
+ att
0
+ end
0
+
0
+ def assert_created(num = 1)
0
+ assert_difference attachment_model.base_class, :count, num do
0
+ if attachment_model.included_modules.include? DbFile
0
+ assert_difference DbFile, :count, num do
0
+ yield
0
+ end
0
+ else
0
+ yield
0
+ end
0
+ end
0
+ end
0
+
0
+ def assert_not_created
0
+ assert_created(0) { yield }
0
+ end
0
+
0
+ def should_reject_by_size_with(klass)
0
+ attachment_model klass
0
+ assert_not_created do
0
+ attachment = upload_file :filename => '/files/rails.png'
0
+ assert attachment.new_record?
0
+ assert attachment.errors.on(:size)
0
+ assert_nil attachment.db_file if attachment.respond_to?(:db_file)
0
+ end
0
+ end
0
+
0
+ def assert_difference(object, method = nil, difference = 1)
0
+ initial_value = object.send(method)
0
+ yield
0
+ assert_equal initial_value + difference, object.send(method)
0
+ end
0
+
0
+ def assert_no_difference(object, method, &block)
0
+ assert_difference object, method, 0, &block
0
+ end
0
+
0
+ def attachment_model(klass = nil)
0
+ @attachment_model = klass if klass
0
+ @attachment_model
0
+ end
0
+end
0
+
0
+require File.join(File.dirname(__FILE__), 'fixtures/attachment')
0
+require File.join(File.dirname(__FILE__), 'fixtures/base_attachment_tests')

Comments

    No one has commented yet.