paperclip-v2_7-patched-ruby-1_8_6 2.7.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +23 -0
- data/.travis.yml +14 -0
- data/Appraisals +20 -0
- data/CONTRIBUTING.md +38 -0
- data/Gemfile +5 -0
- data/LICENSE +26 -0
- data/NEWS +69 -0
- data/README.md +444 -0
- data/Rakefile +41 -0
- data/cucumber/paperclip_steps.rb +6 -0
- data/features/basic_integration.feature +48 -0
- data/features/rake_tasks.feature +68 -0
- data/features/step_definitions/attachment_steps.rb +65 -0
- data/features/step_definitions/html_steps.rb +15 -0
- data/features/step_definitions/rails_steps.rb +193 -0
- data/features/step_definitions/s3_steps.rb +14 -0
- data/features/step_definitions/web_steps.rb +209 -0
- data/features/support/env.rb +8 -0
- data/features/support/fakeweb.rb +3 -0
- data/features/support/fixtures/.boot_config.rb.swo +0 -0
- data/features/support/fixtures/boot_config.txt +15 -0
- data/features/support/fixtures/gemfile.txt +5 -0
- data/features/support/fixtures/preinitializer.txt +20 -0
- data/features/support/paths.rb +28 -0
- data/features/support/rails.rb +46 -0
- data/features/support/selectors.rb +19 -0
- data/gemfiles/rails2.gemfile +9 -0
- data/gemfiles/rails3.gemfile +9 -0
- data/gemfiles/rails3_1.gemfile +9 -0
- data/gemfiles/rails3_2.gemfile +9 -0
- data/generators/paperclip/USAGE +5 -0
- data/generators/paperclip/paperclip_generator.rb +27 -0
- data/generators/paperclip/templates/paperclip_migration.rb.erb +19 -0
- data/init.rb +4 -0
- data/lib/generators/paperclip/USAGE +8 -0
- data/lib/generators/paperclip/paperclip_generator.rb +33 -0
- data/lib/generators/paperclip/templates/paperclip_migration.rb.erb +19 -0
- data/lib/paperclip.rb +493 -0
- data/lib/paperclip/attachment.rb +491 -0
- data/lib/paperclip/attachment_options.rb +10 -0
- data/lib/paperclip/callback_compatibility.rb +61 -0
- data/lib/paperclip/geometry.rb +120 -0
- data/lib/paperclip/interpolations.rb +174 -0
- data/lib/paperclip/iostream.rb +45 -0
- data/lib/paperclip/matchers.rb +64 -0
- data/lib/paperclip/matchers/have_attached_file_matcher.rb +57 -0
- data/lib/paperclip/matchers/validate_attachment_content_type_matcher.rb +81 -0
- data/lib/paperclip/matchers/validate_attachment_presence_matcher.rb +54 -0
- data/lib/paperclip/matchers/validate_attachment_size_matcher.rb +95 -0
- data/lib/paperclip/missing_attachment_styles.rb +87 -0
- data/lib/paperclip/processor.rb +58 -0
- data/lib/paperclip/railtie.rb +35 -0
- data/lib/paperclip/schema.rb +39 -0
- data/lib/paperclip/storage.rb +3 -0
- data/lib/paperclip/storage/filesystem.rb +81 -0
- data/lib/paperclip/storage/fog.rb +191 -0
- data/lib/paperclip/storage/s3.rb +351 -0
- data/lib/paperclip/style.rb +103 -0
- data/lib/paperclip/thumbnail.rb +105 -0
- data/lib/paperclip/upfile.rb +64 -0
- data/lib/paperclip/url_generator.rb +64 -0
- data/lib/paperclip/version.rb +3 -0
- data/lib/tasks/paperclip.rake +101 -0
- data/paperclip.gemspec +41 -0
- data/rails/init.rb +2 -0
- data/shoulda_macros/paperclip.rb +124 -0
- data/test/attachment_options_test.rb +40 -0
- data/test/attachment_test.rb +1211 -0
- data/test/database.yml +4 -0
- data/test/fixtures/12k.png +0 -0
- data/test/fixtures/50x50.png +0 -0
- data/test/fixtures/5k.png +0 -0
- data/test/fixtures/animated.gif +0 -0
- data/test/fixtures/bad.png +1 -0
- data/test/fixtures/fog.yml +8 -0
- data/test/fixtures/s3.yml +8 -0
- data/test/fixtures/spaced file.png +0 -0
- data/test/fixtures/text.txt +1 -0
- data/test/fixtures/twopage.pdf +0 -0
- data/test/fixtures/uppercase.PNG +0 -0
- data/test/geometry_test.rb +206 -0
- data/test/helper.rb +181 -0
- data/test/integration_test.rb +652 -0
- data/test/interpolations_test.rb +219 -0
- data/test/iostream_test.rb +71 -0
- data/test/matchers/have_attached_file_matcher_test.rb +24 -0
- data/test/matchers/validate_attachment_content_type_matcher_test.rb +110 -0
- data/test/matchers/validate_attachment_presence_matcher_test.rb +47 -0
- data/test/matchers/validate_attachment_size_matcher_test.rb +72 -0
- data/test/paperclip_missing_attachment_styles_test.rb +96 -0
- data/test/paperclip_test.rb +409 -0
- data/test/processor_test.rb +10 -0
- data/test/schema_test.rb +98 -0
- data/test/storage/filesystem_test.rb +62 -0
- data/test/storage/fog_test.rb +280 -0
- data/test/storage/s3_live_test.rb +138 -0
- data/test/storage/s3_test.rb +1093 -0
- data/test/style_test.rb +215 -0
- data/test/support/mock_attachment.rb +22 -0
- data/test/support/mock_interpolator.rb +24 -0
- data/test/support/mock_model.rb +2 -0
- data/test/support/mock_url_generator_builder.rb +27 -0
- data/test/thumbnail_test.rb +396 -0
- data/test/upfile_test.rb +53 -0
- data/test/url_generator_test.rb +187 -0
- metadata +374 -0
@@ -0,0 +1,72 @@
|
|
1
|
+
require './test/helper'
|
2
|
+
|
3
|
+
class ValidateAttachmentSizeMatcherTest < Test::Unit::TestCase
|
4
|
+
context "validate_attachment_size" do
|
5
|
+
setup do
|
6
|
+
reset_table("dummies") do |d|
|
7
|
+
d.string :avatar_file_name
|
8
|
+
d.integer :avatar_file_size
|
9
|
+
end
|
10
|
+
@dummy_class = reset_class "Dummy"
|
11
|
+
@dummy_class.has_attached_file :avatar
|
12
|
+
end
|
13
|
+
|
14
|
+
context "of limited size" do
|
15
|
+
setup{ @matcher = self.class.validate_attachment_size(:avatar).in(256..1024) }
|
16
|
+
|
17
|
+
context "given a class with no validation" do
|
18
|
+
should_reject_dummy_class
|
19
|
+
end
|
20
|
+
|
21
|
+
context "given a class with a validation that's too high" do
|
22
|
+
setup { @dummy_class.validates_attachment_size :avatar, :in => 256..2048 }
|
23
|
+
should_reject_dummy_class
|
24
|
+
end
|
25
|
+
|
26
|
+
context "given a class with a validation that's too low" do
|
27
|
+
setup { @dummy_class.validates_attachment_size :avatar, :in => 0..1024 }
|
28
|
+
should_reject_dummy_class
|
29
|
+
end
|
30
|
+
|
31
|
+
context "given a class with a validation that matches" do
|
32
|
+
setup { @dummy_class.validates_attachment_size :avatar, :in => 256..1024 }
|
33
|
+
should_accept_dummy_class
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
context "validates_attachment_size with infinite range" do
|
38
|
+
setup{ @matcher = self.class.validate_attachment_size(:avatar) }
|
39
|
+
|
40
|
+
context "given a class with an upper limit" do
|
41
|
+
setup { @dummy_class.validates_attachment_size :avatar, :less_than => 1 }
|
42
|
+
should_accept_dummy_class
|
43
|
+
end
|
44
|
+
|
45
|
+
context "given a class with no upper limit" do
|
46
|
+
setup { @dummy_class.validates_attachment_size :avatar, :greater_than => 1 }
|
47
|
+
should_accept_dummy_class
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
context "using an :if to control the validation" do
|
52
|
+
setup do
|
53
|
+
@dummy_class.class_eval do
|
54
|
+
validates_attachment_size :avatar, :greater_than => 1024, :if => :go
|
55
|
+
attr_accessor :go
|
56
|
+
end
|
57
|
+
@dummy = @dummy_class.new
|
58
|
+
@matcher = self.class.validate_attachment_size(:avatar).greater_than(1024)
|
59
|
+
end
|
60
|
+
|
61
|
+
should "run the validation if the control is true" do
|
62
|
+
@dummy.go = true
|
63
|
+
assert_accepts @matcher, @dummy
|
64
|
+
end
|
65
|
+
|
66
|
+
should "not run the validation if the control is false" do
|
67
|
+
@dummy.go = false
|
68
|
+
assert_rejects @matcher, @dummy
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,96 @@
|
|
1
|
+
require './test/helper'
|
2
|
+
|
3
|
+
class PaperclipMissingAttachmentStylesTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
context "Paperclip" do
|
6
|
+
setup do
|
7
|
+
Paperclip.classes_with_attachments = Set.new
|
8
|
+
end
|
9
|
+
|
10
|
+
teardown do
|
11
|
+
File.unlink(Paperclip.registered_attachments_styles_path) rescue nil
|
12
|
+
end
|
13
|
+
|
14
|
+
should "be able to keep list of models using it" do
|
15
|
+
assert_kind_of Set, Paperclip.classes_with_attachments
|
16
|
+
assert Paperclip.classes_with_attachments.empty?, 'list should be empty'
|
17
|
+
rebuild_model
|
18
|
+
assert_equal ['Dummy'].to_set, Paperclip.classes_with_attachments
|
19
|
+
end
|
20
|
+
|
21
|
+
should "enable to get and set path to registered styles file" do
|
22
|
+
assert_equal ROOT.join('public/system/paperclip_attachments.yml').to_s, Paperclip.registered_attachments_styles_path
|
23
|
+
Paperclip.registered_attachments_styles_path = '/tmp/config/paperclip_attachments.yml'
|
24
|
+
assert_equal '/tmp/config/paperclip_attachments.yml', Paperclip.registered_attachments_styles_path
|
25
|
+
Paperclip.registered_attachments_styles_path = nil
|
26
|
+
assert_equal ROOT.join('public/system/paperclip_attachments.yml').to_s, Paperclip.registered_attachments_styles_path
|
27
|
+
end
|
28
|
+
|
29
|
+
should "be able to get current attachment styles" do
|
30
|
+
assert_equal Hash.new, Paperclip.send(:current_attachments_styles)
|
31
|
+
rebuild_model :styles => {:croppable => '600x600>', :big => '1000x1000>'}
|
32
|
+
expected_hash = { :Dummy => {:avatar => [:big, :croppable]}}
|
33
|
+
assert_equal expected_hash, Paperclip.send(:current_attachments_styles)
|
34
|
+
end
|
35
|
+
|
36
|
+
should "be able to save current attachment styles for further comparison" do
|
37
|
+
rebuild_model :styles => {:croppable => '600x600>', :big => '1000x1000>'}
|
38
|
+
Paperclip.save_current_attachments_styles!
|
39
|
+
expected_hash = { :Dummy => {:avatar => [:big, :croppable]}}
|
40
|
+
assert_equal expected_hash, YAML.load_file(Paperclip.registered_attachments_styles_path)
|
41
|
+
end
|
42
|
+
|
43
|
+
should "be able to read registered attachment styles from file" do
|
44
|
+
rebuild_model :styles => {:croppable => '600x600>', :big => '1000x1000>'}
|
45
|
+
Paperclip.save_current_attachments_styles!
|
46
|
+
expected_hash = { :Dummy => {:avatar => [:big, :croppable]}}
|
47
|
+
assert_equal expected_hash, Paperclip.send(:get_registered_attachments_styles)
|
48
|
+
end
|
49
|
+
|
50
|
+
should "be able to calculate differences between registered styles and current styles" do
|
51
|
+
rebuild_model :styles => {:croppable => '600x600>', :big => '1000x1000>'}
|
52
|
+
Paperclip.save_current_attachments_styles!
|
53
|
+
rebuild_model :styles => {:thumb => 'x100', :export => 'x400>', :croppable => '600x600>', :big => '1000x1000>'}
|
54
|
+
expected_hash = { :Dummy => {:avatar => [:export, :thumb]} }
|
55
|
+
assert_equal expected_hash, Paperclip.missing_attachments_styles
|
56
|
+
|
57
|
+
ActiveRecord::Base.connection.create_table :books, :force => true
|
58
|
+
class ::Book < ActiveRecord::Base
|
59
|
+
has_attached_file :cover, :styles => {:small => 'x100', :large => '1000x1000>'}
|
60
|
+
has_attached_file :sample, :styles => {:thumb => 'x100'}
|
61
|
+
end
|
62
|
+
|
63
|
+
expected_hash = {
|
64
|
+
:Dummy => {:avatar => [:export, :thumb]},
|
65
|
+
:Book => {:sample => [:thumb], :cover => [:large, :small]}
|
66
|
+
}
|
67
|
+
assert_equal expected_hash, Paperclip.missing_attachments_styles
|
68
|
+
Paperclip.save_current_attachments_styles!
|
69
|
+
assert_equal Hash.new, Paperclip.missing_attachments_styles
|
70
|
+
end
|
71
|
+
|
72
|
+
should "be able to calculate differences when a new attachment is added to a model" do
|
73
|
+
rebuild_model :styles => {:croppable => '600x600>', :big => '1000x1000>'}
|
74
|
+
Paperclip.save_current_attachments_styles!
|
75
|
+
|
76
|
+
class ::Dummy
|
77
|
+
has_attached_file :photo, :styles => {:small => 'x100', :large => '1000x1000>'}
|
78
|
+
end
|
79
|
+
|
80
|
+
expected_hash = {
|
81
|
+
:Dummy => {:photo => [:large, :small]}
|
82
|
+
}
|
83
|
+
assert_equal expected_hash, Paperclip.missing_attachments_styles
|
84
|
+
Paperclip.save_current_attachments_styles!
|
85
|
+
assert_equal Hash.new, Paperclip.missing_attachments_styles
|
86
|
+
end
|
87
|
+
|
88
|
+
# It's impossible to build styles hash without loading from database whole bunch of records
|
89
|
+
should "skip lambda-styles" do
|
90
|
+
rebuild_model :styles => lambda{ |attachment| attachment.instance.other == 'a' ? {:thumb => "50x50#"} : {:large => "400x400"} }
|
91
|
+
assert_equal Hash.new, Paperclip.send(:current_attachments_styles)
|
92
|
+
end
|
93
|
+
|
94
|
+
end
|
95
|
+
|
96
|
+
end
|
@@ -0,0 +1,409 @@
|
|
1
|
+
require './test/helper'
|
2
|
+
|
3
|
+
class PaperclipTest < Test::Unit::TestCase
|
4
|
+
context "Calling Paperclip.run" do
|
5
|
+
setup do
|
6
|
+
Paperclip.options[:log_command] = false
|
7
|
+
Cocaine::CommandLine.expects(:new).with("convert", "stuff", {}).returns(stub(:run))
|
8
|
+
@original_command_line_path = Cocaine::CommandLine.path
|
9
|
+
end
|
10
|
+
|
11
|
+
teardown do
|
12
|
+
Paperclip.options[:log_command] = true
|
13
|
+
Cocaine::CommandLine.path = @original_command_line_path
|
14
|
+
end
|
15
|
+
|
16
|
+
should "run the command with Cocaine" do
|
17
|
+
Paperclip.run("convert", "stuff")
|
18
|
+
end
|
19
|
+
|
20
|
+
should "save Cocaine::CommandLine.path that set before" do
|
21
|
+
Cocaine::CommandLine.path = "/opt/my_app/bin"
|
22
|
+
Paperclip.run("convert", "stuff")
|
23
|
+
assert_equal [Cocaine::CommandLine.path].flatten.include?("/opt/my_app/bin"), true
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
should 'not raise errors when doing a lot of running' do
|
28
|
+
Paperclip.options[:command_path] = ["/usr/local/bin"] * 1024
|
29
|
+
Cocaine::CommandLine.path = "/something/else"
|
30
|
+
100.times do |x|
|
31
|
+
Paperclip.run("echo", x.to_s)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
context "Calling Paperclip.run with a logger" do
|
36
|
+
should "pass the defined logger if :log_command is set" do
|
37
|
+
Paperclip.options[:log_command] = true
|
38
|
+
Cocaine::CommandLine.expects(:new).with("convert", "stuff", :logger => Paperclip.logger).returns(stub(:run))
|
39
|
+
Paperclip.run("convert", "stuff")
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
context "Paperclip.each_instance_with_attachment" do
|
44
|
+
setup do
|
45
|
+
@file = File.new(File.join(FIXTURES_DIR, "5k.png"), 'rb')
|
46
|
+
d1 = Dummy.create(:avatar => @file)
|
47
|
+
d2 = Dummy.create
|
48
|
+
d3 = Dummy.create(:avatar => @file)
|
49
|
+
@expected = [d1, d3]
|
50
|
+
end
|
51
|
+
should "yield every instance of a model that has an attachment" do
|
52
|
+
actual = []
|
53
|
+
Paperclip.each_instance_with_attachment("Dummy", "avatar") do |instance|
|
54
|
+
actual << instance
|
55
|
+
end
|
56
|
+
assert_same_elements @expected, actual
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
should "raise when sent #processor and the name of a class that doesn't exist" do
|
61
|
+
assert_raises(NameError){ Paperclip.processor(:boogey_man) }
|
62
|
+
end
|
63
|
+
|
64
|
+
should "return a class when sent #processor and the name of a class under Paperclip" do
|
65
|
+
assert_equal ::Paperclip::Thumbnail, Paperclip.processor(:thumbnail)
|
66
|
+
end
|
67
|
+
|
68
|
+
should "get a class from a namespaced class name" do
|
69
|
+
class ::One; class Two; end; end
|
70
|
+
assert_equal ::One::Two, Paperclip.class_for("One::Two")
|
71
|
+
end
|
72
|
+
|
73
|
+
should "raise when class doesn't exist in specified namespace" do
|
74
|
+
class ::Three; end
|
75
|
+
class ::Four; end
|
76
|
+
assert_raise NameError do
|
77
|
+
Paperclip.class_for("Three::Four")
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
context "Attachments with clashing URLs should raise error" do
|
82
|
+
setup do
|
83
|
+
class Dummy2 < ActiveRecord::Base
|
84
|
+
include Paperclip::Glue
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
should "generate warning if attachment is redefined with the same path string" do
|
89
|
+
expected_log_msg = "Duplicate path for blah with /system/:id/:style/:filename. This will clash with attachment defined in Dummy class"
|
90
|
+
Paperclip.expects(:log).with(expected_log_msg)
|
91
|
+
Dummy.class_eval do
|
92
|
+
has_attached_file :blah, :path => '/system/:id/:style/:filename'
|
93
|
+
end
|
94
|
+
Dummy2.class_eval do
|
95
|
+
has_attached_file :blah, :path => '/system/:id/:style/:filename'
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
should "not generate warning if attachment is redefined with the same path string but has :class in it" do
|
100
|
+
Paperclip.expects(:log).never
|
101
|
+
Dummy.class_eval do
|
102
|
+
has_attached_file :blah, :path => "/system/:class/:attachment/:id/:style/:filename"
|
103
|
+
end
|
104
|
+
Dummy2.class_eval do
|
105
|
+
has_attached_file :blah, :path => "/system/:class/:attachment/:id/:style/:filename"
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
should "not generate warning if attachment is defined with the same URL string" do
|
110
|
+
Paperclip.expects(:log).never
|
111
|
+
Dummy.class_eval do
|
112
|
+
has_attached_file :blah, :path => "/system/:class/:attachment/:id/:style/:filename", :url => ":s3_alias_url"
|
113
|
+
end
|
114
|
+
Dummy2.class_eval do
|
115
|
+
has_attached_file :blah, :path => "/system/:class/:attachment/:id/:style/:filename", :url => ":s3_alias_url"
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
context "An ActiveRecord model with an 'avatar' attachment" do
|
121
|
+
setup do
|
122
|
+
rebuild_model :path => "tmp/:class/omg/:style.:extension"
|
123
|
+
@file = File.new(File.join(FIXTURES_DIR, "5k.png"), 'rb')
|
124
|
+
end
|
125
|
+
|
126
|
+
teardown { @file.close }
|
127
|
+
|
128
|
+
should "not error when trying to also create a 'blah' attachment" do
|
129
|
+
assert_nothing_raised do
|
130
|
+
Dummy.class_eval do
|
131
|
+
has_attached_file :blah
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
context "that is attr_protected" do
|
137
|
+
setup do
|
138
|
+
Dummy.class_eval do
|
139
|
+
attr_protected :avatar
|
140
|
+
end
|
141
|
+
@dummy = Dummy.new
|
142
|
+
end
|
143
|
+
|
144
|
+
should "not assign the avatar on mass-set" do
|
145
|
+
@dummy.attributes = { :other => "I'm set!",
|
146
|
+
:avatar => @file }
|
147
|
+
|
148
|
+
assert_equal "I'm set!", @dummy.other
|
149
|
+
assert ! @dummy.avatar?
|
150
|
+
end
|
151
|
+
|
152
|
+
should "still allow assigment on normal set" do
|
153
|
+
@dummy.other = "I'm set!"
|
154
|
+
@dummy.avatar = @file
|
155
|
+
|
156
|
+
assert_equal "I'm set!", @dummy.other
|
157
|
+
assert @dummy.avatar?
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
context "with a subclass" do
|
162
|
+
setup do
|
163
|
+
class ::SubDummy < Dummy; end
|
164
|
+
end
|
165
|
+
|
166
|
+
should "be able to use the attachment from the subclass" do
|
167
|
+
assert_nothing_raised do
|
168
|
+
@subdummy = SubDummy.create(:avatar => @file)
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
172
|
+
should "be able to see the attachment definition from the subclass's class" do
|
173
|
+
assert_equal "tmp/:class/omg/:style.:extension",
|
174
|
+
SubDummy.attachment_definitions[:avatar][:path]
|
175
|
+
end
|
176
|
+
|
177
|
+
teardown do
|
178
|
+
Object.send(:remove_const, "SubDummy") rescue nil
|
179
|
+
end
|
180
|
+
end
|
181
|
+
|
182
|
+
should "have an #avatar method" do
|
183
|
+
assert Dummy.new.respond_to?(:avatar)
|
184
|
+
end
|
185
|
+
|
186
|
+
should "have an #avatar= method" do
|
187
|
+
assert Dummy.new.respond_to?(:avatar=)
|
188
|
+
end
|
189
|
+
|
190
|
+
context "that is valid" do
|
191
|
+
setup do
|
192
|
+
@dummy = Dummy.new
|
193
|
+
@dummy.avatar = @file
|
194
|
+
end
|
195
|
+
|
196
|
+
should "be valid" do
|
197
|
+
assert @dummy.valid?
|
198
|
+
end
|
199
|
+
end
|
200
|
+
|
201
|
+
context "a validation with an if guard clause" do
|
202
|
+
context "as a lambda" do
|
203
|
+
setup do
|
204
|
+
Dummy.send(:"validates_attachment_presence", :avatar, :if => lambda{|i| i.foo })
|
205
|
+
@dummy = Dummy.new
|
206
|
+
@dummy.stubs(:avatar_file_name).returns(nil)
|
207
|
+
end
|
208
|
+
|
209
|
+
should "attempt validation if the guard returns true" do
|
210
|
+
@dummy.expects(:foo).returns(true)
|
211
|
+
assert ! @dummy.valid?
|
212
|
+
end
|
213
|
+
|
214
|
+
should "not attempt validation if the guard returns false" do
|
215
|
+
@dummy.expects(:foo).returns(false)
|
216
|
+
assert @dummy.valid?
|
217
|
+
end
|
218
|
+
end
|
219
|
+
|
220
|
+
context "as a method name" do
|
221
|
+
setup do
|
222
|
+
Dummy.send(:"validates_attachment_presence", :avatar, :if => :foo)
|
223
|
+
@dummy = Dummy.new
|
224
|
+
@dummy.stubs(:avatar_file_name).returns(nil)
|
225
|
+
end
|
226
|
+
|
227
|
+
should "attempt validation if the guard returns true" do
|
228
|
+
@dummy.expects(:foo).returns(true)
|
229
|
+
assert ! @dummy.valid?
|
230
|
+
end
|
231
|
+
|
232
|
+
should "not attempt validation if the guard returns false" do
|
233
|
+
@dummy.expects(:foo).returns(false)
|
234
|
+
assert @dummy.valid?
|
235
|
+
end
|
236
|
+
end
|
237
|
+
end
|
238
|
+
|
239
|
+
context "a validation with an unless guard clause" do
|
240
|
+
context "as a lambda" do
|
241
|
+
setup do
|
242
|
+
Dummy.send(:"validates_attachment_presence", :avatar, :unless => lambda{|i| i.foo })
|
243
|
+
@dummy = Dummy.new
|
244
|
+
@dummy.stubs(:avatar_file_name).returns(nil)
|
245
|
+
end
|
246
|
+
|
247
|
+
should "attempt validation if the guard returns true" do
|
248
|
+
@dummy.expects(:foo).returns(false)
|
249
|
+
assert ! @dummy.valid?
|
250
|
+
end
|
251
|
+
|
252
|
+
should "not attempt validation if the guard returns false" do
|
253
|
+
@dummy.expects(:foo).returns(true)
|
254
|
+
assert @dummy.valid?
|
255
|
+
end
|
256
|
+
end
|
257
|
+
|
258
|
+
context "as a method name" do
|
259
|
+
setup do
|
260
|
+
Dummy.send(:"validates_attachment_presence", :avatar, :unless => :foo)
|
261
|
+
@dummy = Dummy.new
|
262
|
+
@dummy.stubs(:avatar_file_name).returns(nil)
|
263
|
+
end
|
264
|
+
|
265
|
+
should "attempt validation if the guard returns true" do
|
266
|
+
@dummy.expects(:foo).returns(false)
|
267
|
+
assert ! @dummy.valid?
|
268
|
+
end
|
269
|
+
|
270
|
+
should "not attempt validation if the guard returns false" do
|
271
|
+
@dummy.expects(:foo).returns(true)
|
272
|
+
assert @dummy.valid?
|
273
|
+
end
|
274
|
+
end
|
275
|
+
end
|
276
|
+
|
277
|
+
should "not have Attachment in the ActiveRecord::Base namespace" do
|
278
|
+
assert_raises(NameError) do
|
279
|
+
ActiveRecord::Base::Attachment
|
280
|
+
end
|
281
|
+
end
|
282
|
+
|
283
|
+
def self.should_validate validation, options, valid_file, invalid_file
|
284
|
+
context "with #{validation} validation and #{options.inspect} options" do
|
285
|
+
setup do
|
286
|
+
rebuild_class
|
287
|
+
Dummy.send(:"validates_attachment_#{validation}", :avatar, options)
|
288
|
+
@dummy = Dummy.new
|
289
|
+
end
|
290
|
+
context "and assigning nil" do
|
291
|
+
setup do
|
292
|
+
@dummy.avatar = nil
|
293
|
+
@dummy.valid?
|
294
|
+
end
|
295
|
+
if validation == :presence
|
296
|
+
should "have an error on the attachment" do
|
297
|
+
assert @dummy.errors[:avatar]
|
298
|
+
assert @dummy.errors[:avatar_file_name]
|
299
|
+
end
|
300
|
+
else
|
301
|
+
should "not have an error on the attachment" do
|
302
|
+
assert @dummy.errors.blank?, @dummy.errors.full_messages.join(", ")
|
303
|
+
end
|
304
|
+
end
|
305
|
+
end
|
306
|
+
context "and assigned a valid file" do
|
307
|
+
setup do
|
308
|
+
@dummy.avatar = valid_file
|
309
|
+
@dummy.valid?
|
310
|
+
end
|
311
|
+
should "not have an error" do
|
312
|
+
assert_equal 0, @dummy.errors.size, @dummy.errors.full_messages.join(", ")
|
313
|
+
end
|
314
|
+
end
|
315
|
+
context "and assigned an invalid file" do
|
316
|
+
setup do
|
317
|
+
@dummy.avatar = invalid_file
|
318
|
+
@dummy.valid?
|
319
|
+
end
|
320
|
+
should "have an error" do
|
321
|
+
assert @dummy.errors.size > 0
|
322
|
+
end
|
323
|
+
end
|
324
|
+
end
|
325
|
+
end
|
326
|
+
|
327
|
+
[[:presence, {}, "5k.png", nil],
|
328
|
+
[:size, {:in => 1..10240}, "5k.png", "12k.png"],
|
329
|
+
[:size, {:less_than => 10240}, "5k.png", "12k.png"],
|
330
|
+
[:size, {:greater_than => 8096}, "12k.png", "5k.png"],
|
331
|
+
[:content_type, {:content_type => "image/png"}, "5k.png", "text.txt"],
|
332
|
+
[:content_type, {:content_type => "text/plain"}, "text.txt", "5k.png"],
|
333
|
+
[:content_type, {:content_type => %r{image/.*}}, "5k.png", "text.txt"]].each do |args|
|
334
|
+
validation, options, valid_file, invalid_file = args
|
335
|
+
valid_file &&= File.open(File.join(FIXTURES_DIR, valid_file), "rb")
|
336
|
+
invalid_file &&= File.open(File.join(FIXTURES_DIR, invalid_file), "rb")
|
337
|
+
|
338
|
+
should_validate validation, options, valid_file, invalid_file
|
339
|
+
end
|
340
|
+
|
341
|
+
context "with content_type validation and lambda message" do
|
342
|
+
context "and assigned an invalid file" do
|
343
|
+
setup do
|
344
|
+
Dummy.send(:"validates_attachment_content_type", :avatar, :content_type => %r{image/.*}, :message => lambda {'lambda content type message'})
|
345
|
+
@dummy = Dummy.new
|
346
|
+
@dummy.avatar &&= File.open(File.join(FIXTURES_DIR, "text.txt"), "rb")
|
347
|
+
@dummy.valid?
|
348
|
+
end
|
349
|
+
|
350
|
+
should "have a content type error message" do
|
351
|
+
assert [@dummy.errors[:avatar_content_type]].flatten.any?{|error| error =~ %r/lambda content type message/ }
|
352
|
+
end
|
353
|
+
end
|
354
|
+
end
|
355
|
+
|
356
|
+
context "with size validation and less_than 10240 option" do
|
357
|
+
context "and assigned an invalid file" do
|
358
|
+
setup do
|
359
|
+
Dummy.send(:"validates_attachment_size", :avatar, :less_than => 10240)
|
360
|
+
@dummy = Dummy.new
|
361
|
+
@dummy.avatar &&= File.open(File.join(FIXTURES_DIR, "12k.png"), "rb")
|
362
|
+
@dummy.valid?
|
363
|
+
end
|
364
|
+
|
365
|
+
should "have a file size min/max error message" do
|
366
|
+
assert [@dummy.errors[:avatar_file_size]].flatten.any?{|error| error =~ %r/between 0 and 10240 bytes/ }
|
367
|
+
end
|
368
|
+
end
|
369
|
+
end
|
370
|
+
|
371
|
+
context "with size validation and less_than 10240 option with lambda message" do
|
372
|
+
context "and assigned an invalid file" do
|
373
|
+
setup do
|
374
|
+
Dummy.send(:"validates_attachment_size", :avatar, :less_than => 10240, :message => lambda {'lambda between 0 and 10240 bytes'})
|
375
|
+
@dummy = Dummy.new
|
376
|
+
@dummy.avatar &&= File.open(File.join(FIXTURES_DIR, "12k.png"), "rb")
|
377
|
+
@dummy.valid?
|
378
|
+
end
|
379
|
+
|
380
|
+
should "have a file size min/max error message" do
|
381
|
+
assert [@dummy.errors[:avatar_file_size]].flatten.any?{|error| error =~ %r/lambda between 0 and 10240 bytes/ }
|
382
|
+
end
|
383
|
+
end
|
384
|
+
end
|
385
|
+
|
386
|
+
end
|
387
|
+
|
388
|
+
context "configuring a custom processor" do
|
389
|
+
setup do
|
390
|
+
@freedom_processor = Class.new do
|
391
|
+
def make(file, options = {}, attachment = nil)
|
392
|
+
file
|
393
|
+
end
|
394
|
+
end.new
|
395
|
+
|
396
|
+
Paperclip.configure do |config|
|
397
|
+
config.register_processor(:freedom, @freedom_processor)
|
398
|
+
end
|
399
|
+
end
|
400
|
+
|
401
|
+
should "be able to find the custom processor" do
|
402
|
+
assert_equal @freedom_processor, Paperclip.processor(:freedom)
|
403
|
+
end
|
404
|
+
|
405
|
+
teardown do
|
406
|
+
Paperclip.clear_processors!
|
407
|
+
end
|
408
|
+
end
|
409
|
+
end
|