attachs 0.3.5 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (97) hide show
  1. checksums.yaml +4 -4
  2. data/MIT-LICENSE +1 -1
  3. data/README.md +254 -0
  4. data/Rakefile +0 -2
  5. data/lib/attachs.rb +37 -10
  6. data/lib/attachs/active_record/base.rb +58 -95
  7. data/lib/attachs/active_record/connection_adapters.rb +40 -0
  8. data/lib/attachs/active_record/migration.rb +17 -0
  9. data/lib/attachs/active_record/validators.rb +7 -0
  10. data/lib/attachs/active_record/validators/attachment_content_type_validator.rb +30 -0
  11. data/lib/attachs/active_record/validators/attachment_presence_validator.rb +22 -0
  12. data/lib/attachs/active_record/validators/attachment_size_validator.rb +39 -0
  13. data/lib/attachs/attachment.rb +114 -0
  14. data/{config → lib/attachs}/locales/en.yml +2 -1
  15. data/{config → lib/attachs}/locales/es.yml +2 -1
  16. data/lib/attachs/railtie.rb +31 -11
  17. data/lib/attachs/storages/base.rb +68 -0
  18. data/lib/attachs/storages/local.rb +61 -37
  19. data/lib/attachs/storages/s3.rb +66 -57
  20. data/lib/attachs/tools/magick.rb +52 -0
  21. data/lib/attachs/types/base.rb +22 -0
  22. data/lib/attachs/types/default.rb +21 -0
  23. data/lib/attachs/types/regular.rb +21 -0
  24. data/lib/attachs/version.rb +1 -1
  25. data/lib/generators/attachs/install_generator.rb +13 -0
  26. data/lib/generators/attachs/templates/attachs.rb +2 -0
  27. data/lib/tasks/attachs.rake +30 -0
  28. data/test/attachment_test.rb +19 -0
  29. data/test/dummy/app/models/medium.rb +5 -0
  30. data/test/dummy/app/models/picture.rb +2 -0
  31. data/test/dummy/config/application.rb +1 -7
  32. data/test/dummy/config/initializers/attachs.rb +25 -0
  33. data/test/dummy/config/initializers/secret_token.rb +1 -0
  34. data/test/dummy/config/routes.rb +0 -1
  35. data/test/dummy/db/migrate/20140808012639_create_media.rb +11 -0
  36. data/test/dummy/db/schema.rb +14 -25
  37. data/test/dummy/log/development.log +9 -27
  38. data/test/dummy/log/test.log +52130 -11207
  39. data/test/dummy/test/fixtures/180x150.gif +0 -0
  40. data/test/generator_test.rb +18 -0
  41. data/test/local_storage_test.rb +57 -0
  42. data/test/magick_tool_test.rb +42 -0
  43. data/test/migration_test.rb +65 -0
  44. data/test/s3_storage_tes.rb +56 -0
  45. data/test/tasks_test.rb +61 -0
  46. data/test/test_helper.rb +17 -17
  47. data/test/validators_test.rb +100 -0
  48. metadata +85 -120
  49. data/README.rdoc +0 -104
  50. data/app/controllers/attachs/presets_controller.rb +0 -17
  51. data/config/routes.rb +0 -5
  52. data/lib/attachs/engine.rb +0 -7
  53. data/lib/attachs/magick/image.rb +0 -85
  54. data/lib/attachs/types/file.rb +0 -131
  55. data/lib/attachs/types/image.rb +0 -58
  56. data/lib/attachs/validators/attachment_content_type_validator.rb +0 -11
  57. data/lib/attachs/validators/attachment_presence_validator.rb +0 -9
  58. data/lib/attachs/validators/attachment_size_validator.rb +0 -21
  59. data/lib/attachs/validators/base.rb +0 -13
  60. data/lib/generators/attachs/s3/config_generator.rb +0 -13
  61. data/lib/generators/attachs/s3/templates/s3.yml +0 -16
  62. data/lib/tasks/attachs_tasks.rake +0 -71
  63. data/test/dummy/app/models/all_attached.rb +0 -16
  64. data/test/dummy/app/models/file_attached.rb +0 -3
  65. data/test/dummy/app/models/image_attached.rb +0 -3
  66. data/test/dummy/app/models/private_file_attached.rb +0 -3
  67. data/test/dummy/app/models/private_image_attached.rb +0 -3
  68. data/test/dummy/db/migrate/20130820222342_create_file_attacheds.rb +0 -9
  69. data/test/dummy/db/migrate/20130820222355_create_image_attacheds.rb +0 -9
  70. data/test/dummy/db/migrate/20130820222534_create_all_attacheds.rb +0 -19
  71. data/test/dummy/public/uploads/files/file.txt +0 -1
  72. data/test/dummy/public/uploads/images/big/image.jpg +0 -0
  73. data/test/dummy/public/uploads/images/original/image.jpg +0 -0
  74. data/test/dummy/public/uploads/images/small/image.jpg +0 -0
  75. data/test/dummy/test/fixtures/file_big.txt +0 -1
  76. data/test/dummy/test/fixtures/file_empty.txt +0 -1
  77. data/test/dummy/test/fixtures/image.jpg +0 -0
  78. data/test/dummy/test/fixtures/image_big.jpg +0 -0
  79. data/test/dummy/test/fixtures/image_empty.jpg +0 -0
  80. data/test/dummy/tmp/uploads/files/13898788260004868.txt +0 -1
  81. data/test/local_file_record_test.rb +0 -31
  82. data/test/local_file_string_test.rb +0 -23
  83. data/test/local_file_upload_test.rb +0 -30
  84. data/test/local_generate_test.rb +0 -25
  85. data/test/local_image_record_test.rb +0 -25
  86. data/test/local_image_string_test.rb +0 -34
  87. data/test/local_image_upload_test.rb +0 -27
  88. data/test/local_records_test.rb +0 -52
  89. data/test/local_validators_test.rb +0 -71
  90. data/test/s3_file_record_tes.rb +0 -21
  91. data/test/s3_file_string_tes.rb +0 -24
  92. data/test/s3_file_upload_tes.rb +0 -31
  93. data/test/s3_image_record_tes.rb +0 -26
  94. data/test/s3_image_string_tes.rb +0 -31
  95. data/test/s3_image_upload_tes.rb +0 -28
  96. data/test/s3_records_tes.rb +0 -56
  97. data/test/s3_validators_tes.rb +0 -72
@@ -1,24 +0,0 @@
1
- require 'test_helper'
2
-
3
- class S3FileStringTest < ActiveSupport::TestCase
4
-
5
- setup do
6
- load_s3
7
- filename = 'file.txt'
8
- fixture_file_upload_s3 "/#{filename}", 'text/plain', ::File.join('uploads', 'files', filename)
9
- @file = Attachs::Types::File.new(filename)
10
- end
11
-
12
- test "should maintain properties and delete correctly" do
13
- assert @file.exists?
14
- assert_equal 11, @file.size
15
- assert_equal '.txt', @file.extname
16
- assert_equal ::File.join('uploads', 'files', @file.filename), @file.path
17
-
18
- uploads_url = @file.url
19
- @file.delete
20
- assert_not_object_s3 uploads_url
21
- assert !@file.exists?
22
- end
23
-
24
- end
@@ -1,31 +0,0 @@
1
- require 'test_helper'
2
-
3
- class S3FileUploadTest < ActiveSupport::TestCase
4
-
5
- setup do
6
- load_s3
7
- @file = Attachs::Types::File.new(fixture_file_upload('/image.jpg', 'image/jpeg'))
8
- end
9
-
10
- test "file should exists and mantain properties" do
11
- assert @file.exists?
12
- assert_equal 58841, @file.size
13
- assert_equal '.jpg', @file.extname
14
- end
15
-
16
- test "should store/delete file correctly and accept cdn" do
17
- @file.store
18
- uploads_url = @file.url
19
- assert_object_s3 uploads_url
20
-
21
- base_url = 'http://cdn.example.com'
22
- Rails.application.config.attachs.base_url = base_url
23
- assert_equal ::File.join(base_url, @file.path), @file.url
24
- Rails.application.config.attachs.base_url = ''
25
-
26
- @file.delete
27
- assert_not_object_s3 uploads_url
28
- assert !@file.exists?
29
- end
30
-
31
- end
@@ -1,26 +0,0 @@
1
- require 'test_helper'
2
-
3
- class S3ImageRecordTest < ActiveSupport::TestCase
4
-
5
- setup do
6
- load_s3
7
- @record = ImageAttached.create(image: fixture_file_upload('/image.jpg', 'image/jpeg'))
8
- end
9
-
10
- test "should save/destory main image and thumbs" do
11
- original = @record.image.url
12
- big = @record.image.url(:big)
13
- small = @record.image.url(:small)
14
-
15
- assert_object_s3 original
16
- assert_object_s3 big
17
- assert_object_s3 small
18
-
19
- @record.destroy
20
-
21
- assert_not_object_s3 original
22
- assert_not_object_s3 big
23
- assert_not_object_s3 small
24
- end
25
-
26
- end
@@ -1,31 +0,0 @@
1
- require 'test_helper'
2
-
3
- class S3ImageStringTest < ActiveSupport::TestCase
4
-
5
- setup do
6
- load_s3
7
- filename = 'image.jpg'
8
- fixture_file_upload_s3 "/#{filename}", 'image/jpeg', ::File.join('uploads', 'images', 'original', filename)
9
- fixture_file_upload_s3 "/#{filename}", 'image/jpeg', ::File.join('uploads', 'images', 'big', filename)
10
- fixture_file_upload_s3 "/#{filename}", 'image/jpeg', ::File.join('uploads', 'images', 'small', filename)
11
- options = { presets: [:small, :big] }
12
- @image = Attachs::Types::Image.new(filename, options)
13
- end
14
-
15
- test "should save/destory main image and thumbs" do
16
- original = @image.url
17
- big = @image.url(:big)
18
- small = @image.url(:small)
19
-
20
- assert_object_s3 original
21
- assert_object_s3 big
22
- assert_object_s3 small
23
-
24
- @image.delete
25
-
26
- assert_not_object_s3 original
27
- assert_not_object_s3 big
28
- assert_not_object_s3 small
29
- end
30
-
31
- end
@@ -1,28 +0,0 @@
1
- require 'test_helper'
2
-
3
- class S3ImageUploadTest < ActiveSupport::TestCase
4
-
5
- setup do
6
- load_s3
7
- options = { presets: [:small, :big] }
8
- @image = Attachs::Types::Image.new(fixture_file_upload('/image.jpg', 'image/jpeg'), options)
9
- @image.store
10
- end
11
-
12
- test "should save/destory main image and thumbs" do
13
- original = @image.url
14
- big = @image.url(:big)
15
- small = @image.url(:small)
16
-
17
- assert_object_s3 original
18
- assert_object_s3 big
19
- assert_object_s3 small
20
-
21
- @image.delete
22
-
23
- assert_not_object_s3 original
24
- assert_not_object_s3 big
25
- assert_not_object_s3 small
26
- end
27
-
28
- end
@@ -1,56 +0,0 @@
1
- require 'test_helper'
2
-
3
- class S3RecordsTest < ActiveSupport::TestCase
4
-
5
- setup do
6
- load_s3
7
- end
8
-
9
- test "should save/update/destroy from the database and save/destroy the file" do
10
- record = FileAttached.create(file: fixture_file_upload('/image.jpg', 'image/jpeg'))
11
- image_filename = record.file.filename
12
- image_url = record.file.url
13
- assert_object_s3 image_url
14
-
15
- record.update_attributes file: fixture_file_upload('/file.txt', 'text/plain')
16
- file_filename = record.file.filename
17
- assert_not_equal image_filename, file_filename
18
- file_url = record.file.url
19
- assert_not_object_s3 image_url
20
- assert_object_s3 file_url
21
-
22
- record.destroy
23
- assert_not_object_s3 file_url
24
- end
25
-
26
- test "should take default file/image and shouldn't store/delete it" do
27
- record = FileAttached.create
28
- file_filename = 'file.txt'
29
- assert_equal ::File.join('uploads', 'files', file_filename), record.file.path
30
- file_url = record.file.url
31
- assert_object_s3 file_url
32
-
33
- record.destroy
34
- assert record.file.exists?
35
- assert_object_s3 file_url
36
-
37
- record = ImageAttached.create
38
- image_filename = 'image.jpg'
39
- assert_equal ::File.join('uploads', 'images', 'original', image_filename), record.image.path
40
- image_original_url = record.image.url
41
- assert_object_s3 image_original_url
42
- assert_equal ::File.join('uploads', 'images', 'small', image_filename), record.image.path(:small)
43
- image_small_url = record.image.url(:small)
44
- assert_object_s3 image_small_url
45
- assert_equal ::File.join('uploads', 'images', 'big', image_filename), record.image.path(:big)
46
- image_big_url = record.image.url(:big)
47
- assert_object_s3 image_big_url
48
-
49
- record.destroy
50
- assert record.image.exists?
51
- assert_object_s3 image_original_url
52
- assert_object_s3 image_small_url
53
- assert_object_s3 image_big_url
54
- end
55
-
56
- end
@@ -1,72 +0,0 @@
1
- require 'test_helper'
2
-
3
- class S3ValidatorsTest < ActiveSupport::TestCase
4
- include ActionView::Helpers::NumberHelper
5
-
6
- setup do
7
- load_s3
8
- @record = AllAttached.new
9
- end
10
-
11
- test "should check if file is present" do
12
- assert !@record.valid?
13
- assert_equal [I18n.t('errors.messages.attachment_presence')], @record.errors[:file_presence]
14
-
15
- @record.image_presence = fixture_file_upload('/image.jpg', 'image/jpeg')
16
- assert !@record.valid?
17
- assert_equal [], @record.errors[:image_presence]
18
- end
19
-
20
- test "should check the file content type" do
21
- @record.image_content_type = fixture_file_upload('/file.txt', 'text/plain')
22
- assert !@record.valid?
23
- assert_equal [I18n.t('errors.messages.attachment_content_type', types: 'jpg')], @record.errors[:image_content_type]
24
-
25
- @record.file_content_type = fixture_file_upload('/image.jpg', 'image/jpeg')
26
- assert !@record.valid?
27
- assert_equal [I18n.t('errors.messages.attachment_content_type', types: 'txt')], @record.errors[:file_content_type]
28
- end
29
-
30
- test "should check the file size" do
31
- @record.file_size = fixture_file_upload('/file_big.txt', 'text/plain')
32
- assert !@record.valid?
33
- assert_equal [I18n.t('errors.messages.attachment_size_less_than', count: number_to_human_size(15.kilobytes))], @record.errors[:file_size]
34
-
35
- @record.file_size = fixture_file_upload('/file_empty.txt', 'text/plain')
36
- assert !@record.valid?
37
- assert_equal [I18n.t('errors.messages.attachment_size_greater_than', count: number_to_human_size(10.bytes))], @record.errors[:file_size]
38
-
39
- @record.file_size = fixture_file_upload('/file.txt', 'text/plain')
40
- assert !@record.valid?
41
- assert_equal [], @record.errors[:file_size]
42
-
43
- @record.image_size = fixture_file_upload('/image_big.jpg', 'image/jpeg')
44
- assert !@record.valid?
45
- assert_equal [I18n.t('errors.messages.attachment_size_in', min: number_to_human_size(2.kilobytes), max: number_to_human_size(60.kilobytes))], @record.errors[:image_size]
46
-
47
- @record.image_size = fixture_file_upload('/image_empty.jpg', 'image/jpeg')
48
- assert !@record.valid?
49
- assert_equal [I18n.t('errors.messages.attachment_size_in', min: number_to_human_size(2.kilobytes), max: number_to_human_size(60.kilobytes))], @record.errors[:image_size]
50
-
51
- @record.image_size = fixture_file_upload('/image.jpg', 'image/jpeg')
52
- assert !@record.valid?
53
- assert_equal [], @record.errors[:image_size]
54
- end
55
-
56
- test "should check all the validations together" do
57
- @record.file_all = fixture_file_upload('/file.txt', 'text/plain')
58
- assert !@record.valid?
59
- assert_equal [], @record.errors[:file_all]
60
-
61
- @record.image_all = fixture_file_upload('/image.jpg', 'image/jpeg')
62
- assert !@record.valid?
63
- assert_equal [], @record.errors[:image_all]
64
-
65
- assert !@record.valid?
66
- assert_equal [I18n.t('errors.messages.blank')], @record.errors[:file_default]
67
-
68
- assert !@record.valid?
69
- assert_equal [I18n.t('errors.messages.blank')], @record.errors[:image_default]
70
- end
71
-
72
- end