mender_paperclip 2.4.3

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.
Files changed (70) hide show
  1. data/LICENSE +26 -0
  2. data/README.md +402 -0
  3. data/Rakefile +86 -0
  4. data/generators/paperclip/USAGE +5 -0
  5. data/generators/paperclip/paperclip_generator.rb +27 -0
  6. data/generators/paperclip/templates/paperclip_migration.rb.erb +19 -0
  7. data/init.rb +4 -0
  8. data/lib/generators/paperclip/USAGE +8 -0
  9. data/lib/generators/paperclip/paperclip_generator.rb +33 -0
  10. data/lib/generators/paperclip/templates/paperclip_migration.rb.erb +19 -0
  11. data/lib/paperclip/attachment.rb +454 -0
  12. data/lib/paperclip/callback_compatibility.rb +61 -0
  13. data/lib/paperclip/geometry.rb +120 -0
  14. data/lib/paperclip/interpolations.rb +181 -0
  15. data/lib/paperclip/iostream.rb +45 -0
  16. data/lib/paperclip/matchers/have_attached_file_matcher.rb +57 -0
  17. data/lib/paperclip/matchers/validate_attachment_content_type_matcher.rb +81 -0
  18. data/lib/paperclip/matchers/validate_attachment_presence_matcher.rb +54 -0
  19. data/lib/paperclip/matchers/validate_attachment_size_matcher.rb +95 -0
  20. data/lib/paperclip/matchers.rb +33 -0
  21. data/lib/paperclip/missing_attachment_styles.rb +87 -0
  22. data/lib/paperclip/options.rb +79 -0
  23. data/lib/paperclip/processor.rb +58 -0
  24. data/lib/paperclip/railtie.rb +26 -0
  25. data/lib/paperclip/storage/filesystem.rb +81 -0
  26. data/lib/paperclip/storage/fog.rb +162 -0
  27. data/lib/paperclip/storage/s3.rb +262 -0
  28. data/lib/paperclip/storage.rb +3 -0
  29. data/lib/paperclip/style.rb +95 -0
  30. data/lib/paperclip/thumbnail.rb +105 -0
  31. data/lib/paperclip/upfile.rb +62 -0
  32. data/lib/paperclip/version.rb +3 -0
  33. data/lib/paperclip.rb +478 -0
  34. data/lib/tasks/paperclip.rake +97 -0
  35. data/rails/init.rb +2 -0
  36. data/shoulda_macros/paperclip.rb +124 -0
  37. data/test/attachment_test.rb +1120 -0
  38. data/test/database.yml +4 -0
  39. data/test/fixtures/12k.png +0 -0
  40. data/test/fixtures/50x50.png +0 -0
  41. data/test/fixtures/5k.png +0 -0
  42. data/test/fixtures/animated.gif +0 -0
  43. data/test/fixtures/bad.png +1 -0
  44. data/test/fixtures/fog.yml +8 -0
  45. data/test/fixtures/s3.yml +8 -0
  46. data/test/fixtures/spaced file.png +0 -0
  47. data/test/fixtures/text.txt +1 -0
  48. data/test/fixtures/twopage.pdf +0 -0
  49. data/test/fixtures/uppercase.PNG +0 -0
  50. data/test/fog_test.rb +191 -0
  51. data/test/geometry_test.rb +206 -0
  52. data/test/helper.rb +152 -0
  53. data/test/integration_test.rb +654 -0
  54. data/test/interpolations_test.rb +195 -0
  55. data/test/iostream_test.rb +71 -0
  56. data/test/matchers/have_attached_file_matcher_test.rb +24 -0
  57. data/test/matchers/validate_attachment_content_type_matcher_test.rb +87 -0
  58. data/test/matchers/validate_attachment_presence_matcher_test.rb +26 -0
  59. data/test/matchers/validate_attachment_size_matcher_test.rb +51 -0
  60. data/test/options_test.rb +68 -0
  61. data/test/paperclip_missing_attachment_styles_test.rb +80 -0
  62. data/test/paperclip_test.rb +329 -0
  63. data/test/processor_test.rb +10 -0
  64. data/test/storage/filesystem_test.rb +52 -0
  65. data/test/storage/s3_live_test.rb +51 -0
  66. data/test/storage/s3_test.rb +633 -0
  67. data/test/style_test.rb +180 -0
  68. data/test/thumbnail_test.rb +383 -0
  69. data/test/upfile_test.rb +53 -0
  70. metadata +243 -0
@@ -0,0 +1,195 @@
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 the partitioned id of the attachment when the id is an integer" do
92
+ attachment = mock
93
+ attachment.expects(:id).returns(23)
94
+ attachment.expects(:instance).returns(attachment)
95
+ assert_equal "000/000/023", Paperclip::Interpolations.id_partition(attachment, :style)
96
+ end
97
+
98
+ should "return the partitioned id of the attachment when the id is a string" do
99
+ attachment = mock
100
+ attachment.expects(:id).returns("32fnj23oio2f")
101
+ attachment.expects(:instance).returns(attachment)
102
+ assert_equal "32f/nj2/3oi", Paperclip::Interpolations.id_partition(attachment, :style)
103
+ end
104
+
105
+ should "return the name of the attachment" do
106
+ attachment = mock
107
+ attachment.expects(:name).returns("file")
108
+ assert_equal "files", Paperclip::Interpolations.attachment(attachment, :style)
109
+ end
110
+
111
+ should "return the style" do
112
+ assert_equal :style, Paperclip::Interpolations.style(:attachment, :style)
113
+ end
114
+
115
+ should "return the default style" do
116
+ attachment = mock
117
+ attachment.expects(:default_style).returns(:default_style)
118
+ assert_equal :default_style, Paperclip::Interpolations.style(attachment, nil)
119
+ end
120
+
121
+ should "reinterpolate :url" do
122
+ attachment = mock
123
+ attachment.expects(:url).with(:style, false).returns("1234")
124
+ assert_equal "1234", Paperclip::Interpolations.url(attachment, :style)
125
+ end
126
+
127
+ should "raise if infinite loop detcted reinterpolating :url" do
128
+ attachment = Object.new
129
+ class << attachment
130
+ def url(*args)
131
+ Paperclip::Interpolations.url(self, :style)
132
+ end
133
+ end
134
+ assert_raises(Paperclip::InfiniteInterpolationError){ Paperclip::Interpolations.url(attachment, :style) }
135
+ end
136
+
137
+ should "return the filename as basename.extension" do
138
+ attachment = mock
139
+ attachment.expects(:styles).returns({})
140
+ attachment.expects(:original_filename).returns("one.jpg").times(3)
141
+ assert_equal "one.jpg", Paperclip::Interpolations.filename(attachment, :style)
142
+ end
143
+
144
+ should "return the filename as basename.extension when format supplied" do
145
+ attachment = mock
146
+ attachment.expects(:styles).returns({:style => {:format => :png}})
147
+ attachment.expects(:original_filename).returns("one.jpg").times(2)
148
+ assert_equal "one.png", Paperclip::Interpolations.filename(attachment, :style)
149
+ end
150
+
151
+ should "return the filename as basename when extension is blank" do
152
+ attachment = mock
153
+ attachment.stubs(:styles).returns({})
154
+ attachment.stubs(:original_filename).returns("one")
155
+ assert_equal "one", Paperclip::Interpolations.filename(attachment, :style)
156
+ end
157
+
158
+ should "return the timestamp" do
159
+ now = Time.now
160
+ zone = 'UTC'
161
+ attachment = mock
162
+ attachment.expects(:instance_read).with(:updated_at).returns(now)
163
+ attachment.expects(:time_zone).returns(zone)
164
+ assert_equal now.in_time_zone(zone).to_s, Paperclip::Interpolations.timestamp(attachment, :style)
165
+ end
166
+
167
+ should "return updated_at" do
168
+ attachment = mock
169
+ seconds_since_epoch = 1234567890
170
+ attachment.expects(:updated_at).returns(seconds_since_epoch)
171
+ assert_equal seconds_since_epoch, Paperclip::Interpolations.updated_at(attachment, :style)
172
+ end
173
+
174
+ should "return attachment's hash when passing both arguments" do
175
+ attachment = mock
176
+ fake_hash = "a_wicked_secure_hash"
177
+ attachment.expects(:hash).returns(fake_hash)
178
+ assert_equal fake_hash, Paperclip::Interpolations.hash(attachment, :style)
179
+ end
180
+
181
+ should "return Object#hash when passing no argument" do
182
+ attachment = mock
183
+ fake_hash = "a_wicked_secure_hash"
184
+ attachment.expects(:hash).never.returns(fake_hash)
185
+ assert_not_equal fake_hash, Paperclip::Interpolations.hash
186
+ end
187
+
188
+ should "call all expected interpolations with the given arguments" do
189
+ Paperclip::Interpolations.expects(:id).with(:attachment, :style).returns(1234)
190
+ Paperclip::Interpolations.expects(:attachment).with(:attachment, :style).returns("attachments")
191
+ Paperclip::Interpolations.expects(:notreal).never
192
+ value = Paperclip::Interpolations.interpolate(":notreal/:id/:attachment", :attachment, :style)
193
+ assert_equal ":notreal/1234/attachments", value
194
+ end
195
+ 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,68 @@
1
+ # encoding: utf-8
2
+ require './test/helper'
3
+
4
+ class MockAttachment < Struct.new(:one, :two)
5
+ def instance
6
+ self
7
+ end
8
+ end
9
+
10
+ class OptionsTest < Test::Unit::TestCase
11
+ context "#styles with a plain hash" do
12
+ setup do
13
+ @attachment = MockAttachment.new(nil, nil)
14
+ @options = Paperclip::Options.new(@attachment,
15
+ :styles => {
16
+ :something => ["400x400", :png]
17
+ })
18
+ end
19
+
20
+ should "return the right data for the style's geometry" do
21
+ assert_equal "400x400", @options.styles[:something][:geometry]
22
+ end
23
+
24
+ should "return the right data for the style's format" do
25
+ assert_equal :png, @options.styles[:something][:format]
26
+ end
27
+ end
28
+
29
+ context "#styles is a proc" do
30
+ setup do
31
+ @attachment = MockAttachment.new("123x456", :doc)
32
+ @options = Paperclip::Options.new(@attachment,
33
+ :styles => lambda {|att|
34
+ {:something => {:geometry => att.one, :format => att.two}}
35
+ })
36
+ end
37
+
38
+ should "return the right data for the style's geometry" do
39
+ assert_equal "123x456", @options.styles[:something][:geometry]
40
+ end
41
+
42
+ should "return the right data for the style's format" do
43
+ assert_equal :doc, @options.styles[:something][:format]
44
+ end
45
+
46
+ should "run the proc each time, giving dynamic results" do
47
+ assert_equal :doc, @options.styles[:something][:format]
48
+ @attachment.two = :pdf
49
+ assert_equal :pdf, @options.styles[:something][:format]
50
+ end
51
+ end
52
+
53
+ context "#processors" do
54
+ setup do
55
+ @attachment = MockAttachment.new(nil, nil)
56
+ end
57
+ should "return processors if not a proc" do
58
+ @options = Paperclip::Options.new(@attachment, :processors => [:one])
59
+ assert_equal [:one], @options.processors
60
+ end
61
+ should "return processors if it is a proc" do
62
+ @options = Paperclip::Options.new(@attachment, :processors => lambda{|att| [att.one]})
63
+ assert_equal [nil], @options.processors
64
+ @attachment.one = :other
65
+ assert_equal [:other], @options.processors
66
+ end
67
+ end
68
+ 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