paperclip_jk 5.0.0.beta2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (190) hide show
  1. checksums.yaml +15 -0
  2. data/.gitignore +21 -0
  3. data/.hound.yml +1066 -0
  4. data/.rubocop.yml +1 -0
  5. data/.travis.yml +26 -0
  6. data/Appraisals +27 -0
  7. data/CONTRIBUTING.md +86 -0
  8. data/Gemfile +15 -0
  9. data/LICENSE +24 -0
  10. data/NEWS +424 -0
  11. data/README.md +999 -0
  12. data/RELEASING.md +17 -0
  13. data/Rakefile +44 -0
  14. data/UPGRADING +17 -0
  15. data/features/basic_integration.feature +81 -0
  16. data/features/migration.feature +70 -0
  17. data/features/rake_tasks.feature +62 -0
  18. data/features/step_definitions/attachment_steps.rb +110 -0
  19. data/features/step_definitions/html_steps.rb +15 -0
  20. data/features/step_definitions/rails_steps.rb +230 -0
  21. data/features/step_definitions/s3_steps.rb +14 -0
  22. data/features/step_definitions/web_steps.rb +107 -0
  23. data/features/support/env.rb +11 -0
  24. data/features/support/fakeweb.rb +13 -0
  25. data/features/support/file_helpers.rb +34 -0
  26. data/features/support/fixtures/boot_config.txt +15 -0
  27. data/features/support/fixtures/gemfile.txt +5 -0
  28. data/features/support/fixtures/preinitializer.txt +20 -0
  29. data/features/support/paths.rb +28 -0
  30. data/features/support/rails.rb +63 -0
  31. data/features/support/selectors.rb +19 -0
  32. data/gemfiles/4.2.awsv2.0.gemfile +17 -0
  33. data/gemfiles/4.2.awsv2.1.gemfile +17 -0
  34. data/gemfiles/4.2.awsv2.gemfile +20 -0
  35. data/gemfiles/5.0.awsv2.0.gemfile +17 -0
  36. data/gemfiles/5.0.awsv2.1.gemfile +17 -0
  37. data/gemfiles/5.0.awsv2.gemfile +25 -0
  38. data/lib/generators/paperclip/USAGE +8 -0
  39. data/lib/generators/paperclip/paperclip_generator.rb +30 -0
  40. data/lib/generators/paperclip/templates/paperclip_migration.rb.erb +15 -0
  41. data/lib/paperclip.rb +211 -0
  42. data/lib/paperclip/attachment.rb +610 -0
  43. data/lib/paperclip/attachment_registry.rb +60 -0
  44. data/lib/paperclip/callbacks.rb +42 -0
  45. data/lib/paperclip/content_type_detector.rb +80 -0
  46. data/lib/paperclip/errors.rb +34 -0
  47. data/lib/paperclip/file_command_content_type_detector.rb +30 -0
  48. data/lib/paperclip/filename_cleaner.rb +16 -0
  49. data/lib/paperclip/geometry.rb +158 -0
  50. data/lib/paperclip/geometry_detector_factory.rb +48 -0
  51. data/lib/paperclip/geometry_parser_factory.rb +31 -0
  52. data/lib/paperclip/glue.rb +17 -0
  53. data/lib/paperclip/has_attached_file.rb +115 -0
  54. data/lib/paperclip/helpers.rb +60 -0
  55. data/lib/paperclip/interpolations.rb +197 -0
  56. data/lib/paperclip/interpolations/plural_cache.rb +18 -0
  57. data/lib/paperclip/io_adapters/abstract_adapter.rb +47 -0
  58. data/lib/paperclip/io_adapters/attachment_adapter.rb +36 -0
  59. data/lib/paperclip/io_adapters/data_uri_adapter.rb +22 -0
  60. data/lib/paperclip/io_adapters/empty_string_adapter.rb +18 -0
  61. data/lib/paperclip/io_adapters/file_adapter.rb +22 -0
  62. data/lib/paperclip/io_adapters/http_url_proxy_adapter.rb +15 -0
  63. data/lib/paperclip/io_adapters/identity_adapter.rb +12 -0
  64. data/lib/paperclip/io_adapters/nil_adapter.rb +34 -0
  65. data/lib/paperclip/io_adapters/registry.rb +32 -0
  66. data/lib/paperclip/io_adapters/stringio_adapter.rb +33 -0
  67. data/lib/paperclip/io_adapters/uploaded_file_adapter.rb +42 -0
  68. data/lib/paperclip/io_adapters/uri_adapter.rb +44 -0
  69. data/lib/paperclip/locales/en.yml +18 -0
  70. data/lib/paperclip/logger.rb +21 -0
  71. data/lib/paperclip/matchers.rb +64 -0
  72. data/lib/paperclip/matchers/have_attached_file_matcher.rb +54 -0
  73. data/lib/paperclip/matchers/validate_attachment_content_type_matcher.rb +100 -0
  74. data/lib/paperclip/matchers/validate_attachment_presence_matcher.rb +59 -0
  75. data/lib/paperclip/matchers/validate_attachment_size_matcher.rb +96 -0
  76. data/lib/paperclip/media_type_spoof_detector.rb +89 -0
  77. data/lib/paperclip/missing_attachment_styles.rb +79 -0
  78. data/lib/paperclip/processor.rb +48 -0
  79. data/lib/paperclip/processor_helpers.rb +50 -0
  80. data/lib/paperclip/rails_environment.rb +25 -0
  81. data/lib/paperclip/railtie.rb +31 -0
  82. data/lib/paperclip/schema.rb +77 -0
  83. data/lib/paperclip/storage.rb +4 -0
  84. data/lib/paperclip/storage/database.rb +140 -0
  85. data/lib/paperclip/storage/filesystem.rb +90 -0
  86. data/lib/paperclip/storage/fog.rb +244 -0
  87. data/lib/paperclip/storage/s3.rb +442 -0
  88. data/lib/paperclip/style.rb +109 -0
  89. data/lib/paperclip/tempfile.rb +43 -0
  90. data/lib/paperclip/tempfile_factory.rb +23 -0
  91. data/lib/paperclip/thumbnail.rb +121 -0
  92. data/lib/paperclip/url_generator.rb +72 -0
  93. data/lib/paperclip/validators.rb +74 -0
  94. data/lib/paperclip/validators/attachment_content_type_validator.rb +88 -0
  95. data/lib/paperclip/validators/attachment_file_name_validator.rb +80 -0
  96. data/lib/paperclip/validators/attachment_file_type_ignorance_validator.rb +29 -0
  97. data/lib/paperclip/validators/attachment_presence_validator.rb +30 -0
  98. data/lib/paperclip/validators/attachment_size_validator.rb +109 -0
  99. data/lib/paperclip/validators/media_type_spoof_detection_validator.rb +27 -0
  100. data/lib/paperclip/version.rb +3 -0
  101. data/lib/tasks/paperclip.rake +127 -0
  102. data/paperclip.gemspec +54 -0
  103. data/shoulda_macros/paperclip.rb +134 -0
  104. data/spec/database.yml +4 -0
  105. data/spec/paperclip/attachment_definitions_spec.rb +13 -0
  106. data/spec/paperclip/attachment_processing_spec.rb +80 -0
  107. data/spec/paperclip/attachment_registry_spec.rb +158 -0
  108. data/spec/paperclip/attachment_spec.rb +1517 -0
  109. data/spec/paperclip/content_type_detector_spec.rb +48 -0
  110. data/spec/paperclip/file_command_content_type_detector_spec.rb +26 -0
  111. data/spec/paperclip/filename_cleaner_spec.rb +14 -0
  112. data/spec/paperclip/geometry_detector_spec.rb +39 -0
  113. data/spec/paperclip/geometry_parser_spec.rb +73 -0
  114. data/spec/paperclip/geometry_spec.rb +255 -0
  115. data/spec/paperclip/glue_spec.rb +44 -0
  116. data/spec/paperclip/has_attached_file_spec.rb +158 -0
  117. data/spec/paperclip/integration_spec.rb +668 -0
  118. data/spec/paperclip/interpolations_spec.rb +262 -0
  119. data/spec/paperclip/io_adapters/abstract_adapter_spec.rb +78 -0
  120. data/spec/paperclip/io_adapters/attachment_adapter_spec.rb +139 -0
  121. data/spec/paperclip/io_adapters/data_uri_adapter_spec.rb +83 -0
  122. data/spec/paperclip/io_adapters/empty_string_adapter_spec.rb +17 -0
  123. data/spec/paperclip/io_adapters/file_adapter_spec.rb +131 -0
  124. data/spec/paperclip/io_adapters/http_url_proxy_adapter_spec.rb +113 -0
  125. data/spec/paperclip/io_adapters/identity_adapter_spec.rb +8 -0
  126. data/spec/paperclip/io_adapters/nil_adapter_spec.rb +25 -0
  127. data/spec/paperclip/io_adapters/registry_spec.rb +35 -0
  128. data/spec/paperclip/io_adapters/stringio_adapter_spec.rb +64 -0
  129. data/spec/paperclip/io_adapters/uploaded_file_adapter_spec.rb +146 -0
  130. data/spec/paperclip/io_adapters/uri_adapter_spec.rb +102 -0
  131. data/spec/paperclip/matchers/have_attached_file_matcher_spec.rb +19 -0
  132. data/spec/paperclip/matchers/validate_attachment_content_type_matcher_spec.rb +109 -0
  133. data/spec/paperclip/matchers/validate_attachment_presence_matcher_spec.rb +69 -0
  134. data/spec/paperclip/matchers/validate_attachment_size_matcher_spec.rb +88 -0
  135. data/spec/paperclip/media_type_spoof_detector_spec.rb +70 -0
  136. data/spec/paperclip/meta_class_spec.rb +30 -0
  137. data/spec/paperclip/paperclip_missing_attachment_styles_spec.rb +84 -0
  138. data/spec/paperclip/paperclip_spec.rb +192 -0
  139. data/spec/paperclip/plural_cache_spec.rb +37 -0
  140. data/spec/paperclip/processor_helpers_spec.rb +57 -0
  141. data/spec/paperclip/processor_spec.rb +26 -0
  142. data/spec/paperclip/rails_environment_spec.rb +33 -0
  143. data/spec/paperclip/rake_spec.rb +103 -0
  144. data/spec/paperclip/schema_spec.rb +248 -0
  145. data/spec/paperclip/storage/filesystem_spec.rb +79 -0
  146. data/spec/paperclip/storage/fog_spec.rb +545 -0
  147. data/spec/paperclip/storage/s3_live_spec.rb +186 -0
  148. data/spec/paperclip/storage/s3_spec.rb +1583 -0
  149. data/spec/paperclip/style_spec.rb +255 -0
  150. data/spec/paperclip/tempfile_factory_spec.rb +33 -0
  151. data/spec/paperclip/thumbnail_spec.rb +500 -0
  152. data/spec/paperclip/url_generator_spec.rb +211 -0
  153. data/spec/paperclip/validators/attachment_content_type_validator_spec.rb +322 -0
  154. data/spec/paperclip/validators/attachment_file_name_validator_spec.rb +160 -0
  155. data/spec/paperclip/validators/attachment_presence_validator_spec.rb +85 -0
  156. data/spec/paperclip/validators/attachment_size_validator_spec.rb +235 -0
  157. data/spec/paperclip/validators/media_type_spoof_detection_validator_spec.rb +52 -0
  158. data/spec/paperclip/validators_spec.rb +164 -0
  159. data/spec/spec_helper.rb +45 -0
  160. data/spec/support/assertions.rb +78 -0
  161. data/spec/support/fake_model.rb +25 -0
  162. data/spec/support/fake_rails.rb +12 -0
  163. data/spec/support/fixtures/12k.png +0 -0
  164. data/spec/support/fixtures/50x50.png +0 -0
  165. data/spec/support/fixtures/5k.png +0 -0
  166. data/spec/support/fixtures/animated +0 -0
  167. data/spec/support/fixtures/animated.gif +0 -0
  168. data/spec/support/fixtures/animated.unknown +0 -0
  169. data/spec/support/fixtures/bad.png +1 -0
  170. data/spec/support/fixtures/empty.html +1 -0
  171. data/spec/support/fixtures/empty.xlsx +0 -0
  172. data/spec/support/fixtures/fog.yml +8 -0
  173. data/spec/support/fixtures/rotated.jpg +0 -0
  174. data/spec/support/fixtures/s3.yml +8 -0
  175. data/spec/support/fixtures/spaced file.jpg +0 -0
  176. data/spec/support/fixtures/spaced file.png +0 -0
  177. data/spec/support/fixtures/text.txt +1 -0
  178. data/spec/support/fixtures/twopage.pdf +0 -0
  179. data/spec/support/fixtures/uppercase.PNG +0 -0
  180. data/spec/support/matchers/accept.rb +5 -0
  181. data/spec/support/matchers/exist.rb +5 -0
  182. data/spec/support/matchers/have_column.rb +23 -0
  183. data/spec/support/mock_attachment.rb +22 -0
  184. data/spec/support/mock_interpolator.rb +24 -0
  185. data/spec/support/mock_url_generator_builder.rb +27 -0
  186. data/spec/support/model_reconstruction.rb +68 -0
  187. data/spec/support/reporting.rb +11 -0
  188. data/spec/support/test_data.rb +13 -0
  189. data/spec/support/version_helper.rb +9 -0
  190. metadata +713 -0
@@ -0,0 +1,17 @@
1
+ Releasing paperclip
2
+
3
+ 1. Update `lib/paperclip/version.rb` file accordingly.
4
+ 2. Update `NEWS` to reflect the changes since last release.
5
+ 3. Commit changes. There shouldn’t be code changes, and thus CI doesn’t need to
6
+ run, you can then add “[ci skip]” to the commit message.
7
+ 4. Tag the release: `git tag -m 'vVERSION' vVERSION`
8
+ 5. Push changes: `git push --tags`
9
+ 6. Build and publish the gem:
10
+
11
+ ```bash
12
+ gem build paperclip.gemspec
13
+ gem push paperclip-VERSION.gem
14
+ ```
15
+
16
+ 7. Announce the new release, making sure to say “thank you” to the contributors
17
+ who helped shape this version.
@@ -0,0 +1,44 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'appraisal'
3
+ require 'rspec/core/rake_task'
4
+ require 'cucumber/rake/task'
5
+
6
+ desc 'Default: run unit tests.'
7
+ task :default => [:clean, :all]
8
+
9
+ desc 'Test the paperclip plugin under all supported Rails versions.'
10
+ task :all do |t|
11
+ if ENV['BUNDLE_GEMFILE']
12
+ exec('rake spec cucumber')
13
+ else
14
+ exec("rm -f gemfiles/*.lock")
15
+ Rake::Task["appraisal:gemfiles"].execute
16
+ Rake::Task["appraisal:install"].execute
17
+ exec('rake appraisal')
18
+ end
19
+ end
20
+
21
+ desc 'Test the paperclip plugin.'
22
+ RSpec::Core::RakeTask.new(:spec)
23
+
24
+ desc 'Run integration test'
25
+ Cucumber::Rake::Task.new do |t|
26
+ t.cucumber_opts = %w{--format progress}
27
+ end
28
+
29
+ desc 'Start an IRB session with all necessary files required.'
30
+ task :shell do |t|
31
+ chdir File.dirname(__FILE__)
32
+ exec 'irb -I lib/ -I lib/paperclip -r rubygems -r active_record -r tempfile -r init'
33
+ end
34
+
35
+ desc 'Clean up files.'
36
+ task :clean do |t|
37
+ FileUtils.rm_rf "doc"
38
+ FileUtils.rm_rf "tmp"
39
+ FileUtils.rm_rf "pkg"
40
+ FileUtils.rm_rf "public"
41
+ FileUtils.rm "test/debug.log" rescue nil
42
+ FileUtils.rm "test/paperclip.db" rescue nil
43
+ Dir.glob("paperclip-*.gem").each{|f| FileUtils.rm f }
44
+ end
@@ -0,0 +1,17 @@
1
+ ##################################################
2
+ # NOTE FOR UPGRADING FROM 4.3.0 OR EARLIER #
3
+ ##################################################
4
+
5
+ Paperclip is now compatible with aws-sdk >= 2.0.0.
6
+
7
+ If you are using S3 storage, aws-sdk >= 2.0.0 requires you to make a few small
8
+ changes:
9
+
10
+ * You must set the `s3_region`
11
+ * If you are explicitly setting permissions anywhere, such as in an initializer,
12
+ note that the format of the permissions changed from using an underscore to
13
+ using a hyphen. For example, `:public_read` needs to be changed to
14
+ `public-read`.
15
+
16
+ For a walkthrough of upgrading from 4 to 5 and aws-sdk >= 2.0 you can watch
17
+ http://rubythursday.com/episodes/ruby-snack-27-upgrade-paperclip-and-aws-sdk-in-prep-for-rails-5
@@ -0,0 +1,81 @@
1
+ Feature: Rails integration
2
+
3
+ Background:
4
+ Given I generate a new rails application
5
+ And I run a rails generator to generate a "User" scaffold with "name:string"
6
+ And I run a paperclip generator to add a paperclip "attachment" to the "User" model
7
+ And I run a migration
8
+ And I update my new user view to include the file upload field
9
+ And I update my user view to include the attachment
10
+ And I allow the attachment to be submitted
11
+
12
+ Scenario: Configure defaults for all attachments through Railtie
13
+ Given I add this snippet to config/application.rb:
14
+ """
15
+ config.paperclip_defaults = {
16
+ :url => "/paperclip/custom/:attachment/:style/:filename",
17
+ :validate_media_type => false
18
+ }
19
+ """
20
+ And I attach :attachment
21
+ And I start the rails application
22
+ When I go to the new user page
23
+ And I fill in "Name" with "something"
24
+ And I attach the file "spec/support/fixtures/animated.unknown" to "Attachment"
25
+ And I press "Submit"
26
+ Then I should see "Name: something"
27
+ And I should see an image with a path of "/paperclip/custom/attachments/original/animated.unknown"
28
+ And the file at "/paperclip/custom/attachments/original/animated.unknown" should be the same as "spec/support/fixtures/animated.unknown"
29
+
30
+ Scenario: Add custom processors
31
+ Given I add a "test" processor in "lib/paperclip"
32
+ And I add a "cool" processor in "lib/paperclip_processors"
33
+ And I attach :attachment with:
34
+ """
35
+ styles: { original: {} }, processors: [:test, :cool]
36
+ """
37
+ And I start the rails application
38
+ When I go to the new user page
39
+ And I fill in "Name" with "something"
40
+ And I attach the file "spec/support/fixtures/5k.png" to "Attachment"
41
+ And I press "Submit"
42
+ Then I should see "Name: something"
43
+ And I should see an image with a path of "/paperclip/custom/attachments/original/5k.png"
44
+
45
+ Scenario: Filesystem integration test
46
+ Given I attach :attachment with:
47
+ """
48
+ :url => "/system/:attachment/:style/:filename"
49
+ """
50
+ And I start the rails application
51
+ When I go to the new user page
52
+ And I fill in "Name" with "something"
53
+ And I attach the file "spec/support/fixtures/5k.png" to "Attachment"
54
+ And I press "Submit"
55
+ Then I should see "Name: something"
56
+ And I should see an image with a path of "/system/attachments/original/5k.png"
57
+ And the file at "/system/attachments/original/5k.png" should be the same as "spec/support/fixtures/5k.png"
58
+
59
+ Scenario: S3 Integration test
60
+ Given I attach :attachment with:
61
+ """
62
+ :storage => :s3,
63
+ :path => "/:attachment/:style/:filename",
64
+ :s3_credentials => Rails.root.join("config/s3.yml"),
65
+ :styles => { :square => "100x100#" }
66
+ """
67
+ And I write to "config/s3.yml" with:
68
+ """
69
+ bucket: paperclip
70
+ access_key_id: access_key
71
+ secret_access_key: secret_key
72
+ s3_region: us-west-2
73
+ """
74
+ And I start the rails application
75
+ When I go to the new user page
76
+ And I fill in "Name" with "something"
77
+ And I attach the file "spec/support/fixtures/5k.png" to "Attachment" on S3
78
+ And I press "Submit"
79
+ Then I should see "Name: something"
80
+ And I should see an image with a path of "http://s3.amazonaws.com/paperclip/attachments/original/5k.png"
81
+ And the file at "http://s3.amazonaws.com/paperclip/attachments/original/5k.png" should be uploaded to S3
@@ -0,0 +1,70 @@
1
+ Feature: Migration
2
+
3
+ Background:
4
+ Given I generate a new rails application
5
+ And I write to "app/models/user.rb" with:
6
+ """
7
+ class User < ActiveRecord::Base; end
8
+ """
9
+
10
+ Scenario: Vintage syntax
11
+ When I write to "db/migrate/01_add_attachment_to_users.rb" with:
12
+ """
13
+ class AddAttachmentToUsers < ActiveRecord::Migration
14
+ def self.up
15
+ create_table :users do |t|
16
+ t.has_attached_file :avatar
17
+ end
18
+ end
19
+
20
+ def self.down
21
+ drop_attached_file :users, :avatar
22
+ end
23
+ end
24
+ """
25
+ And I run a migration
26
+ Then I should have attachment columns for "avatar"
27
+
28
+ When I rollback a migration
29
+ Then I should not have attachment columns for "avatar"
30
+
31
+ Scenario: New syntax with create_table
32
+ When I write to "db/migrate/01_add_attachment_to_users.rb" with:
33
+ """
34
+ class AddAttachmentToUsers < ActiveRecord::Migration
35
+ def self.up
36
+ create_table :users do |t|
37
+ t.attachment :avatar
38
+ end
39
+ end
40
+ end
41
+ """
42
+ And I run a migration
43
+ Then I should have attachment columns for "avatar"
44
+
45
+ Scenario: New syntax outside of create_table
46
+ When I write to "db/migrate/01_create_users.rb" with:
47
+ """
48
+ class CreateUsers < ActiveRecord::Migration
49
+ def self.up
50
+ create_table :users
51
+ end
52
+ end
53
+ """
54
+ And I write to "db/migrate/02_add_attachment_to_users.rb" with:
55
+ """
56
+ class AddAttachmentToUsers < ActiveRecord::Migration
57
+ def self.up
58
+ add_attachment :users, :avatar
59
+ end
60
+
61
+ def self.down
62
+ remove_attachment :users, :avatar
63
+ end
64
+ end
65
+ """
66
+ And I run a migration
67
+ Then I should have attachment columns for "avatar"
68
+
69
+ When I rollback a migration
70
+ Then I should not have attachment columns for "avatar"
@@ -0,0 +1,62 @@
1
+ Feature: Rake tasks
2
+
3
+ Background:
4
+ Given I generate a new rails application
5
+ And I run a rails generator to generate a "User" scaffold with "name:string"
6
+ And I run a paperclip generator to add a paperclip "attachment" to the "User" model
7
+ And I run a migration
8
+ And I attach :attachment with:
9
+ """
10
+ :path => ":rails_root/public/system/:attachment/:style/:filename"
11
+ """
12
+
13
+ Scenario: Paperclip refresh thumbnails task
14
+ When I modify my attachment definition to:
15
+ """
16
+ has_attached_file :attachment, :path => ":rails_root/public/system/:attachment/:style/:filename",
17
+ :styles => { :medium => "200x200#" }
18
+ """
19
+ And I upload the fixture "5k.png"
20
+ Then the attachment "medium/5k.png" should have a dimension of 200x200
21
+ When I modify my attachment definition to:
22
+ """
23
+ has_attached_file :attachment, :path => ":rails_root/public/system/:attachment/:style/:filename",
24
+ :styles => { :medium => "100x100#" }
25
+ """
26
+ When I successfully run `bundle exec rake paperclip:refresh:thumbnails CLASS=User --trace`
27
+ Then the attachment "original/5k.png" should exist
28
+ And the attachment "medium/5k.png" should have a dimension of 100x100
29
+
30
+ Scenario: Paperclip refresh metadata task
31
+ When I upload the fixture "5k.png"
32
+ And I swap the attachment "original/5k.png" with the fixture "12k.png"
33
+ And I successfully run `bundle exec rake paperclip:refresh:metadata CLASS=User --trace`
34
+ Then the attachment should have the same content type as the fixture "12k.png"
35
+ And the attachment should have the same file size as the fixture "12k.png"
36
+
37
+ Scenario: Paperclip refresh missing styles task
38
+ When I upload the fixture "5k.png"
39
+ Then the attachment file "original/5k.png" should exist
40
+ And the attachment file "medium/5k.png" should not exist
41
+ When I modify my attachment definition to:
42
+ """
43
+ has_attached_file :attachment, :path => ":rails_root/public/system/:attachment/:style/:filename",
44
+ :styles => { :medium => "200x200#" }
45
+ """
46
+ When I successfully run `bundle exec rake paperclip:refresh:missing_styles --trace`
47
+ Then the attachment file "original/5k.png" should exist
48
+ And the attachment file "medium/5k.png" should exist
49
+
50
+ Scenario: Paperclip clean task
51
+ When I upload the fixture "5k.png"
52
+ And I upload the fixture "12k.png"
53
+ Then the attachment file "original/5k.png" should exist
54
+ And the attachment file "original/12k.png" should exist
55
+ When I modify my attachment definition to:
56
+ """
57
+ has_attached_file :attachment, :path => ":rails_root/public/system/:attachment/:style/:filename"
58
+ validates_attachment_size :attachment, :less_than => 10.kilobytes
59
+ """
60
+ And I successfully run `bundle exec rake paperclip:clean CLASS=User --trace`
61
+ Then the attachment file "original/5k.png" should exist
62
+ But the attachment file "original/12k.png" should not exist
@@ -0,0 +1,110 @@
1
+ module AttachmentHelpers
2
+ def fixture_path(filename)
3
+ File.expand_path("#{PROJECT_ROOT}/spec/support/fixtures/#{filename}")
4
+ end
5
+
6
+ def attachment_path(filename)
7
+ File.expand_path("public/system/attachments/#{filename}")
8
+ end
9
+ end
10
+ World(AttachmentHelpers)
11
+
12
+ When /^I modify my attachment definition to:$/ do |definition|
13
+ content = cd(".") { File.read("app/models/user.rb") }
14
+ name = content[/has_attached_file :\w+/][/:\w+/]
15
+ content.gsub!(/has_attached_file.+end/m, <<-FILE)
16
+ #{definition}
17
+ do_not_validate_attachment_file_type #{name}
18
+ end
19
+ FILE
20
+
21
+ write_file "app/models/user.rb", content
22
+ cd(".") { FileUtils.rm_rf ".rbx" }
23
+ end
24
+
25
+ When /^I upload the fixture "([^"]*)"$/ do |filename|
26
+ run_simple %(bundle exec #{runner_command} "User.create!(:attachment => File.open('#{fixture_path(filename)}'))")
27
+ end
28
+
29
+ Then /^the attachment "([^"]*)" should have a dimension of (\d+x\d+)$/ do |filename, dimension|
30
+ cd(".") do
31
+ geometry = `identify -format "%wx%h" "#{attachment_path(filename)}"`.strip
32
+ expect(geometry).to eq(dimension)
33
+ end
34
+ end
35
+
36
+ Then /^the attachment "([^"]*)" should exist$/ do |filename|
37
+ cd(".") do
38
+ expect(File.exist?(attachment_path(filename))).to be true
39
+ end
40
+ end
41
+
42
+ When /^I swap the attachment "([^"]*)" with the fixture "([^"]*)"$/ do |attachment_filename, fixture_filename|
43
+ cd(".") do
44
+ require 'fileutils'
45
+ FileUtils.rm_f attachment_path(attachment_filename)
46
+ FileUtils.cp fixture_path(fixture_filename), attachment_path(attachment_filename)
47
+ end
48
+ end
49
+
50
+ Then /^the attachment should have the same content type as the fixture "([^"]*)"$/ do |filename|
51
+ cd(".") do
52
+ begin
53
+ # Use mime/types/columnar if available, for reduced memory usage
54
+ require "mime/types/columnar"
55
+ rescue LoadError
56
+ require "mime/types"
57
+ end
58
+
59
+ attachment_content_type = `bundle exec #{runner_command} "puts User.last.attachment_content_type"`.strip
60
+ expected = MIME::Types.type_for(filename).first.content_type
61
+ expect(attachment_content_type).to eq(expected)
62
+ end
63
+ end
64
+
65
+ Then /^the attachment should have the same file name as the fixture "([^"]*)"$/ do |filename|
66
+ cd(".") do
67
+ attachment_file_name = `bundle exec #{runner_command} "puts User.last.attachment_file_name"`.strip
68
+ expect(attachment_file_name).to eq(File.name(fixture_path(filename)).to_s)
69
+ end
70
+ end
71
+
72
+ Then /^the attachment should have the same file size as the fixture "([^"]*)"$/ do |filename|
73
+ cd(".") do
74
+ attachment_file_size = `bundle exec #{runner_command} "puts User.last.attachment_file_size"`.strip
75
+ expect(attachment_file_size).to eq(File.size(fixture_path(filename)).to_s)
76
+ end
77
+ end
78
+
79
+ Then /^the attachment file "([^"]*)" should (not )?exist$/ do |filename, not_exist|
80
+ cd(".") do
81
+ expect(attachment_path(filename)).not_to be_an_existing_file
82
+ end
83
+ end
84
+
85
+ Then /^I should have attachment columns for "([^"]*)"$/ do |attachment_name|
86
+ cd(".") do
87
+ columns = eval(`bundle exec #{runner_command} "puts User.columns.map{ |column| [column.name, column.type] }.inspect"`.strip)
88
+ expect_columns = [
89
+ ["#{attachment_name}_file_name", :string],
90
+ ["#{attachment_name}_content_type", :string],
91
+ ["#{attachment_name}_file_size", :integer],
92
+ ["#{attachment_name}_updated_at", :datetime]
93
+ ]
94
+ expect(columns).to include(*expect_columns)
95
+ end
96
+ end
97
+
98
+ Then /^I should not have attachment columns for "([^"]*)"$/ do |attachment_name|
99
+ cd(".") do
100
+ columns = eval(`bundle exec #{runner_command} "puts User.columns.map{ |column| [column.name, column.type] }.inspect"`.strip)
101
+ expect_columns = [
102
+ ["#{attachment_name}_file_name", :string],
103
+ ["#{attachment_name}_content_type", :string],
104
+ ["#{attachment_name}_file_size", :integer],
105
+ ["#{attachment_name}_updated_at", :datetime]
106
+ ]
107
+
108
+ expect(columns).not_to include(*expect_columns)
109
+ end
110
+ end
@@ -0,0 +1,15 @@
1
+ Then %r{I should see an image with a path of "([^"]*)"} do |path|
2
+ expect(page).to have_css("img[src^='#{path}']")
3
+ end
4
+
5
+ Then %r{^the file at "([^"]*)" is the same as "([^"]*)"$} do |web_file, path|
6
+ expected = IO.read(path)
7
+ actual = if web_file.match %r{^https?://}
8
+ Net::HTTP.get(URI.parse(web_file))
9
+ else
10
+ visit(web_file)
11
+ page.body
12
+ end
13
+ actual.force_encoding("UTF-8") if actual.respond_to?(:force_encoding)
14
+ expect(actual).to eq(expected)
15
+ end
@@ -0,0 +1,230 @@
1
+ Given /^I generate a new rails application$/ do
2
+ steps %{
3
+ When I run `bundle exec #{new_application_command} #{APP_NAME} --skip-bundle`
4
+ And I cd to "#{APP_NAME}"
5
+ And I turn off class caching
6
+ And I fix the application.rb for 3.0.12
7
+ And I write to "Gemfile" with:
8
+ """
9
+ source "http://rubygems.org"
10
+ gem "rails", "#{framework_version}"
11
+ gem "sqlite3", "1.3.8", :platform => [:ruby, :rbx]
12
+ gem "activerecord-jdbcsqlite3-adapter", :platform => :jruby
13
+ gem "jruby-openssl", :platform => :jruby
14
+ gem "capybara"
15
+ gem "gherkin"
16
+ gem "aws-sdk"
17
+ gem "racc", :platform => :rbx
18
+ gem "rubysl", :platform => :rbx
19
+ """
20
+ And I remove turbolinks
21
+ And I empty the application.js file
22
+ And I configure the application to use "paperclip" from this project
23
+ }
24
+ end
25
+
26
+ Given "I fix the application.rb for 3.0.12" do
27
+ cd(".") do
28
+ File.open("config/application.rb", "a") do |f|
29
+ f << "ActionController::Base.config.relative_url_root = ''"
30
+ end
31
+ end
32
+ end
33
+
34
+ Given "I allow the attachment to be submitted" do
35
+ cd(".") do
36
+ transform_file("app/controllers/users_controller.rb") do |content|
37
+ content.gsub("params.require(:user).permit(:name)",
38
+ "params.require(:user).permit!")
39
+ end
40
+ end
41
+ end
42
+
43
+ Given "I remove turbolinks" do
44
+ cd(".") do
45
+ transform_file("app/assets/javascripts/application.js") do |content|
46
+ content.gsub("//= require turbolinks", "")
47
+ end
48
+ transform_file("app/views/layouts/application.html.erb") do |content|
49
+ content.gsub(', "data-turbolinks-track" => true', "")
50
+ end
51
+ end
52
+ end
53
+
54
+ Given /^I attach :attachment$/ do
55
+ attach_attachment("attachment")
56
+ end
57
+
58
+ Given /^I attach :attachment with:$/ do |definition|
59
+ attach_attachment("attachment", definition)
60
+ end
61
+
62
+ def attach_attachment(name, definition = nil)
63
+ snippet = ""
64
+ if using_protected_attributes?
65
+ snippet += "attr_accessible :name, :#{name}\n"
66
+ end
67
+ snippet += "has_attached_file :#{name}"
68
+ if definition
69
+ snippet += ", \n"
70
+ snippet += definition
71
+ end
72
+ snippet += "\ndo_not_validate_attachment_file_type :#{name}\n"
73
+ cd(".") do
74
+ transform_file("app/models/user.rb") do |content|
75
+ content.sub(/end\Z/, "#{snippet}\nend")
76
+ end
77
+ end
78
+ end
79
+
80
+ Given "I empty the application.js file" do
81
+ cd(".") do
82
+ transform_file("app/assets/javascripts/application.js") do |content|
83
+ ""
84
+ end
85
+ end
86
+ end
87
+
88
+ Given /^I run a rails generator to generate a "([^"]*)" scaffold with "([^"]*)"$/ do |model_name, attributes|
89
+ step %[I successfully run `bundle exec #{generator_command} scaffold #{model_name} #{attributes}`]
90
+ end
91
+
92
+ Given /^I run a paperclip generator to add a paperclip "([^"]*)" to the "([^"]*)" model$/ do |attachment_name, model_name|
93
+ step %[I successfully run `bundle exec #{generator_command} paperclip #{model_name} #{attachment_name}`]
94
+ end
95
+
96
+ Given /^I run a migration$/ do
97
+ step %[I successfully run `bundle exec rake db:migrate --trace`]
98
+ end
99
+
100
+ When /^I rollback a migration$/ do
101
+ step %[I successfully run `bundle exec rake db:rollback STEPS=1 --trace`]
102
+ end
103
+
104
+ Given /^I update my new user view to include the file upload field$/ do
105
+ steps %{
106
+ Given I overwrite "app/views/users/new.html.erb" with:
107
+ """
108
+ <%= form_for @user, :html => { :multipart => true } do |f| %>
109
+ <%= f.label :name %>
110
+ <%= f.text_field :name %>
111
+ <%= f.label :attachment %>
112
+ <%= f.file_field :attachment %>
113
+ <%= submit_tag "Submit" %>
114
+ <% end %>
115
+ """
116
+ }
117
+ end
118
+
119
+ Given /^I update my user view to include the attachment$/ do
120
+ steps %{
121
+ Given I overwrite "app/views/users/show.html.erb" with:
122
+ """
123
+ <p>Name: <%= @user.name %></p>
124
+ <p>Attachment: <%= image_tag @user.attachment.url %></p>
125
+ """
126
+ }
127
+ end
128
+
129
+ Given /^I add this snippet to the User model:$/ do |snippet|
130
+ file_name = "app/models/user.rb"
131
+ cd(".") do
132
+ content = File.read(file_name)
133
+ File.open(file_name, 'w') { |f| f << content.sub(/end\Z/, "#{snippet}\nend") }
134
+ end
135
+ end
136
+
137
+ Given /^I add this snippet to config\/application.rb:$/ do |snippet|
138
+ file_name = "config/application.rb"
139
+ cd(".") do
140
+ content = File.read(file_name)
141
+ File.open(file_name, 'w') {|f| f << content.sub(/class Application < Rails::Application.*$/, "class Application < Rails::Application\n#{snippet}\n")}
142
+ end
143
+ end
144
+
145
+ Given /^I start the rails application$/ do
146
+ cd(".") do
147
+ require "./config/environment"
148
+ require "capybara/rails"
149
+ end
150
+ end
151
+
152
+ Given /^I reload my application$/ do
153
+ Rails::Application.reload!
154
+ end
155
+
156
+ When /^I turn off class caching$/ do
157
+ cd(".") do
158
+ file = "config/environments/test.rb"
159
+ config = IO.read(file)
160
+ config.gsub!(%r{^\s*config.cache_classes.*$},
161
+ "config.cache_classes = false")
162
+ File.open(file, "w"){|f| f.write(config) }
163
+ end
164
+ end
165
+
166
+ Then /^the file at "([^"]*)" should be the same as "([^"]*)"$/ do |web_file, path|
167
+ expected = IO.read(path)
168
+ actual = read_from_web(web_file)
169
+ expect(actual).to eq(expected)
170
+ end
171
+
172
+ When /^I configure the application to use "([^\"]+)" from this project$/ do |name|
173
+ append_to_gemfile "gem '#{name}', :path => '#{PROJECT_ROOT}'"
174
+ steps %{And I run `bundle install --local`}
175
+ end
176
+
177
+ When /^I configure the application to use "([^\"]+)"$/ do |gem_name|
178
+ append_to_gemfile "gem '#{gem_name}'"
179
+ end
180
+
181
+ When /^I append gems from Appraisal Gemfile$/ do
182
+ File.read(ENV['BUNDLE_GEMFILE']).split(/\n/).each do |line|
183
+ if line =~ /^gem "(?!rails|appraisal)/
184
+ append_to_gemfile line.strip
185
+ end
186
+ end
187
+ end
188
+
189
+ When /^I comment out the gem "([^"]*)" from the Gemfile$/ do |gemname|
190
+ comment_out_gem_in_gemfile gemname
191
+ end
192
+
193
+ Given(/^I add a "(.*?)" processor in "(.*?)"$/) do |processor, directory|
194
+ filename = "#{directory}/#{processor}.rb"
195
+ cd(".") do
196
+ FileUtils.mkdir_p directory
197
+ File.open(filename, "w") do |f|
198
+ f.write(<<-CLASS)
199
+ module Paperclip
200
+ class #{processor.capitalize} < Processor
201
+ def make
202
+ basename = File.basename(file.path, File.extname(file.path))
203
+ dst_format = options[:format] ? ".\#{options[:format]}" : ''
204
+
205
+ dst = Tempfile.new([basename, dst_format])
206
+ dst.binmode
207
+
208
+ convert(':src :dst',
209
+ src: File.expand_path(file.path),
210
+ dst: File.expand_path(dst.path)
211
+ )
212
+
213
+ dst
214
+ end
215
+ end
216
+ end
217
+ CLASS
218
+ end
219
+ end
220
+ end
221
+
222
+ def transform_file(filename)
223
+ if File.exist?(filename)
224
+ content = File.read(filename)
225
+ File.open(filename, "w") do |f|
226
+ content = yield(content)
227
+ f.write(content)
228
+ end
229
+ end
230
+ end