paperclip 2.8.0 → 3.0.2

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of paperclip might be problematic. Click here for more details.

Files changed (94) hide show
  1. data/.gitignore +1 -0
  2. data/.travis.yml +9 -7
  3. data/Appraisals +6 -12
  4. data/Gemfile +2 -0
  5. data/NEWS +24 -0
  6. data/README.md +53 -21
  7. data/Rakefile +7 -2
  8. data/UPGRADING +14 -0
  9. data/features/basic_integration.feature +8 -8
  10. data/features/rake_tasks.feature +1 -1
  11. data/features/step_definitions/attachment_steps.rb +11 -2
  12. data/features/step_definitions/rails_steps.rb +17 -79
  13. data/features/support/env.rb +3 -0
  14. data/features/support/file_helpers.rb +24 -0
  15. data/features/support/rails.rb +3 -3
  16. data/gemfiles/{rails3_1.gemfile → 3.0.gemfile} +3 -1
  17. data/gemfiles/{rails2.gemfile → 3.1.gemfile} +3 -1
  18. data/gemfiles/{rails3.gemfile → 3.2.gemfile} +3 -1
  19. data/images.rake +21 -0
  20. data/lib/generators/paperclip/paperclip_generator.rb +1 -2
  21. data/lib/paperclip.rb +48 -319
  22. data/lib/paperclip/attachment.rb +33 -81
  23. data/lib/paperclip/attachment_options.rb +0 -1
  24. data/lib/paperclip/callbacks.rb +30 -0
  25. data/lib/paperclip/errors.rb +27 -0
  26. data/lib/paperclip/geometry.rb +6 -4
  27. data/lib/paperclip/glue.rb +15 -0
  28. data/lib/paperclip/helpers.rb +71 -0
  29. data/lib/paperclip/instance_methods.rb +35 -0
  30. data/lib/paperclip/interpolations.rb +2 -2
  31. data/lib/paperclip/io_adapters/attachment_adapter.rb +62 -0
  32. data/lib/paperclip/io_adapters/file_adapter.rb +81 -0
  33. data/lib/paperclip/io_adapters/identity_adapter.rb +12 -0
  34. data/lib/paperclip/io_adapters/nil_adapter.rb +34 -0
  35. data/lib/paperclip/io_adapters/registry.rb +32 -0
  36. data/lib/paperclip/io_adapters/stringio_adapter.rb +64 -0
  37. data/lib/paperclip/io_adapters/uploaded_file_adapter.rb +63 -0
  38. data/lib/paperclip/locales/en.yml +17 -0
  39. data/lib/paperclip/logger.rb +21 -0
  40. data/lib/paperclip/matchers/validate_attachment_content_type_matcher.rb +1 -1
  41. data/lib/paperclip/matchers/validate_attachment_presence_matcher.rb +2 -2
  42. data/lib/paperclip/matchers/validate_attachment_size_matcher.rb +7 -7
  43. data/lib/paperclip/processor.rb +32 -17
  44. data/lib/paperclip/railtie.rb +10 -15
  45. data/lib/paperclip/storage/filesystem.rb +5 -14
  46. data/lib/paperclip/storage/fog.rb +2 -21
  47. data/lib/paperclip/storage/s3.rb +12 -29
  48. data/lib/paperclip/tempfile.rb +41 -0
  49. data/lib/paperclip/thumbnail.rb +2 -3
  50. data/lib/paperclip/validators.rb +45 -0
  51. data/lib/paperclip/validators/attachment_content_type_validator.rb +47 -0
  52. data/lib/paperclip/validators/attachment_presence_validator.rb +26 -0
  53. data/lib/paperclip/validators/attachment_size_validator.rb +102 -0
  54. data/lib/paperclip/version.rb +1 -1
  55. data/lib/tasks/paperclip.rake +3 -11
  56. data/paperclip.gemspec +15 -5
  57. data/test/adapter_registry_test.rb +32 -0
  58. data/test/attachment_adapter_test.rb +48 -0
  59. data/test/attachment_options_test.rb +0 -13
  60. data/test/attachment_test.rb +27 -55
  61. data/test/file_adapter_test.rb +43 -0
  62. data/test/generator_test.rb +78 -0
  63. data/test/geometry_test.rb +5 -5
  64. data/test/helper.rb +9 -11
  65. data/test/identity_adapter_test.rb +8 -0
  66. data/test/integration_test.rb +39 -94
  67. data/test/interpolations_test.rb +8 -1
  68. data/test/matchers/validate_attachment_size_matcher_test.rb +16 -2
  69. data/test/nil_adapter_test.rb +25 -0
  70. data/test/paperclip_test.rb +30 -189
  71. data/test/storage/filesystem_test.rb +0 -14
  72. data/test/storage/fog_test.rb +0 -14
  73. data/test/storage/s3_live_test.rb +22 -9
  74. data/test/storage/s3_test.rb +70 -34
  75. data/test/stringio_adapter_test.rb +42 -0
  76. data/test/style_test.rb +10 -16
  77. data/test/thumbnail_test.rb +16 -10
  78. data/test/uploaded_file_adapter_test.rb +98 -0
  79. data/test/validators/attachment_content_type_validator_test.rb +140 -0
  80. data/test/validators/attachment_presence_validator_test.rb +85 -0
  81. data/test/validators/attachment_size_validator_test.rb +207 -0
  82. data/test/validators_test.rb +25 -0
  83. metadata +152 -30
  84. data/gemfiles/rails3_2.gemfile +0 -9
  85. data/generators/paperclip/USAGE +0 -5
  86. data/generators/paperclip/paperclip_generator.rb +0 -27
  87. data/generators/paperclip/templates/paperclip_migration.rb.erb +0 -19
  88. data/init.rb +0 -4
  89. data/lib/paperclip/callback_compatibility.rb +0 -61
  90. data/lib/paperclip/iostream.rb +0 -45
  91. data/lib/paperclip/upfile.rb +0 -64
  92. data/rails/init.rb +0 -2
  93. data/test/iostream_test.rb +0 -71
  94. data/test/upfile_test.rb +0 -53
@@ -1,3 +1,3 @@
1
1
  module Paperclip
2
- VERSION = "2.8.0" unless defined? Paperclip::VERSION
2
+ VERSION = "3.0.2" unless defined? Paperclip::VERSION
3
3
  end
@@ -45,15 +45,11 @@ namespace :paperclip do
45
45
  names = Paperclip::Task.obtain_attachments(klass)
46
46
  names.each do |name|
47
47
  Paperclip.each_instance_with_attachment(klass, name) do |instance|
48
- if file = instance.send(name).to_file(:original)
48
+ if file = Paperclip.io_adapters.for(instance.send(name))
49
49
  instance.send("#{name}_file_name=", instance.send("#{name}_file_name").strip)
50
50
  instance.send("#{name}_content_type=", file.content_type.to_s.strip)
51
51
  instance.send("#{name}_file_size=", file.size) if instance.respond_to?("#{name}_file_size")
52
- if Rails.version >= "3.0.0"
53
- instance.save(:validate => false)
54
- else
55
- instance.save(false)
56
- end
52
+ instance.save(:validate => false)
57
53
  else
58
54
  true
59
55
  end
@@ -88,11 +84,7 @@ namespace :paperclip do
88
84
  attributes = %w(file_size file_name content_type).map{ |suffix| "#{name}_#{suffix}".to_sym }
89
85
  if attributes.any?{ |attribute| instance.errors[attribute].present? }
90
86
  instance.send("#{name}=", nil)
91
- if Rails.version >= "3.0.0"
92
- instance.save(:validate => false)
93
- else
94
- instance.save(false)
95
- end
87
+ instance.save(:validate => false)
96
88
  end
97
89
  end
98
90
  end
@@ -18,24 +18,34 @@ Gem::Specification.new do |s|
18
18
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
19
  s.require_paths = ["lib"]
20
20
 
21
+ if File.exists?('UPGRADING')
22
+ s.post_install_message = File.read("UPGRADING")
23
+ end
24
+
21
25
  s.requirements << "ImageMagick"
26
+ s.required_ruby_version = ">= 1.9.2"
22
27
 
23
- s.add_dependency('activerecord', '>= 2.3.0')
24
- s.add_dependency('activesupport', '>= 2.3.2')
28
+ s.add_dependency('activerecord', '>= 3.0.0')
29
+ s.add_dependency('activemodel', '>= 3.0.0')
30
+ s.add_dependency('activesupport', '>= 3.0.0')
25
31
  s.add_dependency('cocaine', '>= 0.0.2')
26
32
  s.add_dependency('mime-types')
27
33
 
28
34
  s.add_development_dependency('shoulda')
29
35
  s.add_development_dependency('appraisal', '~> 0.4.0')
30
36
  s.add_development_dependency('mocha')
31
- s.add_development_dependency('aws-sdk')
32
- s.add_development_dependency('sqlite3', '~> 1.3.5')
37
+ s.add_development_dependency('aws-sdk', '~> 1.3.8')
38
+ s.add_development_dependency('bourne')
39
+ s.add_development_dependency('sqlite3', '~> 1.3.4')
33
40
  s.add_development_dependency('cucumber', '~> 1.1.0')
34
41
  s.add_development_dependency('aruba')
42
+ s.add_development_dependency('nokogiri', '~> 1.4.7')
35
43
  s.add_development_dependency('capybara')
36
44
  s.add_development_dependency('bundler')
37
- s.add_development_dependency('cocaine', '~> 0.4.0')
45
+ s.add_development_dependency('cocaine', '~> 0.2')
38
46
  s.add_development_dependency('fog')
47
+ s.add_development_dependency('pry')
48
+ s.add_development_dependency('launchy')
39
49
  s.add_development_dependency('rake')
40
50
  s.add_development_dependency('fakeweb')
41
51
  end
@@ -0,0 +1,32 @@
1
+ require './test/helper'
2
+
3
+ class AdapterRegistryTest < Test::Unit::TestCase
4
+ context "for" do
5
+ setup do
6
+ class AdapterTest
7
+ def initialize(target); end
8
+ end
9
+ @subject = Paperclip::AdapterRegistry.new
10
+ @subject.register(AdapterTest){|t| Symbol === t }
11
+ end
12
+ should "return the class registered for the adapted type" do
13
+ assert_equal AdapterTest, @subject.for(:target).class
14
+ end
15
+ end
16
+
17
+ context "registered?" do
18
+ setup do
19
+ class AdapterTest
20
+ def initialize(target); end
21
+ end
22
+ @subject = Paperclip::AdapterRegistry.new
23
+ @subject.register(AdapterTest){|t| Symbol === t }
24
+ end
25
+ should "return true when the class of this adapter has been registered" do
26
+ assert @subject.registered?(AdapterTest.new(:target))
27
+ end
28
+ should "return false when the adapter has not been registered" do
29
+ assert ! @subject.registered?(Object)
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,48 @@
1
+ require './test/helper'
2
+
3
+ class AttachmentAdapterTest < Test::Unit::TestCase
4
+ def setup
5
+ rebuild_model :path => "tmp/:class/:attachment/:style/:filename"
6
+ @attachment = Dummy.new.avatar
7
+ @file = File.new(fixture_file("5k.png"))
8
+ @file.binmode
9
+
10
+ @attachment.assign(@file)
11
+ @attachment.save
12
+ @subject = Paperclip.io_adapters.for(@attachment)
13
+ end
14
+
15
+ should "get the right filename" do
16
+ assert_equal "5k.png", @subject.original_filename
17
+ end
18
+
19
+ should "force binmode on tempfile" do
20
+ assert @subject.instance_variable_get("@tempfile").binmode?
21
+ end
22
+
23
+ should "get the content type" do
24
+ assert_equal "image/png", @subject.content_type
25
+ end
26
+
27
+ should "get the file's size" do
28
+ assert_equal 4456, @subject.size
29
+ end
30
+
31
+ should "return false for a call to nil?" do
32
+ assert ! @subject.nil?
33
+ end
34
+
35
+ should "generate a MD5 hash of the contents" do
36
+ expected = Digest::MD5.file(@file.path).to_s
37
+ assert_equal expected, @subject.fingerprint
38
+ end
39
+
40
+ should "read the contents of the file" do
41
+ expected = @file.read
42
+ actual = @subject.read
43
+ assert expected.length > 0
44
+ assert_equal expected.length, actual.length
45
+ assert_equal expected, actual
46
+ end
47
+
48
+ end
@@ -5,19 +5,6 @@ class AttachmentOptionsTest < Test::Unit::TestCase
5
5
  assert_kind_of Hash, Paperclip::AttachmentOptions.new({})
6
6
  end
7
7
 
8
- should "add a default empty validations" do
9
- options = {:arbi => :trary}
10
- expected = {:validations => []}.merge(options)
11
- actual = Paperclip::AttachmentOptions.new(options).to_hash
12
- assert_equal expected, actual
13
- end
14
-
15
- should "not override validations if passed to initializer" do
16
- options = {:validations => "something"}
17
- attachment_options = Paperclip::AttachmentOptions.new(options)
18
- assert_equal "something", attachment_options[:validations]
19
- end
20
-
21
8
  should "respond to []" do
22
9
  assert Paperclip::AttachmentOptions.new({}).respond_to?(:[])
23
10
  end
@@ -103,6 +103,15 @@ class AttachmentTest < Test::Unit::TestCase
103
103
  assert_equal "#{Rails.root}/public/fake_models/1234/fake", @attachment.path
104
104
  end
105
105
 
106
+ should "default to a path that scales" do
107
+ avatar_attachment = attachment
108
+ model = avatar_attachment.instance
109
+ model.id = 1234
110
+ model.avatar_file_name = "fake.jpg"
111
+ expected_path = "#{Rails.root}/public/system/fake_models/avatars/000/001/234/original/fake.jpg"
112
+ assert_equal expected_path, avatar_attachment.path
113
+ end
114
+
106
115
  context "Attachment default_options" do
107
116
  setup do
108
117
  rebuild_model
@@ -503,7 +512,7 @@ class AttachmentTest < Test::Unit::TestCase
503
512
  setup do
504
513
  rebuild_model :processor => [:thumbnail], :styles => { :small => '' }, :whiny_thumbnails => true
505
514
  @dummy = Dummy.new
506
- Paperclip::Thumbnail.expects(:make).raises(Paperclip::PaperclipError, "cannot be processed.")
515
+ Paperclip::Thumbnail.expects(:make).raises(Paperclip::Error, "cannot be processed.")
507
516
  @file = StringIO.new("...")
508
517
  @file.stubs(:to_tempfile).returns(@file)
509
518
  @dummy.avatar = @file
@@ -570,7 +579,7 @@ class AttachmentTest < Test::Unit::TestCase
570
579
  should "convert underscored storage name to camelcase" do
571
580
  rebuild_model :storage => :not_here
572
581
  @dummy = Dummy.new
573
- exception = assert_raises(Paperclip::StorageMethodNotFound) do
582
+ exception = assert_raises(Paperclip::Errors::StorageMethodNotFound) do
574
583
  @dummy.avatar
575
584
  end
576
585
  assert exception.message.include?("NotHere")
@@ -579,7 +588,7 @@ class AttachmentTest < Test::Unit::TestCase
579
588
  should "raise an error if you try to include a storage module that doesn't exist" do
580
589
  rebuild_model :storage => :not_here
581
590
  @dummy = Dummy.new
582
- assert_raises(Paperclip::StorageMethodNotFound) do
591
+ assert_raises(Paperclip::Errors::StorageMethodNotFound) do
583
592
  @dummy.avatar
584
593
  end
585
594
  end
@@ -669,10 +678,7 @@ class AttachmentTest < Test::Unit::TestCase
669
678
  @file = StringIO.new(".")
670
679
  @file.stubs(:original_filename).returns("5k.png\n\n")
671
680
  @file.stubs(:content_type).returns("image/png\n\n")
672
- @file.stubs(:to_tempfile).returns(@file)
673
681
  @dummy = Dummy.new
674
- Paperclip::Thumbnail.expects(:make).returns(@file)
675
- @attachment = @dummy.avatar
676
682
  @dummy.avatar = @file
677
683
  end
678
684
 
@@ -689,23 +695,12 @@ class AttachmentTest < Test::Unit::TestCase
689
695
  setup do
690
696
  rebuild_model
691
697
 
692
- @not_file = mock("not_file")
693
- @tempfile = mock("tempfile")
694
- @not_file.stubs(:nil?).returns(false)
695
- @not_file.expects(:size).returns(10)
696
- @tempfile.expects(:size).returns(10)
697
- @not_file.expects(:original_filename).returns("sheep_say_bæ.png\r\n")
698
- @not_file.expects(:content_type).returns("image/png\r\n")
698
+ @file = StringIO.new(".")
699
+ @file.stubs(:original_filename).returns("sheep_say_bæ.png\r\n")
700
+ @file.stubs(:content_type).returns("image/png\r\n")
699
701
 
700
702
  @dummy = Dummy.new
701
- @attachment = @dummy.avatar
702
- @attachment.expects(:valid_assignment?).with(@not_file).returns(true)
703
- @attachment.expects(:queue_existing_for_delete)
704
- @attachment.expects(:post_process)
705
- @attachment.expects(:to_tempfile).returns(@tempfile)
706
- @attachment.expects(:generate_fingerprint).with(@tempfile).returns("12345")
707
- @attachment.expects(:generate_fingerprint).with(@not_file).returns("12345")
708
- @dummy.avatar = @not_file
703
+ @dummy.avatar = @file
709
704
  end
710
705
 
711
706
  should "not remove strange letters" do
@@ -716,14 +711,14 @@ class AttachmentTest < Test::Unit::TestCase
716
711
  context "Attachment with reserved filename" do
717
712
  setup do
718
713
  rebuild_model
719
- @file = StringIO.new(".")
714
+ @file = Paperclip.io_adapters.for(StringIO.new("."))
720
715
  end
721
716
 
722
717
  context "with default configuration" do
723
718
  "&$+,/:;=?@<>[]{}|\^~%# ".split(//).each do |character|
724
719
  context "with character #{character}" do
725
720
  setup do
726
- @file.stubs(:original_filename).returns("file#{character}name.png")
721
+ @file.original_filename = "file#{character}name.png"
727
722
  @dummy = Dummy.new
728
723
  @dummy.avatar = @file
729
724
  end
@@ -787,11 +782,8 @@ class AttachmentTest < Test::Unit::TestCase
787
782
  end
788
783
 
789
784
  should "should have matching to_s and url methods" do
790
- file = @attachment.to_file
791
- assert file
792
785
  assert_match @attachment.to_s, @attachment.url
793
786
  assert_match @attachment.to_s(:small), @attachment.url(:small)
794
- file.close
795
787
  end
796
788
  end
797
789
 
@@ -816,13 +808,12 @@ class AttachmentTest < Test::Unit::TestCase
816
808
 
817
809
  should "raise if there are not the correct columns when you try to assign" do
818
810
  @other_attachment = Paperclip::Attachment.new(:not_here, @instance)
819
- assert_raises(Paperclip::PaperclipError) do
811
+ assert_raises(Paperclip::Error) do
820
812
  @other_attachment.assign(@file)
821
813
  end
822
814
  end
823
815
 
824
816
  should "return nil as path when no file assigned" do
825
- assert @attachment.to_file.nil?
826
817
  assert_equal nil, @attachment.path
827
818
  assert_equal nil, @attachment.path(:blah)
828
819
  end
@@ -877,10 +868,6 @@ class AttachmentTest < Test::Unit::TestCase
877
868
  assert @attachment.dirty?
878
869
  end
879
870
 
880
- should "set uploaded_file for access beyond the paperclip lifecycle" do
881
- assert_equal @file, @attachment.uploaded_file
882
- end
883
-
884
871
  context "and saved" do
885
872
  setup do
886
873
  @attachment.save
@@ -888,11 +875,7 @@ class AttachmentTest < Test::Unit::TestCase
888
875
 
889
876
  should "commit the files to disk" do
890
877
  [:large, :medium, :small].each do |style|
891
- io = @attachment.to_file(style)
892
- # p "in commit to disk test, io is #{io.inspect} and @instance.id is #{@instance.id}"
893
- assert File.exists?(io.path)
894
- assert ! io.is_a?(::Tempfile)
895
- io.close
878
+ assert File.exists?(@attachment.path(style))
896
879
  end
897
880
  end
898
881
 
@@ -909,11 +892,6 @@ class AttachmentTest < Test::Unit::TestCase
909
892
  end
910
893
  end
911
894
 
912
- should "still have its #file attribute not be nil" do
913
- assert ! (file = @attachment.to_file).nil?
914
- file.close
915
- end
916
-
917
895
  context "and trying to delete" do
918
896
  setup do
919
897
  @existing_names = @attachment.styles.keys.collect do |style|
@@ -1002,7 +980,7 @@ class AttachmentTest < Test::Unit::TestCase
1002
980
  end
1003
981
 
1004
982
  should "not be able to find the module" do
1005
- assert_raise(Paperclip::StorageMethodNotFound){ Dummy.new.avatar }
983
+ assert_raise(Paperclip::Errors::StorageMethodNotFound){ Dummy.new.avatar }
1006
984
  end
1007
985
  end
1008
986
  end
@@ -1038,7 +1016,7 @@ class AttachmentTest < Test::Unit::TestCase
1038
1016
 
1039
1017
  should "return the right value when sent #avatar_file_size" do
1040
1018
  @dummy.avatar = @file
1041
- assert_equal @file.size, @dummy.avatar.size
1019
+ assert_equal File.size(@file), @dummy.avatar.size
1042
1020
  end
1043
1021
 
1044
1022
  context "and avatar_updated_at column" do
@@ -1059,18 +1037,12 @@ class AttachmentTest < Test::Unit::TestCase
1059
1037
  assert_equal now.to_i, @dummy.avatar.updated_at
1060
1038
  end
1061
1039
  end
1062
-
1063
- should "not calculate fingerprint after save" do
1064
- @dummy.avatar = @file
1065
- @dummy.save
1066
- assert_nil @dummy.avatar.fingerprint
1067
- end
1068
-
1069
- should "not calculate fingerprint before saving" do
1040
+
1041
+ should "not calculate fingerprint" do
1070
1042
  @dummy.avatar = @file
1071
1043
  assert_nil @dummy.avatar.fingerprint
1072
1044
  end
1073
-
1045
+
1074
1046
  context "and avatar_content_type column" do
1075
1047
  setup do
1076
1048
  ActiveRecord::Base.connection.add_column :dummies, :avatar_content_type, :string
@@ -1101,14 +1073,14 @@ class AttachmentTest < Test::Unit::TestCase
1101
1073
 
1102
1074
  should "return the right value when sent #avatar_file_size" do
1103
1075
  @dummy.avatar = @file
1104
- assert_equal @file.size, @dummy.avatar.size
1076
+ assert_equal File.size(@file), @dummy.avatar.size
1105
1077
  end
1106
1078
 
1107
1079
  should "return the right value when saved, reloaded, and sent #avatar_file_size" do
1108
1080
  @dummy.avatar = @file
1109
1081
  @dummy.save
1110
1082
  @dummy = Dummy.find(@dummy.id)
1111
- assert_equal @file.size, @dummy.avatar.size
1083
+ assert_equal File.size(@file), @dummy.avatar.size
1112
1084
  end
1113
1085
  end
1114
1086
 
@@ -0,0 +1,43 @@
1
+ require './test/helper'
2
+
3
+ class FileAdapterTest < Test::Unit::TestCase
4
+ context "a new instance" do
5
+ setup do
6
+ @file = File.new(fixture_file("5k.png"))
7
+ @file.binmode
8
+ @subject = Paperclip.io_adapters.for(@file)
9
+ end
10
+
11
+ should "get the right filename" do
12
+ assert_equal "5k.png", @subject.original_filename
13
+ end
14
+
15
+ should "force binmode on tempfile" do
16
+ assert @subject.instance_variable_get("@tempfile").binmode?
17
+ end
18
+
19
+ should "get the content type" do
20
+ assert_equal "image/png", @subject.content_type
21
+ end
22
+
23
+ should "get the file's size" do
24
+ assert_equal 4456, @subject.size
25
+ end
26
+
27
+ should "return false for a call to nil?" do
28
+ assert ! @subject.nil?
29
+ end
30
+
31
+ should "generate a MD5 hash of the contents" do
32
+ expected = Digest::MD5.file(@file.path).to_s
33
+ assert_equal expected, @subject.fingerprint
34
+ end
35
+
36
+ should "read the contents of the file" do
37
+ expected = @file.read
38
+ assert expected.length > 0
39
+ assert_equal expected, @subject.read
40
+ end
41
+
42
+ end
43
+ end
@@ -0,0 +1,78 @@
1
+ require './test/helper'
2
+ require 'rails/generators'
3
+ require 'generators/paperclip/paperclip_generator'
4
+
5
+ class GeneratorTest < Rails::Generators::TestCase
6
+ tests PaperclipGenerator
7
+ destination File.expand_path("../tmp", File.dirname(__FILE__))
8
+ setup :prepare_destination
9
+
10
+ context 'running migration' do
11
+ context 'with single attachment name' do
12
+ setup do
13
+ run_generator %w(user avatar)
14
+ end
15
+
16
+ should 'create a correct migration file' do
17
+ assert_migration 'db/migrate/add_attachment_avatar_to_users.rb' do |migration|
18
+ assert_match /class AddAttachmentAvatarToUsers/, migration
19
+
20
+ assert_class_method :up, migration do |up|
21
+ assert_match /add_column :users, :avatar_file_name, :string/, up
22
+ assert_match /add_column :users, :avatar_content_type, :string/, up
23
+ assert_match /add_column :users, :avatar_file_size, :integer/, up
24
+ assert_match /add_column :users, :avatar_updated_at, :datetime/, up
25
+ end
26
+
27
+ assert_class_method :down, migration do |down|
28
+ assert_match /remove_column :users, :avatar_file_name/, down
29
+ assert_match /remove_column :users, :avatar_content_type/, down
30
+ assert_match /remove_column :users, :avatar_file_size/, down
31
+ assert_match /remove_column :users, :avatar_updated_at/, down
32
+ end
33
+ end
34
+ end
35
+ end
36
+
37
+ context 'with multiple attachment names' do
38
+ setup do
39
+ run_generator %w(user avatar photo)
40
+ end
41
+
42
+ should 'create a correct migration file' do
43
+ assert_migration 'db/migrate/add_attachment_avatar_photo_to_users.rb' do |migration|
44
+ assert_match /class AddAttachmentAvatarPhotoToUsers/, migration
45
+
46
+ assert_class_method :up, migration do |up|
47
+ assert_match /add_column :users, :avatar_file_name, :string/, up
48
+ assert_match /add_column :users, :avatar_content_type, :string/, up
49
+ assert_match /add_column :users, :avatar_file_size, :integer/, up
50
+ assert_match /add_column :users, :avatar_updated_at, :datetime/, up
51
+ assert_match /add_column :users, :photo_file_name, :string/, up
52
+ assert_match /add_column :users, :photo_content_type, :string/, up
53
+ assert_match /add_column :users, :photo_file_size, :integer/, up
54
+ assert_match /add_column :users, :photo_updated_at, :datetime/, up
55
+ end
56
+
57
+ assert_class_method :down, migration do |down|
58
+ assert_match /remove_column :users, :avatar_file_name/, down
59
+ assert_match /remove_column :users, :avatar_content_type/, down
60
+ assert_match /remove_column :users, :avatar_file_size/, down
61
+ assert_match /remove_column :users, :avatar_updated_at/, down
62
+ assert_match /remove_column :users, :photo_file_name/, down
63
+ assert_match /remove_column :users, :photo_content_type/, down
64
+ assert_match /remove_column :users, :photo_file_size/, down
65
+ assert_match /remove_column :users, :photo_updated_at/, down
66
+ end
67
+ end
68
+ end
69
+ end
70
+
71
+ context 'without required arguments' do
72
+ should 'not create the migration' do
73
+ silence_stream(STDERR) { run_generator %w() }
74
+ assert_no_migration 'db/migrate/add_attachment_avatar_to_users.rb'
75
+ end
76
+ end
77
+ end
78
+ end