attachmerb_fu 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (36) hide show
  1. data/LICENSE +22 -0
  2. data/README +166 -0
  3. data/Rakefile +35 -0
  4. data/TODO +5 -0
  5. data/lib/amazon_s3.yml.tpl +14 -0
  6. data/lib/attachment_fu.rb +431 -0
  7. data/lib/attachmerb_fu.rb +446 -0
  8. data/lib/attachmerb_fu/backends/db_file_backend.rb +37 -0
  9. data/lib/attachmerb_fu/backends/file_system_backend.rb +95 -0
  10. data/lib/attachmerb_fu/backends/s3_backend.rb +307 -0
  11. data/lib/attachmerb_fu/merbtasks.rb +6 -0
  12. data/lib/attachmerb_fu/processors/image_science_processor.rb +60 -0
  13. data/lib/attachmerb_fu/processors/mini_magick_processor.rb +54 -0
  14. data/lib/attachmerb_fu/processors/rmagick_processor.rb +51 -0
  15. data/lib/geometry.rb +93 -0
  16. data/lib/tempfile_ext.rb +9 -0
  17. data/lib/test/amazon_s3.yml +6 -0
  18. data/lib/test/backends/db_file_test.rb +16 -0
  19. data/lib/test/backends/file_system_test.rb +80 -0
  20. data/lib/test/backends/remote/s3_test.rb +103 -0
  21. data/lib/test/base_attachment_tests.rb +57 -0
  22. data/lib/test/basic_test.rb +64 -0
  23. data/lib/test/database.yml +18 -0
  24. data/lib/test/extra_attachment_test.rb +57 -0
  25. data/lib/test/fixtures/attachment.rb +127 -0
  26. data/lib/test/fixtures/files/fake/rails.png +0 -0
  27. data/lib/test/fixtures/files/foo.txt +1 -0
  28. data/lib/test/fixtures/files/rails.png +0 -0
  29. data/lib/test/geometry_test.rb +101 -0
  30. data/lib/test/processors/image_science_test.rb +31 -0
  31. data/lib/test/processors/mini_magick_test.rb +31 -0
  32. data/lib/test/processors/rmagick_test.rb +241 -0
  33. data/lib/test/schema.rb +86 -0
  34. data/lib/test/test_helper.rb +142 -0
  35. data/lib/test/validation_test.rb +55 -0
  36. metadata +107 -0
@@ -0,0 +1,86 @@
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 :image_science_attachments, :force => true do |t|
27
+ t.column :parent_id, :integer
28
+ t.column :thumbnail, :string
29
+ t.column :filename, :string, :limit => 255
30
+ t.column :content_type, :string, :limit => 255
31
+ t.column :size, :integer
32
+ t.column :width, :integer
33
+ t.column :height, :integer
34
+ t.column :type, :string
35
+ end
36
+
37
+ create_table :mini_magick_attachments, :force => true do |t|
38
+ t.column :parent_id, :integer
39
+ t.column :thumbnail, :string
40
+ t.column :filename, :string, :limit => 255
41
+ t.column :content_type, :string, :limit => 255
42
+ t.column :size, :integer
43
+ t.column :width, :integer
44
+ t.column :height, :integer
45
+ t.column :type, :string
46
+ end
47
+
48
+ create_table :mini_magick_attachments, :force => true do |t|
49
+ t.column :parent_id, :integer
50
+ t.column :thumbnail, :string
51
+ t.column :filename, :string, :limit => 255
52
+ t.column :content_type, :string, :limit => 255
53
+ t.column :size, :integer
54
+ t.column :width, :integer
55
+ t.column :height, :integer
56
+ t.column :type, :string
57
+ end
58
+
59
+ create_table :orphan_attachments, :force => true do |t|
60
+ t.column :db_file_id, :integer
61
+ t.column :filename, :string, :limit => 255
62
+ t.column :content_type, :string, :limit => 255
63
+ t.column :size, :integer
64
+ end
65
+
66
+ create_table :minimal_attachments, :force => true do |t|
67
+ t.column :size, :integer
68
+ t.column :content_type, :string, :limit => 255
69
+ end
70
+
71
+ create_table :db_files, :force => true do |t|
72
+ t.column :data, :binary
73
+ end
74
+
75
+ create_table :s3_attachments, :force => true do |t|
76
+ t.column :parent_id, :integer
77
+ t.column :thumbnail, :string
78
+ t.column :filename, :string, :limit => 255
79
+ t.column :content_type, :string, :limit => 255
80
+ t.column :size, :integer
81
+ t.column :width, :integer
82
+ t.column :height, :integer
83
+ t.column :type, :string
84
+ t.column :aspect_ratio, :float
85
+ end
86
+ end
@@ -0,0 +1,142 @@
1
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
2
+
3
+ ENV['RAILS_ENV'] = 'test'
4
+
5
+ require 'test/unit'
6
+ require File.expand_path(File.join(File.dirname(__FILE__), '../../../../config/environment.rb'))
7
+ require 'breakpoint'
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 use_temp_file(fixture_filename)
91
+ temp_path = File.join('/tmp', File.basename(fixture_filename))
92
+ FileUtils.mkdir_p File.join(fixture_path, 'tmp')
93
+ FileUtils.cp File.join(fixture_path, fixture_filename), File.join(fixture_path, temp_path)
94
+ yield temp_path
95
+ ensure
96
+ FileUtils.rm_rf File.join(fixture_path, 'tmp')
97
+ end
98
+
99
+ def assert_created(num = 1)
100
+ assert_difference attachment_model.base_class, :count, num do
101
+ if attachment_model.included_modules.include? DbFile
102
+ assert_difference DbFile, :count, num do
103
+ yield
104
+ end
105
+ else
106
+ yield
107
+ end
108
+ end
109
+ end
110
+
111
+ def assert_not_created
112
+ assert_created(0) { yield }
113
+ end
114
+
115
+ def should_reject_by_size_with(klass)
116
+ attachment_model klass
117
+ assert_not_created do
118
+ attachment = upload_file :filename => '/files/rails.png'
119
+ assert attachment.new_record?
120
+ assert attachment.errors.on(:size)
121
+ assert_nil attachment.db_file if attachment.respond_to?(:db_file)
122
+ end
123
+ end
124
+
125
+ def assert_difference(object, method = nil, difference = 1)
126
+ initial_value = object.send(method)
127
+ yield
128
+ assert_equal initial_value + difference, object.send(method)
129
+ end
130
+
131
+ def assert_no_difference(object, method, &block)
132
+ assert_difference object, method, 0, &block
133
+ end
134
+
135
+ def attachment_model(klass = nil)
136
+ @attachment_model = klass if klass
137
+ @attachment_model
138
+ end
139
+ end
140
+
141
+ require File.join(File.dirname(__FILE__), 'fixtures/attachment')
142
+ 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,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: attachmerb_fu
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Michael Siebert
8
+ autorequire: attachmerb_fu
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-01-10 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: merb
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 0.5.0
23
+ version:
24
+ description: Merb plugin that provides a port of attachment_fu to merb
25
+ email: siebertm85@googlemail.com
26
+ executables: []
27
+
28
+ extensions: []
29
+
30
+ extra_rdoc_files:
31
+ - README
32
+ - LICENSE
33
+ - TODO
34
+ files:
35
+ - LICENSE
36
+ - README
37
+ - Rakefile
38
+ - TODO
39
+ - lib/amazon_s3.yml.tpl
40
+ - lib/attachment_fu.rb
41
+ - lib/attachmerb_fu
42
+ - lib/attachmerb_fu/backends
43
+ - lib/attachmerb_fu/backends/db_file_backend.rb
44
+ - lib/attachmerb_fu/backends/file_system_backend.rb
45
+ - lib/attachmerb_fu/backends/s3_backend.rb
46
+ - lib/attachmerb_fu/merbtasks.rb
47
+ - lib/attachmerb_fu/processors
48
+ - lib/attachmerb_fu/processors/image_science_processor.rb
49
+ - lib/attachmerb_fu/processors/mini_magick_processor.rb
50
+ - lib/attachmerb_fu/processors/rmagick_processor.rb
51
+ - lib/attachmerb_fu.rb
52
+ - lib/geometry.rb
53
+ - lib/tempfile_ext.rb
54
+ - lib/test
55
+ - lib/test/amazon_s3.yml
56
+ - lib/test/backends
57
+ - lib/test/backends/db_file_test.rb
58
+ - lib/test/backends/file_system_test.rb
59
+ - lib/test/backends/remote
60
+ - lib/test/backends/remote/s3_test.rb
61
+ - lib/test/base_attachment_tests.rb
62
+ - lib/test/basic_test.rb
63
+ - lib/test/database.yml
64
+ - lib/test/extra_attachment_test.rb
65
+ - lib/test/fixtures
66
+ - lib/test/fixtures/attachment.rb
67
+ - lib/test/fixtures/files
68
+ - lib/test/fixtures/files/fake
69
+ - lib/test/fixtures/files/fake/rails.png
70
+ - lib/test/fixtures/files/foo.txt
71
+ - lib/test/fixtures/files/rails.png
72
+ - lib/test/geometry_test.rb
73
+ - lib/test/processors
74
+ - lib/test/processors/image_science_test.rb
75
+ - lib/test/processors/mini_magick_test.rb
76
+ - lib/test/processors/rmagick_test.rb
77
+ - lib/test/schema.rb
78
+ - lib/test/test_helper.rb
79
+ - lib/test/validation_test.rb
80
+ has_rdoc: true
81
+ homepage: http://merb-plugins.rubyforge.org/attachmerb_fu/
82
+ post_install_message:
83
+ rdoc_options: []
84
+
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: "0"
92
+ version:
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: "0"
98
+ version:
99
+ requirements: []
100
+
101
+ rubyforge_project:
102
+ rubygems_version: 1.0.1
103
+ signing_key:
104
+ specification_version: 2
105
+ summary: Merb plugin that provides a port of attachment_fu to merb
106
+ test_files: []
107
+