dm-paperclip 2.1.2

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.
@@ -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', 'dm-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,119 @@
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 write protected" do
11
+ setup do
12
+ Dummy.class_eval do
13
+ has_attached_file :image, { :protected => true }
14
+ end
15
+ @dummy = Dummy.new
16
+ end
17
+
18
+ should "not assign the avatar on mass-set" do
19
+ @dummy.attributes = { :other => "I'm set!" } #,
20
+ #:image => @file }
21
+
22
+ assert_equal "I'm set!", @dummy.other
23
+ assert ! @dummy.image?
24
+ end
25
+
26
+ should "still allow assigment on normal set" do
27
+ @dummy.other = "I'm set!"
28
+ @dummy.image = @file
29
+
30
+ assert_equal "I'm set!", @dummy.other
31
+ assert @dummy.image?
32
+ end
33
+ end
34
+
35
+ context "with a subclass" do
36
+ setup do
37
+ class ::SubDummy < Dummy; end
38
+ end
39
+
40
+ should "be able to use the attachment from the subclass" do
41
+ assert_nothing_raised do
42
+ @subdummy = SubDummy.create(:avatar => @file)
43
+ end
44
+ end
45
+
46
+ should "be able to see the attachment definition from the subclass's class" do
47
+ assert_equal "tmp/:class/omg/:style.:extension", SubDummy.attachment_definitions[:avatar][:path]
48
+ end
49
+
50
+ teardown do
51
+ Object.send(:remove_const, "SubDummy") rescue nil
52
+ end
53
+ end
54
+
55
+ should "have an #avatar method" do
56
+ assert Dummy.new.respond_to?(:avatar)
57
+ end
58
+
59
+ should "have an #avatar= method" do
60
+ assert Dummy.new.respond_to?(:avatar=)
61
+ end
62
+
63
+ [[:presence, nil, "5k.png", nil],
64
+ [:size, {:in => 1..10240}, "5k.png", "12k.png"],
65
+ [:size2, {:in => 1..10240}, nil, "12k.png"],
66
+ [:content_type1, {:content_type => "image/png"}, "5k.png", "text.txt"],
67
+ [:content_type2, {:content_type => "text/plain"}, "text.txt", "5k.png"],
68
+ [:content_type3, {:content_type => %r{image/.*}}, "5k.png", "text.txt"],
69
+ [:content_type4, {:content_type => "image/png"}, nil, "text.txt"]].each do |args|
70
+ context "with #{args[0]} validations" do
71
+ setup do
72
+ Dummy.class_eval do
73
+ send(*[:"validates_attachment_#{args[0].to_s[/[a-z_]*/]}", :avatar, args[1]].compact)
74
+ end
75
+ @dummy = Dummy.new
76
+ end
77
+
78
+ context "and a valid file" do
79
+ setup do
80
+ @file = args[2] && File.new(File.join(FIXTURES_DIR, args[2]))
81
+ end
82
+
83
+ should "not have any errors" do
84
+ @dummy.avatar = @file
85
+ assert @dummy.valid?
86
+ assert_equal 0, @dummy.errors.length
87
+ end
88
+ end
89
+
90
+ context "and an invalid file" do
91
+ setup do
92
+ @file = args[3] && File.new(File.join(FIXTURES_DIR, args[3]))
93
+ end
94
+
95
+ should "have errors" do
96
+ @dummy.avatar = @file
97
+ assert ! @dummy.valid?
98
+ assert_equal 1, @dummy.errors.length
99
+ end
100
+ end
101
+
102
+ # context "and an invalid file with :message" do
103
+ # setup do
104
+ # @file = args[3] && File.new(File.join(FIXTURES_DIR, args[3]))
105
+ # end
106
+ #
107
+ # should "have errors" do
108
+ # if args[1] && args[1][:message] && args[4]
109
+ # @dummy.avatar = @file
110
+ # assert ! @dummy.valid?
111
+ # assert_equal 1, @dummy.errors.length
112
+ # assert_equal args[4], @dummy.errors[0]
113
+ # end
114
+ # end
115
+ # end
116
+ end
117
+ end
118
+ end
119
+ 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', 'dm-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', 'dm-paperclip', 'geometry.rb')
8
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'dm-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,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dm-paperclip
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.1.2
5
+ platform: ruby
6
+ authors:
7
+ - Ken Robertson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-05-15 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: ken@invalidlogic.com jyurek@thoughtbot.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.textile
24
+ files:
25
+ - README.textile
26
+ - LICENSE
27
+ - Rakefile
28
+ - init.rb
29
+ - lib/dm-paperclip
30
+ - lib/dm-paperclip/attachment.rb
31
+ - lib/dm-paperclip/geometry.rb
32
+ - lib/dm-paperclip/iostream.rb
33
+ - lib/dm-paperclip/storage.rb
34
+ - lib/dm-paperclip/thumbnail.rb
35
+ - lib/dm-paperclip/upfile.rb
36
+ - lib/dm-paperclip/validations.rb
37
+ - lib/dm-paperclip.rb
38
+ - tasks/paperclip_tasks.rake
39
+ - test/fixtures
40
+ - test/fixtures/12k.png
41
+ - test/fixtures/50x50.png
42
+ - test/fixtures/5k.png
43
+ - test/fixtures/bad.png
44
+ - test/fixtures/text.txt
45
+ - test/helper.rb
46
+ - test/test_attachment.rb
47
+ - test/test_geometry.rb
48
+ - test/test_integration.rb
49
+ - test/test_iostream.rb
50
+ - test/test_paperclip.rb
51
+ - test/test_storage.rb
52
+ - test/test_thumbnail.rb
53
+ has_rdoc: true
54
+ homepage: http://invalidlogic.com/dm-paperclip/
55
+ post_install_message:
56
+ rdoc_options:
57
+ - --line-numbers
58
+ - --inline-source
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: "0"
66
+ version:
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: "0"
72
+ version:
73
+ requirements:
74
+ - ImageMagick
75
+ - data_mapper
76
+ rubyforge_project: dm-paperclip
77
+ rubygems_version: 1.1.1
78
+ signing_key:
79
+ specification_version: 2
80
+ summary: File attachments as attributes for DataMapper, based on the original Paperclip by Jon Yurek at Thoughtbot
81
+ test_files:
82
+ - test/test_attachment.rb
83
+ - test/test_geometry.rb
84
+ - test/test_integration.rb
85
+ - test/test_iostream.rb
86
+ - test/test_paperclip.rb
87
+ - test/test_storage.rb
88
+ - test/test_thumbnail.rb