cloudfuji_paperclip 2.4.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (105) hide show
  1. data/.gitignore +22 -0
  2. data/.travis.yml +13 -0
  3. data/Appraisals +14 -0
  4. data/CONTRIBUTING.md +38 -0
  5. data/Gemfile +5 -0
  6. data/Gemfile.lock +137 -0
  7. data/LICENSE +26 -0
  8. data/README.md +444 -0
  9. data/Rakefile +41 -0
  10. data/cucumber/paperclip_steps.rb +6 -0
  11. data/features/basic_integration.feature +46 -0
  12. data/features/rake_tasks.feature +63 -0
  13. data/features/step_definitions/attachment_steps.rb +65 -0
  14. data/features/step_definitions/html_steps.rb +15 -0
  15. data/features/step_definitions/rails_steps.rb +182 -0
  16. data/features/step_definitions/s3_steps.rb +14 -0
  17. data/features/step_definitions/web_steps.rb +209 -0
  18. data/features/support/env.rb +8 -0
  19. data/features/support/fakeweb.rb +3 -0
  20. data/features/support/fixtures/.boot_config.rb.swo +0 -0
  21. data/features/support/fixtures/boot_config.txt +15 -0
  22. data/features/support/fixtures/gemfile.txt +5 -0
  23. data/features/support/fixtures/preinitializer.txt +20 -0
  24. data/features/support/paths.rb +28 -0
  25. data/features/support/rails.rb +46 -0
  26. data/features/support/selectors.rb +19 -0
  27. data/gemfiles/rails2.gemfile +9 -0
  28. data/gemfiles/rails3.gemfile +9 -0
  29. data/gemfiles/rails3_1.gemfile +9 -0
  30. data/generators/paperclip/USAGE +5 -0
  31. data/generators/paperclip/paperclip_generator.rb +27 -0
  32. data/generators/paperclip/templates/paperclip_migration.rb.erb +19 -0
  33. data/init.rb +4 -0
  34. data/lib/generators/paperclip/USAGE +8 -0
  35. data/lib/generators/paperclip/paperclip_generator.rb +33 -0
  36. data/lib/generators/paperclip/templates/paperclip_migration.rb.erb +19 -0
  37. data/lib/paperclip/attachment.rb +468 -0
  38. data/lib/paperclip/callback_compatibility.rb +61 -0
  39. data/lib/paperclip/geometry.rb +120 -0
  40. data/lib/paperclip/interpolations.rb +174 -0
  41. data/lib/paperclip/iostream.rb +45 -0
  42. data/lib/paperclip/matchers/have_attached_file_matcher.rb +57 -0
  43. data/lib/paperclip/matchers/validate_attachment_content_type_matcher.rb +81 -0
  44. data/lib/paperclip/matchers/validate_attachment_presence_matcher.rb +54 -0
  45. data/lib/paperclip/matchers/validate_attachment_size_matcher.rb +95 -0
  46. data/lib/paperclip/matchers.rb +64 -0
  47. data/lib/paperclip/missing_attachment_styles.rb +87 -0
  48. data/lib/paperclip/processor.rb +58 -0
  49. data/lib/paperclip/railtie.rb +31 -0
  50. data/lib/paperclip/schema.rb +39 -0
  51. data/lib/paperclip/storage/filesystem.rb +81 -0
  52. data/lib/paperclip/storage/fog.rb +174 -0
  53. data/lib/paperclip/storage/s3.rb +333 -0
  54. data/lib/paperclip/storage.rb +3 -0
  55. data/lib/paperclip/style.rb +103 -0
  56. data/lib/paperclip/thumbnail.rb +105 -0
  57. data/lib/paperclip/upfile.rb +62 -0
  58. data/lib/paperclip/url_generator.rb +64 -0
  59. data/lib/paperclip/version.rb +3 -0
  60. data/lib/paperclip.rb +487 -0
  61. data/lib/tasks/paperclip.rake +101 -0
  62. data/paperclip.gemspec +41 -0
  63. data/rails/init.rb +2 -0
  64. data/shoulda_macros/paperclip.rb +124 -0
  65. data/test/.gitignore +1 -0
  66. data/test/attachment_test.rb +1116 -0
  67. data/test/database.yml +4 -0
  68. data/test/fixtures/12k.png +0 -0
  69. data/test/fixtures/50x50.png +0 -0
  70. data/test/fixtures/5k.png +0 -0
  71. data/test/fixtures/animated.gif +0 -0
  72. data/test/fixtures/bad.png +1 -0
  73. data/test/fixtures/fog.yml +8 -0
  74. data/test/fixtures/question?mark.png +0 -0
  75. data/test/fixtures/s3.yml +8 -0
  76. data/test/fixtures/spaced file.png +0 -0
  77. data/test/fixtures/text.txt +1 -0
  78. data/test/fixtures/twopage.pdf +0 -0
  79. data/test/fixtures/uppercase.PNG +0 -0
  80. data/test/geometry_test.rb +206 -0
  81. data/test/helper.rb +177 -0
  82. data/test/integration_test.rb +654 -0
  83. data/test/interpolations_test.rb +216 -0
  84. data/test/iostream_test.rb +71 -0
  85. data/test/matchers/have_attached_file_matcher_test.rb +24 -0
  86. data/test/matchers/validate_attachment_content_type_matcher_test.rb +87 -0
  87. data/test/matchers/validate_attachment_presence_matcher_test.rb +26 -0
  88. data/test/matchers/validate_attachment_size_matcher_test.rb +51 -0
  89. data/test/paperclip_missing_attachment_styles_test.rb +80 -0
  90. data/test/paperclip_test.rb +390 -0
  91. data/test/processor_test.rb +10 -0
  92. data/test/schema_test.rb +98 -0
  93. data/test/storage/filesystem_test.rb +56 -0
  94. data/test/storage/fog_test.rb +219 -0
  95. data/test/storage/s3_live_test.rb +138 -0
  96. data/test/storage/s3_test.rb +943 -0
  97. data/test/style_test.rb +209 -0
  98. data/test/support/mock_attachment.rb +22 -0
  99. data/test/support/mock_interpolator.rb +24 -0
  100. data/test/support/mock_model.rb +2 -0
  101. data/test/support/mock_url_generator_builder.rb +27 -0
  102. data/test/thumbnail_test.rb +383 -0
  103. data/test/upfile_test.rb +53 -0
  104. data/test/url_generator_test.rb +187 -0
  105. metadata +404 -0
@@ -0,0 +1,216 @@
1
+ require './test/helper'
2
+
3
+ class InterpolationsTest < Test::Unit::TestCase
4
+ should "return all methods but the infrastructure when sent #all" do
5
+ methods = Paperclip::Interpolations.all
6
+ assert ! methods.include?(:[])
7
+ assert ! methods.include?(:[]=)
8
+ assert ! methods.include?(:all)
9
+ methods.each do |m|
10
+ assert Paperclip::Interpolations.respond_to?(m)
11
+ end
12
+ end
13
+
14
+ should "return the Rails.root" do
15
+ assert_equal Rails.root, Paperclip::Interpolations.rails_root(:attachment, :style)
16
+ end
17
+
18
+ should "return the Rails.env" do
19
+ assert_equal Rails.env, Paperclip::Interpolations.rails_env(:attachment, :style)
20
+ end
21
+
22
+ should "return the class of the Interpolations module when called with no params" do
23
+ assert_equal Module, Paperclip::Interpolations.class
24
+ end
25
+
26
+ should "return the class of the instance" do
27
+ attachment = mock
28
+ attachment.expects(:instance).returns(attachment)
29
+ attachment.expects(:class).returns("Thing")
30
+ assert_equal "things", Paperclip::Interpolations.class(attachment, :style)
31
+ end
32
+
33
+ should "return the basename of the file" do
34
+ attachment = mock
35
+ attachment.expects(:original_filename).returns("one.jpg").times(2)
36
+ assert_equal "one", Paperclip::Interpolations.basename(attachment, :style)
37
+ end
38
+
39
+ should "return the extension of the file" do
40
+ attachment = mock
41
+ attachment.expects(:original_filename).returns("one.jpg")
42
+ attachment.expects(:styles).returns({})
43
+ assert_equal "jpg", Paperclip::Interpolations.extension(attachment, :style)
44
+ end
45
+
46
+ should "return the extension of the file as the format if defined in the style" do
47
+ attachment = mock
48
+ attachment.expects(:original_filename).never
49
+ attachment.expects(:styles).returns({:style => {:format => "png"}})
50
+ assert_equal "png", Paperclip::Interpolations.extension(attachment, :style)
51
+ end
52
+
53
+ should "return the extension of the file based on the content type" do
54
+ attachment = mock
55
+ attachment.expects(:content_type).returns('image/jpeg')
56
+ interpolations = Paperclip::Interpolations
57
+ interpolations.expects(:extension).returns('random')
58
+ assert_equal "jpeg", interpolations.content_type_extension(attachment, :style)
59
+ end
60
+
61
+ should "return the original extension of the file if it matches a content type extension" do
62
+ attachment = mock
63
+ attachment.expects(:content_type).returns('image/jpeg')
64
+ interpolations = Paperclip::Interpolations
65
+ interpolations.expects(:extension).returns('jpe')
66
+ assert_equal "jpe", interpolations.content_type_extension(attachment, :style)
67
+ end
68
+
69
+ should "return the latter half of the content type of the extension if no match found" do
70
+ attachment = mock
71
+ attachment.expects(:content_type).at_least_once().returns('not/found')
72
+ interpolations = Paperclip::Interpolations
73
+ interpolations.expects(:extension).returns('random')
74
+ assert_equal "found", interpolations.content_type_extension(attachment, :style)
75
+ end
76
+
77
+ should "return the #to_param of the attachment" do
78
+ attachment = mock
79
+ attachment.expects(:to_param).returns("23-awesome")
80
+ attachment.expects(:instance).returns(attachment)
81
+ assert_equal "23-awesome", Paperclip::Interpolations.param(attachment, :style)
82
+ end
83
+
84
+ should "return the id of the attachment" do
85
+ attachment = mock
86
+ attachment.expects(:id).returns(23)
87
+ attachment.expects(:instance).returns(attachment)
88
+ assert_equal 23, Paperclip::Interpolations.id(attachment, :style)
89
+ end
90
+
91
+ should "return nil for attachments to new records" do
92
+ attachment = mock
93
+ attachment.expects(:id).returns(nil)
94
+ attachment.expects(:instance).returns(attachment)
95
+ assert_nil Paperclip::Interpolations.id(attachment, :style)
96
+ end
97
+
98
+ should "return the partitioned id of the attachment when the id is an integer" do
99
+ attachment = mock
100
+ attachment.expects(:id).returns(23)
101
+ attachment.expects(:instance).returns(attachment)
102
+ assert_equal "000/000/023", Paperclip::Interpolations.id_partition(attachment, :style)
103
+ end
104
+
105
+ should "return the partitioned id of the attachment when the id is a string" do
106
+ attachment = mock
107
+ attachment.expects(:id).returns("32fnj23oio2f")
108
+ attachment.expects(:instance).returns(attachment)
109
+ assert_equal "32f/nj2/3oi", Paperclip::Interpolations.id_partition(attachment, :style)
110
+ end
111
+
112
+ should "return nil for the partitioned id of an attachment to a new record (when the id is nil)" do
113
+ attachment = mock
114
+ attachment.expects(:id).returns(nil)
115
+ attachment.expects(:instance).returns(attachment)
116
+ assert_nil Paperclip::Interpolations.id_partition(attachment, :style)
117
+ end
118
+
119
+ should "return the name of the attachment" do
120
+ attachment = mock
121
+ attachment.expects(:name).returns("file")
122
+ assert_equal "files", Paperclip::Interpolations.attachment(attachment, :style)
123
+ end
124
+
125
+ should "return the style" do
126
+ assert_equal :style, Paperclip::Interpolations.style(:attachment, :style)
127
+ end
128
+
129
+ should "return the default style" do
130
+ attachment = mock
131
+ attachment.expects(:default_style).returns(:default_style)
132
+ assert_equal :default_style, Paperclip::Interpolations.style(attachment, nil)
133
+ end
134
+
135
+ should "reinterpolate :url" do
136
+ attachment = mock
137
+ attachment.expects(:url).with(:style, :timestamp => false, :escape => false).returns("1234")
138
+ assert_equal "1234", Paperclip::Interpolations.url(attachment, :style)
139
+ end
140
+
141
+ should "raise if infinite loop detcted reinterpolating :url" do
142
+ attachment = Object.new
143
+ class << attachment
144
+ def url(*args)
145
+ Paperclip::Interpolations.url(self, :style)
146
+ end
147
+ end
148
+ assert_raises(Paperclip::InfiniteInterpolationError){ Paperclip::Interpolations.url(attachment, :style) }
149
+ end
150
+
151
+ should "return the filename as basename.extension" do
152
+ attachment = mock
153
+ attachment.expects(:styles).returns({})
154
+ attachment.expects(:original_filename).returns("one.jpg").times(3)
155
+ assert_equal "one.jpg", Paperclip::Interpolations.filename(attachment, :style)
156
+ end
157
+
158
+ should "return the filename as basename.extension when format supplied" do
159
+ attachment = mock
160
+ attachment.expects(:styles).returns({:style => {:format => :png}})
161
+ attachment.expects(:original_filename).returns("one.jpg").times(2)
162
+ assert_equal "one.png", Paperclip::Interpolations.filename(attachment, :style)
163
+ end
164
+
165
+ should "return the filename as basename when extension is blank" do
166
+ attachment = mock
167
+ attachment.stubs(:styles).returns({})
168
+ attachment.stubs(:original_filename).returns("one")
169
+ assert_equal "one", Paperclip::Interpolations.filename(attachment, :style)
170
+ end
171
+
172
+ should "return the basename when the extension contains regexp special characters" do
173
+ attachment = mock
174
+ attachment.stubs(:styles).returns({})
175
+ attachment.stubs(:original_filename).returns("one.ab)")
176
+ assert_equal "one", Paperclip::Interpolations.basename(attachment, :style)
177
+ end
178
+
179
+ should "return the timestamp" do
180
+ now = Time.now
181
+ zone = 'UTC'
182
+ attachment = mock
183
+ attachment.expects(:instance_read).with(:updated_at).returns(now)
184
+ attachment.expects(:time_zone).returns(zone)
185
+ assert_equal now.in_time_zone(zone).to_s, Paperclip::Interpolations.timestamp(attachment, :style)
186
+ end
187
+
188
+ should "return updated_at" do
189
+ attachment = mock
190
+ seconds_since_epoch = 1234567890
191
+ attachment.expects(:updated_at).returns(seconds_since_epoch)
192
+ assert_equal seconds_since_epoch, Paperclip::Interpolations.updated_at(attachment, :style)
193
+ end
194
+
195
+ should "return attachment's hash when passing both arguments" do
196
+ attachment = mock
197
+ fake_hash = "a_wicked_secure_hash"
198
+ attachment.expects(:hash).returns(fake_hash)
199
+ assert_equal fake_hash, Paperclip::Interpolations.hash(attachment, :style)
200
+ end
201
+
202
+ should "return Object#hash when passing no argument" do
203
+ attachment = mock
204
+ fake_hash = "a_wicked_secure_hash"
205
+ attachment.expects(:hash).never.returns(fake_hash)
206
+ assert_not_equal fake_hash, Paperclip::Interpolations.hash
207
+ end
208
+
209
+ should "call all expected interpolations with the given arguments" do
210
+ Paperclip::Interpolations.expects(:id).with(:attachment, :style).returns(1234)
211
+ Paperclip::Interpolations.expects(:attachment).with(:attachment, :style).returns("attachments")
212
+ Paperclip::Interpolations.expects(:notreal).never
213
+ value = Paperclip::Interpolations.interpolate(":notreal/:id/:attachment", :attachment, :style)
214
+ assert_equal ":notreal/1234/attachments", value
215
+ end
216
+ end
@@ -0,0 +1,71 @@
1
+ require './test/helper'
2
+
3
+ class IOStreamTest < Test::Unit::TestCase
4
+ include IOStream
5
+ context "A file" do
6
+ setup do
7
+ @file = File.new(File.join(File.dirname(__FILE__), "fixtures", "5k.png"), 'rb')
8
+ end
9
+
10
+ teardown { @file.close }
11
+
12
+ context "that is sent #stream_to" do
13
+
14
+ context "and given a String" do
15
+ setup do
16
+ FileUtils.mkdir_p(File.join(ROOT, 'tmp'))
17
+ assert @result = stream_to(@file, File.join(ROOT, 'tmp', 'iostream.string.test'))
18
+ end
19
+
20
+ should "return a File" do
21
+ assert @result.is_a?(File)
22
+ end
23
+
24
+ should "contain the same data as the original file" do
25
+ @file.rewind; @result.rewind
26
+ assert_equal @file.read, @result.read
27
+ end
28
+ end
29
+
30
+ context "and given a Tempfile" do
31
+ setup do
32
+ tempfile = Tempfile.new('iostream.test')
33
+ tempfile.binmode
34
+ assert @result = stream_to(@file, tempfile)
35
+ end
36
+
37
+ should "return a Tempfile" do
38
+ assert @result.is_a?(Tempfile)
39
+ end
40
+
41
+ should "contain the same data as the original file" do
42
+ @file.rewind; @result.rewind
43
+ assert_equal @file.read, @result.read
44
+ end
45
+ end
46
+
47
+ end
48
+
49
+ context "that is converted #to_tempfile" do
50
+ setup do
51
+ assert @tempfile = to_tempfile(@file)
52
+ end
53
+
54
+ should "convert it to a Paperclip Tempfile" do
55
+ assert @tempfile.is_a?(Paperclip::Tempfile)
56
+ end
57
+
58
+ should "have the name be based on the original_filename" do
59
+ name = File.basename(@file.path)
60
+ extension = File.extname(name)
61
+ basename = File.basename(name, extension)
62
+ assert_match %r[^stream.*?#{Regexp.quote(extension)}], File.basename(@tempfile.path)
63
+ end
64
+
65
+ should "have the Tempfile contain the same data as the file" do
66
+ @file.rewind; @tempfile.rewind
67
+ assert_equal @file.read, @tempfile.read
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,24 @@
1
+ require './test/helper'
2
+
3
+ class HaveAttachedFileMatcherTest < Test::Unit::TestCase
4
+ context "have_attached_file" do
5
+ setup do
6
+ @dummy_class = reset_class "Dummy"
7
+ reset_table "dummies"
8
+ @matcher = self.class.have_attached_file(:avatar)
9
+ end
10
+
11
+ context "given a class with no attachment" do
12
+ should_reject_dummy_class
13
+ end
14
+
15
+ context "given a class with an attachment" do
16
+ setup do
17
+ modify_table("dummies"){|d| d.string :avatar_file_name }
18
+ @dummy_class.has_attached_file :avatar
19
+ end
20
+
21
+ should_accept_dummy_class
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,87 @@
1
+ require './test/helper'
2
+
3
+ class ValidateAttachmentContentTypeMatcherTest < Test::Unit::TestCase
4
+ context "validate_attachment_content_type" do
5
+ setup do
6
+ reset_table("dummies") do |d|
7
+ d.string :title
8
+ d.string :avatar_file_name
9
+ d.string :avatar_content_type
10
+ end
11
+ @dummy_class = reset_class "Dummy"
12
+ @dummy_class.has_attached_file :avatar
13
+ @matcher = self.class.validate_attachment_content_type(:avatar).
14
+ allowing(%w(image/png image/jpeg)).
15
+ rejecting(%w(audio/mp3 application/octet-stream))
16
+ end
17
+
18
+ context "given a class with no validation" do
19
+ should_reject_dummy_class
20
+ end
21
+
22
+ context "given a class with a validation that doesn't match" do
23
+ setup do
24
+ @dummy_class.validates_attachment_content_type :avatar, :content_type => %r{audio/.*}
25
+ end
26
+
27
+ should_reject_dummy_class
28
+ end
29
+
30
+ context "given a class with a matching validation" do
31
+ setup do
32
+ @dummy_class.validates_attachment_content_type :avatar, :content_type => %r{image/.*}
33
+ end
34
+
35
+ should_accept_dummy_class
36
+ end
37
+
38
+ context "given a class with other validations but matching types" do
39
+ setup do
40
+ @dummy_class.validates_presence_of :title
41
+ @dummy_class.validates_attachment_content_type :avatar, :content_type => %r{image/.*}
42
+ end
43
+
44
+ should_accept_dummy_class
45
+ end
46
+
47
+ context "given a class that matches and a matcher that only specifies 'allowing'" do
48
+ setup do
49
+ @dummy_class.validates_attachment_content_type :avatar, :content_type => %r{image/.*}
50
+ @matcher = self.class.validate_attachment_content_type(:avatar).
51
+ allowing(%w(image/png image/jpeg))
52
+ end
53
+
54
+ should_accept_dummy_class
55
+ end
56
+
57
+ context "given a class that does not match and a matcher that only specifies 'allowing'" do
58
+ setup do
59
+ @dummy_class.validates_attachment_content_type :avatar, :content_type => %r{audio/.*}
60
+ @matcher = self.class.validate_attachment_content_type(:avatar).
61
+ allowing(%w(image/png image/jpeg))
62
+ end
63
+
64
+ should_reject_dummy_class
65
+ end
66
+
67
+ context "given a class that matches and a matcher that only specifies 'rejecting'" do
68
+ setup do
69
+ @dummy_class.validates_attachment_content_type :avatar, :content_type => %r{image/.*}
70
+ @matcher = self.class.validate_attachment_content_type(:avatar).
71
+ rejecting(%w(audio/mp3 application/octet-stream))
72
+ end
73
+
74
+ should_accept_dummy_class
75
+ end
76
+
77
+ context "given a class that does not match and a matcher that only specifies 'rejecting'" do
78
+ setup do
79
+ @dummy_class.validates_attachment_content_type :avatar, :content_type => %r{audio/.*}
80
+ @matcher = self.class.validate_attachment_content_type(:avatar).
81
+ rejecting(%w(audio/mp3 application/octet-stream))
82
+ end
83
+
84
+ should_reject_dummy_class
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,26 @@
1
+ require './test/helper'
2
+
3
+ class ValidateAttachmentPresenceMatcherTest < Test::Unit::TestCase
4
+ context "validate_attachment_presence" do
5
+ setup do
6
+ reset_table("dummies") do |d|
7
+ d.string :avatar_file_name
8
+ end
9
+ @dummy_class = reset_class "Dummy"
10
+ @dummy_class.has_attached_file :avatar
11
+ @matcher = self.class.validate_attachment_presence(:avatar)
12
+ end
13
+
14
+ context "given a class with no validation" do
15
+ should_reject_dummy_class
16
+ end
17
+
18
+ context "given a class with a matching validation" do
19
+ setup do
20
+ @dummy_class.validates_attachment_presence :avatar
21
+ end
22
+
23
+ should_accept_dummy_class
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,51 @@
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
+ end
51
+ end
@@ -0,0 +1,80 @@
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
+ # It's impossible to build styles hash without loading from database whole bunch of records
73
+ should "skip lambda-styles" do
74
+ rebuild_model :styles => lambda{ |attachment| attachment.instance.other == 'a' ? {:thumb => "50x50#"} : {:large => "400x400"} }
75
+ assert_equal Hash.new, Paperclip.send(:current_attachments_styles)
76
+ end
77
+
78
+ end
79
+
80
+ end