popel-attachment_fu 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (33) hide show
  1. data/README +200 -0
  2. data/VERSION.yml +4 -0
  3. data/lib/geometry.rb +93 -0
  4. data/lib/technoweenie/attachment_fu.rb +528 -0
  5. data/lib/technoweenie/attachment_fu/backends/db_file_backend.rb +39 -0
  6. data/lib/technoweenie/attachment_fu/backends/file_system_backend.rb +126 -0
  7. data/lib/technoweenie/attachment_fu/backends/s3_backend.rb +394 -0
  8. data/lib/technoweenie/attachment_fu/processors/core_image_processor.rb +59 -0
  9. data/lib/technoweenie/attachment_fu/processors/gd2_processor.rb +54 -0
  10. data/lib/technoweenie/attachment_fu/processors/image_science_processor.rb +61 -0
  11. data/lib/technoweenie/attachment_fu/processors/mini_magick_processor.rb +132 -0
  12. data/lib/technoweenie/attachment_fu/processors/rmagick_processor.rb +57 -0
  13. data/test/backends/db_file_test.rb +16 -0
  14. data/test/backends/file_system_test.rb +143 -0
  15. data/test/backends/remote/s3_test.rb +119 -0
  16. data/test/base_attachment_tests.rb +77 -0
  17. data/test/basic_test.rb +70 -0
  18. data/test/database.yml +18 -0
  19. data/test/extra_attachment_test.rb +67 -0
  20. data/test/fixtures/attachment.rb +215 -0
  21. data/test/fixtures/files/fake/rails.png +0 -0
  22. data/test/fixtures/files/foo.txt +1 -0
  23. data/test/fixtures/files/rails.png +0 -0
  24. data/test/geometry_test.rb +108 -0
  25. data/test/processors/core_image_test.rb +37 -0
  26. data/test/processors/gd2_test.rb +31 -0
  27. data/test/processors/image_science_test.rb +31 -0
  28. data/test/processors/mini_magick_test.rb +103 -0
  29. data/test/processors/rmagick_test.rb +255 -0
  30. data/test/schema.rb +121 -0
  31. data/test/test_helper.rb +150 -0
  32. data/test/validation_test.rb +55 -0
  33. metadata +95 -0
@@ -0,0 +1,121 @@
1
+ ActiveRecord::Schema.define(:version => 0) do
2
+ create_table :attachments, :force => true do |t|
3
+ t.column :db_file_id, :integer
4
+ t.column :parent_id, :integer
5
+ t.column :thumbnail, :string
6
+ t.column :filename, :string, :limit => 255
7
+ t.column :content_type, :string, :limit => 255
8
+ t.column :size, :integer
9
+ t.column :width, :integer
10
+ t.column :height, :integer
11
+ t.column :aspect_ratio, :float
12
+ end
13
+
14
+ create_table :file_attachments, :force => true do |t|
15
+ t.column :parent_id, :integer
16
+ t.column :thumbnail, :string
17
+ t.column :filename, :string, :limit => 255
18
+ t.column :content_type, :string, :limit => 255
19
+ t.column :size, :integer
20
+ t.column :width, :integer
21
+ t.column :height, :integer
22
+ t.column :type, :string
23
+ t.column :aspect_ratio, :float
24
+ end
25
+
26
+ create_table :file_attachments_with_string_id, :id => false, :force => true do |t|
27
+ t.column :id, :string
28
+ t.column :parent_id, :string
29
+ t.column :thumbnail, :string
30
+ t.column :filename, :string, :limit => 255
31
+ t.column :content_type, :string, :limit => 255
32
+ t.column :size, :integer
33
+ t.column :width, :integer
34
+ t.column :height, :integer
35
+ t.column :type, :string
36
+ t.column :aspect_ratio, :float
37
+ end
38
+
39
+ create_table :gd2_attachments, :force => true do |t|
40
+ t.column :parent_id, :integer
41
+ t.column :thumbnail, :string
42
+ t.column :filename, :string, :limit => 255
43
+ t.column :content_type, :string, :limit => 255
44
+ t.column :size, :integer
45
+ t.column :width, :integer
46
+ t.column :height, :integer
47
+ t.column :type, :string
48
+ end
49
+
50
+ create_table :image_science_attachments, :force => true do |t|
51
+ t.column :parent_id, :integer
52
+ t.column :thumbnail, :string
53
+ t.column :filename, :string, :limit => 255
54
+ t.column :content_type, :string, :limit => 255
55
+ t.column :size, :integer
56
+ t.column :width, :integer
57
+ t.column :height, :integer
58
+ t.column :type, :string
59
+ end
60
+
61
+ create_table :core_image_attachments, :force => true do |t|
62
+ t.column :parent_id, :integer
63
+ t.column :thumbnail, :string
64
+ t.column :filename, :string, :limit => 255
65
+ t.column :content_type, :string, :limit => 255
66
+ t.column :size, :integer
67
+ t.column :width, :integer
68
+ t.column :height, :integer
69
+ t.column :type, :string
70
+ end
71
+
72
+ create_table :mini_magick_attachments, :force => true do |t|
73
+ t.column :parent_id, :integer
74
+ t.column :thumbnail, :string
75
+ t.column :filename, :string, :limit => 255
76
+ t.column :content_type, :string, :limit => 255
77
+ t.column :size, :integer
78
+ t.column :width, :integer
79
+ t.column :height, :integer
80
+ t.column :type, :string
81
+ end
82
+
83
+ create_table :mini_magick_attachments, :force => true do |t|
84
+ t.column :parent_id, :integer
85
+ t.column :thumbnail, :string
86
+ t.column :filename, :string, :limit => 255
87
+ t.column :content_type, :string, :limit => 255
88
+ t.column :size, :integer
89
+ t.column :width, :integer
90
+ t.column :height, :integer
91
+ t.column :type, :string
92
+ end
93
+
94
+ create_table :orphan_attachments, :force => true do |t|
95
+ t.column :db_file_id, :integer
96
+ t.column :filename, :string, :limit => 255
97
+ t.column :content_type, :string, :limit => 255
98
+ t.column :size, :integer
99
+ end
100
+
101
+ create_table :minimal_attachments, :force => true do |t|
102
+ t.column :size, :integer
103
+ t.column :content_type, :string, :limit => 255
104
+ end
105
+
106
+ create_table :db_files, :force => true do |t|
107
+ t.column :data, :binary
108
+ end
109
+
110
+ create_table :s3_attachments, :force => true do |t|
111
+ t.column :parent_id, :integer
112
+ t.column :thumbnail, :string
113
+ t.column :filename, :string, :limit => 255
114
+ t.column :content_type, :string, :limit => 255
115
+ t.column :size, :integer
116
+ t.column :width, :integer
117
+ t.column :height, :integer
118
+ t.column :type, :string
119
+ t.column :aspect_ratio, :float
120
+ end
121
+ end
@@ -0,0 +1,150 @@
1
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
2
+
3
+ ENV['RAILS_ENV'] = 'test'
4
+ ENV['RAILS_ROOT'] ||= File.dirname(__FILE__) + '/../../../..'
5
+
6
+ require 'test/unit'
7
+ require File.expand_path(File.join(ENV['RAILS_ROOT'], 'config/environment.rb'))
8
+ require 'active_record/fixtures'
9
+ require 'action_controller/test_process'
10
+
11
+ config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
12
+ ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
13
+
14
+ db_adapter = ENV['DB']
15
+
16
+ # no db passed, try one of these fine config-free DBs before bombing.
17
+ db_adapter ||=
18
+ begin
19
+ require 'rubygems'
20
+ require 'sqlite'
21
+ 'sqlite'
22
+ rescue MissingSourceFile
23
+ begin
24
+ require 'sqlite3'
25
+ 'sqlite3'
26
+ rescue MissingSourceFile
27
+ end
28
+ end
29
+
30
+ if db_adapter.nil?
31
+ raise "No DB Adapter selected. Pass the DB= option to pick one, or install Sqlite or Sqlite3."
32
+ end
33
+
34
+ ActiveRecord::Base.establish_connection(config[db_adapter])
35
+
36
+ load(File.dirname(__FILE__) + "/schema.rb")
37
+
38
+ Test::Unit::TestCase.fixture_path = File.dirname(__FILE__) + "/fixtures"
39
+ $LOAD_PATH.unshift(Test::Unit::TestCase.fixture_path)
40
+
41
+ class Test::Unit::TestCase #:nodoc:
42
+ include ActionController::TestProcess
43
+ def create_fixtures(*table_names)
44
+ if block_given?
45
+ Fixtures.create_fixtures(Test::Unit::TestCase.fixture_path, table_names) { yield }
46
+ else
47
+ Fixtures.create_fixtures(Test::Unit::TestCase.fixture_path, table_names)
48
+ end
49
+ end
50
+
51
+ def setup
52
+ Attachment.saves = 0
53
+ DbFile.transaction { [Attachment, FileAttachment, OrphanAttachment, MinimalAttachment, DbFile].each { |klass| klass.delete_all } }
54
+ attachment_model self.class.attachment_model
55
+ end
56
+
57
+ def teardown
58
+ FileUtils.rm_rf File.join(File.dirname(__FILE__), 'files')
59
+ end
60
+
61
+ self.use_transactional_fixtures = true
62
+ self.use_instantiated_fixtures = false
63
+
64
+ def self.attachment_model(klass = nil)
65
+ @attachment_model = klass if klass
66
+ @attachment_model
67
+ end
68
+
69
+ def self.test_against_class(test_method, klass, subclass = false)
70
+ define_method("#{test_method}_on_#{:sub if subclass}class") do
71
+ klass = Class.new(klass) if subclass
72
+ attachment_model klass
73
+ send test_method, klass
74
+ end
75
+ end
76
+
77
+ def self.test_against_subclass(test_method, klass)
78
+ test_against_class test_method, klass, true
79
+ end
80
+
81
+ protected
82
+ def upload_file(options = {})
83
+ use_temp_file options[:filename] do |file|
84
+ att = attachment_model.create :uploaded_data => fixture_file_upload(file, options[:content_type] || 'image/png')
85
+ att.reload unless att.new_record?
86
+ return att
87
+ end
88
+ end
89
+
90
+ def upload_merb_file(options = {})
91
+ use_temp_file options[:filename] do |file|
92
+ att = attachment_model.create :uploaded_data => {"size" => file.size, "content_type" => options[:content_type] || 'image/png', "filename" => file, 'tempfile' => fixture_file_upload(file, options[:content_type] || 'image/png')}
93
+ att.reload unless att.new_record?
94
+ return att
95
+ end
96
+ end
97
+
98
+ def use_temp_file(fixture_filename)
99
+ temp_path = File.join('/tmp', File.basename(fixture_filename))
100
+ FileUtils.mkdir_p File.join(fixture_path, 'tmp')
101
+ FileUtils.cp File.join(fixture_path, fixture_filename), File.join(fixture_path, temp_path)
102
+ yield temp_path
103
+ ensure
104
+ FileUtils.rm_rf File.join(fixture_path, 'tmp')
105
+ end
106
+
107
+ def assert_created(num = 1)
108
+ assert_difference attachment_model.base_class, :count, num do
109
+ if attachment_model.included_modules.include? DbFile
110
+ assert_difference DbFile, :count, num do
111
+ yield
112
+ end
113
+ else
114
+ yield
115
+ end
116
+ end
117
+ end
118
+
119
+ def assert_not_created
120
+ assert_created(0) { yield }
121
+ end
122
+
123
+ def should_reject_by_size_with(klass)
124
+ attachment_model klass
125
+ assert_not_created do
126
+ attachment = upload_file :filename => '/files/rails.png'
127
+ assert attachment.new_record?
128
+ assert attachment.errors.on(:size)
129
+ assert_nil attachment.db_file if attachment.respond_to?(:db_file)
130
+ end
131
+ end
132
+
133
+ def assert_difference(object, method = nil, difference = 1)
134
+ initial_value = object.send(method)
135
+ yield
136
+ assert_equal initial_value + difference, object.send(method)
137
+ end
138
+
139
+ def assert_no_difference(object, method, &block)
140
+ assert_difference object, method, 0, &block
141
+ end
142
+
143
+ def attachment_model(klass = nil)
144
+ @attachment_model = klass if klass
145
+ @attachment_model
146
+ end
147
+ end
148
+
149
+ require File.join(File.dirname(__FILE__), 'fixtures/attachment')
150
+ require File.join(File.dirname(__FILE__), 'base_attachment_tests')
@@ -0,0 +1,55 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), 'test_helper'))
2
+
3
+ class ValidationTest < Test::Unit::TestCase
4
+ def test_should_invalidate_big_files
5
+ @attachment = SmallAttachment.new
6
+ assert !@attachment.valid?
7
+ assert @attachment.errors.on(:size)
8
+
9
+ @attachment.size = 2000
10
+ assert !@attachment.valid?
11
+ assert @attachment.errors.on(:size), @attachment.errors.full_messages.to_sentence
12
+
13
+ @attachment.size = 1000
14
+ assert !@attachment.valid?
15
+ assert_nil @attachment.errors.on(:size)
16
+ end
17
+
18
+ def test_should_invalidate_small_files
19
+ @attachment = BigAttachment.new
20
+ assert !@attachment.valid?
21
+ assert @attachment.errors.on(:size)
22
+
23
+ @attachment.size = 2000
24
+ assert !@attachment.valid?
25
+ assert @attachment.errors.on(:size), @attachment.errors.full_messages.to_sentence
26
+
27
+ @attachment.size = 1.megabyte
28
+ assert !@attachment.valid?
29
+ assert_nil @attachment.errors.on(:size)
30
+ end
31
+
32
+ def test_should_validate_content_type
33
+ @attachment = PdfAttachment.new
34
+ assert !@attachment.valid?
35
+ assert @attachment.errors.on(:content_type)
36
+
37
+ @attachment.content_type = 'foo'
38
+ assert !@attachment.valid?
39
+ assert @attachment.errors.on(:content_type)
40
+
41
+ @attachment.content_type = 'pdf'
42
+ assert !@attachment.valid?
43
+ assert_nil @attachment.errors.on(:content_type)
44
+ end
45
+
46
+ def test_should_require_filename
47
+ @attachment = Attachment.new
48
+ assert !@attachment.valid?
49
+ assert @attachment.errors.on(:filename)
50
+
51
+ @attachment.filename = 'foo'
52
+ assert !@attachment.valid?
53
+ assert_nil @attachment.errors.on(:filename)
54
+ end
55
+ end
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: popel-attachment_fu
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.4
5
+ platform: ruby
6
+ authors:
7
+ - Technoweenie
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-03-23 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Treat an ActiveRecord model as a file attachment, storing its patch, size, content type, etc.
17
+ email:
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README
24
+ files:
25
+ - VERSION.yml
26
+ - lib/geometry.rb
27
+ - lib/technoweenie
28
+ - lib/technoweenie/attachment_fu.rb
29
+ - lib/technoweenie/attachment_fu
30
+ - lib/technoweenie/attachment_fu/backends
31
+ - lib/technoweenie/attachment_fu/backends/db_file_backend.rb
32
+ - lib/technoweenie/attachment_fu/backends/file_system_backend.rb
33
+ - lib/technoweenie/attachment_fu/backends/s3_backend.rb
34
+ - lib/technoweenie/attachment_fu/processors
35
+ - lib/technoweenie/attachment_fu/processors/core_image_processor.rb
36
+ - lib/technoweenie/attachment_fu/processors/gd2_processor.rb
37
+ - lib/technoweenie/attachment_fu/processors/image_science_processor.rb
38
+ - lib/technoweenie/attachment_fu/processors/mini_magick_processor.rb
39
+ - lib/technoweenie/attachment_fu/processors/rmagick_processor.rb
40
+ - test/backends
41
+ - test/backends/db_file_test.rb
42
+ - test/backends/file_system_test.rb
43
+ - test/backends/remote
44
+ - test/backends/remote/s3_test.rb
45
+ - test/base_attachment_tests.rb
46
+ - test/basic_test.rb
47
+ - test/database.yml
48
+ - test/extra_attachment_test.rb
49
+ - test/fixtures
50
+ - test/fixtures/attachment.rb
51
+ - test/fixtures/files
52
+ - test/fixtures/files/fake
53
+ - test/fixtures/files/fake/rails.png
54
+ - test/fixtures/files/foo.txt
55
+ - test/fixtures/files/rails.png
56
+ - test/geometry_test.rb
57
+ - test/processors
58
+ - test/processors/core_image_test.rb
59
+ - test/processors/gd2_test.rb
60
+ - test/processors/image_science_test.rb
61
+ - test/processors/mini_magick_test.rb
62
+ - test/processors/rmagick_test.rb
63
+ - test/schema.rb
64
+ - test/test_helper.rb
65
+ - test/validation_test.rb
66
+ - README
67
+ has_rdoc: true
68
+ homepage: http://weblog.techno-weenie.net
69
+ post_install_message:
70
+ rdoc_options:
71
+ - --inline-source
72
+ - --charset=UTF-8
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: "0"
80
+ version:
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: "0"
86
+ version:
87
+ requirements: []
88
+
89
+ rubyforge_project:
90
+ rubygems_version: 1.2.0
91
+ signing_key:
92
+ specification_version: 2
93
+ summary: Treat an ActiveRecord model as a file attachment, storing its patch, size, content type, etc.
94
+ test_files: []
95
+