jcnetdev-paperclip 1.0.20080704

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,60 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'stringio'
4
+ require 'tempfile'
5
+ require 'shoulda'
6
+
7
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'paperclip', 'iostream.rb')
8
+
9
+ class IOStreamTest < Test::Unit::TestCase
10
+ context "IOStream" do
11
+ should "be included in IO, File, Tempfile, and StringIO" do
12
+ [IO, File, Tempfile, StringIO].each do |klass|
13
+ assert klass.included_modules.include?(IOStream), "Not in #{klass}"
14
+ end
15
+ end
16
+ end
17
+
18
+ context "A file" do
19
+ setup do
20
+ @file = File.new(File.join(File.dirname(__FILE__), "fixtures", "5k.png"))
21
+ end
22
+
23
+ context "that is sent #stream_to" do
24
+
25
+ [["/tmp/iostream.string.test", File],
26
+ [Tempfile.new('iostream.test'), Tempfile]].each do |args|
27
+
28
+ context "and given a #{args[0].class.to_s}" do
29
+ setup do
30
+ assert @result = @file.stream_to(args[0])
31
+ end
32
+
33
+ should "return a #{args[1].to_s}" do
34
+ assert @result.is_a?(args[1])
35
+ end
36
+
37
+ should "contain the same data as the original file" do
38
+ @file.rewind; @result.rewind
39
+ assert_equal @file.read, @result.read
40
+ end
41
+ end
42
+ end
43
+ end
44
+
45
+ context "that is sent #to_tempfile" do
46
+ setup do
47
+ assert @tempfile = @file.to_tempfile
48
+ end
49
+
50
+ should "convert it to a Tempfile" do
51
+ assert @tempfile.is_a?(Tempfile)
52
+ end
53
+
54
+ should "have the Tempfile contain the same data as the file" do
55
+ @file.rewind; @tempfile.rewind
56
+ assert_equal @file.read, @tempfile.read
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,123 @@
1
+ require 'test/helper.rb'
2
+
3
+ class PaperclipTest < Test::Unit::TestCase
4
+ context "An ActiveRecord model with an 'avatar' attachment" do
5
+ setup do
6
+ rebuild_model :path => "tmp/:class/omg/:style.:extension"
7
+ @file = File.new(File.join(FIXTURES_DIR, "5k.png"))
8
+ end
9
+
10
+ context "that is attr_protected" do
11
+ setup do
12
+ Dummy.class_eval do
13
+ attr_protected :avatar
14
+ end
15
+ @dummy = Dummy.new
16
+ end
17
+
18
+ should "not assign the avatar on mass-set" do
19
+ @dummy.logger.expects(:debug)
20
+
21
+ @dummy.attributes = { :other => "I'm set!",
22
+ :avatar => @file }
23
+
24
+ assert_equal "I'm set!", @dummy.other
25
+ assert ! @dummy.avatar?
26
+ end
27
+
28
+ should "still allow assigment on normal set" do
29
+ @dummy.logger.expects(:debug).times(0)
30
+
31
+ @dummy.other = "I'm set!"
32
+ @dummy.avatar = @file
33
+
34
+ assert_equal "I'm set!", @dummy.other
35
+ assert @dummy.avatar?
36
+ end
37
+ end
38
+
39
+ context "with a subclass" do
40
+ setup do
41
+ class ::SubDummy < Dummy; end
42
+ end
43
+
44
+ should "be able to use the attachment from the subclass" do
45
+ assert_nothing_raised do
46
+ @subdummy = SubDummy.create(:avatar => @file)
47
+ end
48
+ end
49
+
50
+ should "be able to see the attachment definition from the subclass's class" do
51
+ assert_equal "tmp/:class/omg/:style.:extension", SubDummy.attachment_definitions[:avatar][:path]
52
+ end
53
+
54
+ teardown do
55
+ Object.send(:remove_const, "SubDummy") rescue nil
56
+ end
57
+ end
58
+
59
+ should "have an #avatar method" do
60
+ assert Dummy.new.respond_to?(:avatar)
61
+ end
62
+
63
+ should "have an #avatar= method" do
64
+ assert Dummy.new.respond_to?(:avatar=)
65
+ end
66
+
67
+ [[:presence, nil, "5k.png", nil],
68
+ [:size, {:in => 1..10240}, "5k.png", "12k.png"],
69
+ [:size2, {:in => 1..10240}, nil, "12k.png"],
70
+ [:content_type1, {:content_type => "image/png"}, "5k.png", "text.txt"],
71
+ [:content_type2, {:content_type => "text/plain"}, "text.txt", "5k.png"],
72
+ [:content_type3, {:content_type => %r{image/.*}}, "5k.png", "text.txt"],
73
+ [:content_type4, {:content_type => "image/png"}, nil, "text.txt"]].each do |args|
74
+ context "with #{args[0]} validations" do
75
+ setup do
76
+ Dummy.class_eval do
77
+ send(*[:"validates_attachment_#{args[0].to_s[/[a-z_]*/]}", :avatar, args[1]].compact)
78
+ end
79
+ @dummy = Dummy.new
80
+ end
81
+
82
+ context "and a valid file" do
83
+ setup do
84
+ @file = args[2] && File.new(File.join(FIXTURES_DIR, args[2]))
85
+ end
86
+
87
+ should "not have any errors" do
88
+ @dummy.avatar = @file
89
+ assert @dummy.avatar.valid?
90
+ assert_equal 0, @dummy.avatar.errors.length
91
+ end
92
+ end
93
+
94
+ context "and an invalid file" do
95
+ setup do
96
+ @file = args[3] && File.new(File.join(FIXTURES_DIR, args[3]))
97
+ end
98
+
99
+ should "have errors" do
100
+ @dummy.avatar = @file
101
+ assert ! @dummy.avatar.valid?
102
+ assert_equal 1, @dummy.avatar.errors.length
103
+ end
104
+ end
105
+
106
+ # context "and an invalid file with :message" do
107
+ # setup do
108
+ # @file = args[3] && File.new(File.join(FIXTURES_DIR, args[3]))
109
+ # end
110
+ #
111
+ # should "have errors" do
112
+ # if args[1] && args[1][:message] && args[4]
113
+ # @dummy.avatar = @file
114
+ # assert ! @dummy.avatar.valid?
115
+ # assert_equal 1, @dummy.avatar.errors.length
116
+ # assert_equal args[4], @dummy.avatar.errors[0]
117
+ # end
118
+ # end
119
+ # end
120
+ end
121
+ end
122
+ end
123
+ end
@@ -0,0 +1,136 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+ require 'right_aws'
5
+
6
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'paperclip', 'geometry.rb')
7
+
8
+ class StorageTest < Test::Unit::TestCase
9
+ context "Parsing S3 credentials" do
10
+ setup do
11
+ rebuild_model :storage => :s3,
12
+ :bucket => "testing",
13
+ :s3_credentials => {:not => :important}
14
+
15
+ @dummy = Dummy.new
16
+ @avatar = @dummy.avatar
17
+
18
+ @current_env = ENV['RAILS_ENV']
19
+ end
20
+
21
+ teardown do
22
+ ENV['RAILS_ENV'] = @current_env
23
+ end
24
+
25
+ should "get the correct credentials when RAILS_ENV is production" do
26
+ ENV['RAILS_ENV'] = 'production'
27
+ assert_equal({:key => "12345"},
28
+ @avatar.parse_credentials('production' => {:key => '12345'},
29
+ :development => {:key => "54321"}))
30
+ end
31
+
32
+ should "get the correct credentials when RAILS_ENV is development" do
33
+ ENV['RAILS_ENV'] = 'development'
34
+ assert_equal({:key => "54321"},
35
+ @avatar.parse_credentials('production' => {:key => '12345'},
36
+ :development => {:key => "54321"}))
37
+ end
38
+
39
+ should "return the argument if the key does not exist" do
40
+ ENV['RAILS_ENV'] = "not really an env"
41
+ assert_equal({:test => "12345"}, @avatar.parse_credentials(:test => "12345"))
42
+ end
43
+ end
44
+
45
+ context "An attachment with S3 storage" do
46
+ setup do
47
+ rebuild_model :storage => :s3,
48
+ :bucket => "testing",
49
+ :path => ":attachment/:style/:basename.:extension",
50
+ :s3_credentials => {
51
+ 'access_key_id' => "12345",
52
+ 'secret_access_key' => "54321"
53
+ }
54
+ end
55
+
56
+ should "be extended by the S3 module" do
57
+ assert Dummy.new.avatar.is_a?(Paperclip::Storage::S3)
58
+ end
59
+
60
+ should "not be extended by the Filesystem module" do
61
+ assert ! Dummy.new.avatar.is_a?(Paperclip::Storage::Filesystem)
62
+ end
63
+
64
+ context "when assigned" do
65
+ setup do
66
+ @file = File.new(File.join(File.dirname(__FILE__), 'fixtures', '5k.png'))
67
+ @dummy = Dummy.new
68
+ @dummy.avatar = @file
69
+ end
70
+
71
+ should "not get a bucket to get a URL" do
72
+ @dummy.avatar.expects(:s3).never
73
+ @dummy.avatar.expects(:s3_bucket).never
74
+ assert_equal "https://s3.amazonaws.com/testing/avatars/original/5k.png", @dummy.avatar.url
75
+ end
76
+
77
+ context "and saved" do
78
+ setup do
79
+ @s3_mock = stub
80
+ @bucket_mock = stub
81
+ RightAws::S3.expects(:new).with("12345", "54321", {}).returns(@s3_mock)
82
+ @s3_mock.expects(:bucket).with("testing", true, "public-read").returns(@bucket_mock)
83
+ @key_mock = stub
84
+ @bucket_mock.expects(:key).returns(@key_mock)
85
+ @key_mock.expects(:data=)
86
+ @key_mock.expects(:put)
87
+ @dummy.save
88
+ end
89
+
90
+ should "succeed" do
91
+ assert true
92
+ end
93
+ end
94
+ end
95
+ end
96
+
97
+ unless ENV["S3_TEST_BUCKET"].blank?
98
+ context "Using S3 for real, an attachment with S3 storage" do
99
+ setup do
100
+ rebuild_model :styles => { :thumb => "100x100", :square => "32x32#" },
101
+ :storage => :s3,
102
+ :bucket => ENV["S3_TEST_BUCKET"],
103
+ :path => ":class/:attachment/:id/:style.:extension",
104
+ :s3_credentials => File.new(File.join(File.dirname(__FILE__), "s3.yml"))
105
+
106
+ Dummy.delete_all
107
+ @dummy = Dummy.new
108
+ end
109
+
110
+ should "be extended by the S3 module" do
111
+ assert Dummy.new.avatar.is_a?(Paperclip::Storage::S3)
112
+ end
113
+
114
+ context "when assigned" do
115
+ setup do
116
+ @file = File.new(File.join(File.dirname(__FILE__), 'fixtures', '5k.png'))
117
+ @dummy.avatar = @file
118
+ end
119
+
120
+ should "still return a Tempfile when sent #to_io" do
121
+ assert_equal Tempfile, @dummy.avatar.to_io.class
122
+ end
123
+
124
+ context "and saved" do
125
+ setup do
126
+ @dummy.save
127
+ end
128
+
129
+ should "be on S3" do
130
+ assert true
131
+ end
132
+ end
133
+ end
134
+ end
135
+ end
136
+ end
@@ -0,0 +1,107 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+ require 'mocha'
5
+ require 'tempfile'
6
+
7
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'paperclip', 'geometry.rb')
8
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'paperclip', 'thumbnail.rb')
9
+
10
+ class ThumbnailTest < Test::Unit::TestCase
11
+
12
+ context "A Paperclip Tempfile" do
13
+ setup do
14
+ @tempfile = Paperclip::Tempfile.new("file.jpg")
15
+ end
16
+
17
+ should "have its path contain a real extension" do
18
+ assert_equal ".jpg", File.extname(@tempfile.path)
19
+ end
20
+
21
+ should "be a real Tempfile" do
22
+ assert @tempfile.is_a?(::Tempfile)
23
+ end
24
+ end
25
+
26
+ context "Another Paperclip Tempfile" do
27
+ setup do
28
+ @tempfile = Paperclip::Tempfile.new("file")
29
+ end
30
+
31
+ should "not have an extension if not given one" do
32
+ assert_equal "", File.extname(@tempfile.path)
33
+ end
34
+
35
+ should "still be a real Tempfile" do
36
+ assert @tempfile.is_a?(::Tempfile)
37
+ end
38
+ end
39
+
40
+ context "An image" do
41
+ setup do
42
+ @file = File.new(File.join(File.dirname(__FILE__), "fixtures", "5k.png"))
43
+ end
44
+
45
+ [["600x600>", "434x66"],
46
+ ["400x400>", "400x61"],
47
+ ["32x32<", "434x66"]
48
+ ].each do |args|
49
+ context "being thumbnailed with a geometry of #{args[0]}" do
50
+ setup do
51
+ @thumb = Paperclip::Thumbnail.new(@file, args[0])
52
+ end
53
+
54
+ should "start with dimensions of 434x66" do
55
+ cmd = %Q[identify -format "%wx%h" #{@file.path}]
56
+ assert_equal "434x66", `#{cmd}`.chomp
57
+ end
58
+
59
+ should "report the correct target geometry" do
60
+ assert_equal args[0], @thumb.target_geometry.to_s
61
+ end
62
+
63
+ context "when made" do
64
+ setup do
65
+ @thumb_result = @thumb.make
66
+ end
67
+
68
+ should "be the size we expect it to be" do
69
+ cmd = %Q[identify -format "%wx%h" #{@thumb_result.path}]
70
+ assert_equal args[1], `#{cmd}`.chomp
71
+ end
72
+ end
73
+ end
74
+ end
75
+
76
+ context "being thumbnailed at 100x50 with cropping" do
77
+ setup do
78
+ @thumb = Paperclip::Thumbnail.new(@file, "100x50#")
79
+ end
80
+
81
+ should "report its correct current and target geometries" do
82
+ assert_equal "100x50#", @thumb.target_geometry.to_s
83
+ assert_equal "434x66", @thumb.current_geometry.to_s
84
+ end
85
+
86
+ should "report its correct format" do
87
+ assert_nil @thumb.format
88
+ end
89
+
90
+ should "have whiny_thumbnails turned on by default" do
91
+ assert @thumb.whiny_thumbnails
92
+ end
93
+
94
+ should "send the right command to convert when sent #make" do
95
+ @thumb.expects(:system).with do |arg|
96
+ arg.match %r{convert\s+"#{File.expand_path(@thumb.file.path)}"\s+-scale\s+\"x50\"\s+-crop\s+\"100x50\+114\+0\"\s+\+repage\s+".*?"}
97
+ end
98
+ @thumb.make
99
+ end
100
+
101
+ should "create the thumbnail when sent #make" do
102
+ dst = @thumb.make
103
+ assert_match /100x50/, `identify #{dst.path}`
104
+ end
105
+ end
106
+ end
107
+ end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jcnetdev-paperclip
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.20080704
5
+ platform: ruby
6
+ authors:
7
+ - Jon Yurek
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-07-04 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rails
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: "2.1"
23
+ version:
24
+ description: Paperclip is intended as an easy file attachment library for ActiveRecord. The intent behind it was to keep setup as easy as possible and to treat files as much like other attributes as possible. This means they aren't saved to their final locations on disk, nor are they deleted if set to nil, until ActiveRecord::Base#save is called. It manages validations based on size and presence, if required. It can transform its assigned image into thumbnails if needed, and the prerequisites are as simple as installing ImageMagick (which, for most modern Unix-based systems, is as easy as installing the right packages). Attached files are saved to the filesystem and referenced in the browser by an easily understandable specification, which has sensible and useful defaults.
25
+ email: info@thoughtbot.com
26
+ executables: []
27
+
28
+ extensions: []
29
+
30
+ extra_rdoc_files:
31
+ - README
32
+ files:
33
+ - LICENSE
34
+ - README
35
+ - README.rdoc
36
+ - Rakefile
37
+ - generators/paperclip/paperclip_generator.rb
38
+ - generators/paperclip/templates/paperclip_migration.rb
39
+ - generators/paperclip/USAGE
40
+ - init.rb
41
+ - lib/paperclip/attachment.rb
42
+ - lib/paperclip/geometry.rb
43
+ - lib/paperclip/iostream.rb
44
+ - lib/paperclip/storage.rb
45
+ - lib/paperclip/thumbnail.rb
46
+ - lib/paperclip/upfile.rb
47
+ - lib/paperclip.rb
48
+ - paperclip.gemspec
49
+ - rails/init.rb
50
+ - tasks/paperclip_tasks.rake
51
+ has_rdoc: true
52
+ homepage: http://github.com/thoughtbot/paperclip
53
+ post_install_message:
54
+ rdoc_options:
55
+ - --main
56
+ - README
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: "0"
64
+ version:
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: "0"
70
+ version:
71
+ requirements: []
72
+
73
+ rubyforge_project:
74
+ rubygems_version: 1.2.0
75
+ signing_key:
76
+ specification_version: 2
77
+ summary: Allows easy file uploading for Rails
78
+ test_files:
79
+ - test/.gitignore
80
+ - test/database.yml
81
+ - test/fixtures/12k.png
82
+ - test/fixtures/50x50.png
83
+ - test/fixtures/5k.png
84
+ - test/fixtures/bad.png
85
+ - test/fixtures/text.txt
86
+ - test/helper.rb
87
+ - test/test_attachment.rb
88
+ - test/test_geometry.rb
89
+ - test/test_integration.rb
90
+ - test/test_iostream.rb
91
+ - test/test_paperclip.rb
92
+ - test/test_storage.rb
93
+ - test/test_thumbnail.rb