active_storage_validations 0.5.0 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA256:
3
- metadata.gz: 0b1779b51e62cc8db0e51d18a471ce349a2ee7d7a6867921cfa879e9e0e696f9
4
- data.tar.gz: 1f4972a779e7a2938a0d5ef8c322da49300644ec155e1d2a883af13363afc4c4
2
+ SHA1:
3
+ metadata.gz: 2501b8cdd1162e90088185dc89db71838ff63b9d
4
+ data.tar.gz: 2e08cbebaad918109615e604fe78f8ab323b5488
5
5
  SHA512:
6
- metadata.gz: 96539daee539f2ca6faeb7f276e6bec9f102fb62f54e32bee3509eedd996570366b89594349d548121ebb4f268088e6cd389a07b895736433a9f57389c3bdab6
7
- data.tar.gz: 90ef6e23cb8347c25cc5cf9f4d1a78c1001b650d7f352e56794a2590fbb00abf1581dd5e3dd7ddb4c5184d3ccee8ebcc4f01eefe888179d649cd22d3f718a085
6
+ metadata.gz: 42098a546430f2ddc6cdd4f06c51d91bbdde962c06108c4e0d6b8a1a80c69028caac6080dfa19283710c11b6b9f4bb29dcc379add02bec91e7c84995ea4d4ef4
7
+ data.tar.gz: 4922ed55b2e7a9a7ae381e279c21f5c9c25085eda26139c2921cda45110211f970fe8305a934d84b5e4c32db94c5a9a7c407f1acc2327b0e9d0774c57f58e102
data/README.md CHANGED
@@ -77,10 +77,9 @@ Very simple example of validation with file attached, content type check and cus
77
77
  [![Sample](https://raw.githubusercontent.com/igorkasyanchuk/active_storage_validations/master/docs/preview.png)](https://raw.githubusercontent.com/igorkasyanchuk/active_storage_validations/master/docs/preview.png)
78
78
 
79
79
  ## Todo
80
- * verify with remote storages
80
+ * verify with remote storages (s3, etc)
81
+ * verify how it works with direct upload
81
82
  * better error message when content_size is invalid
82
- * travis CI
83
- * split validation classes into own classes
84
83
 
85
84
  ## Tests & Contributing
86
85
 
@@ -94,6 +93,8 @@ You are welcome to contribute.
94
93
  ## Contributors (BIG THANK YOU)
95
94
  - https://github.com/schweigert
96
95
  - https://github.com/tleneveu
96
+ - https://github.com/reckerswartz
97
+ - https://github.com/Uysim
97
98
 
98
99
 
99
100
  ## License
@@ -1,99 +1,10 @@
1
- require "active_storage_validations/railtie"
1
+ require 'active_storage_validations/railtie'
2
+ require 'active_storage_validations/engine'
3
+ require 'active_storage_validations/attached_validator'
4
+ require 'active_storage_validations/content_type_validator'
5
+ require 'active_storage_validations/size_validator'
2
6
 
3
- module ActiveStorageValidations
4
- class Engine < ::Rails::Engine
5
- end
6
7
 
7
- class AttachedValidator < ActiveModel::EachValidator
8
- def validate_each(record, attribute, value)
9
- unless record.send(attribute).attached?
10
- record.errors.add(attribute, :blank)
11
- end
12
- end
13
- end
14
8
 
15
- class ContentTypeValidator < ActiveModel::EachValidator
16
- def validate_each(record, attribute, value)
17
- files = record.send(attribute)
18
-
19
- return true unless files.attached?
20
- return true if types.empty?
21
-
22
- files = Array.wrap(files)
23
-
24
- errors_options = { authorized_types: types_to_human_format }
25
- errors_options[:message] = options[:message] if options[:message].present?
26
-
27
- files.each do |file|
28
- unless content_type_valid?(file)
29
- errors_options[:content_type] = content_type(file)
30
-
31
- record.errors.add(attribute, :content_type_invalid, errors_options)
32
- return
33
- end
34
- end
35
- end
36
-
37
- def types
38
- Array.wrap(options[:with]) + Array.wrap(options[:in])
39
- end
40
-
41
- def types_to_human_format
42
- types.join(", ")
43
- end
44
-
45
- def content_type(file)
46
- file.blob.content_type
47
- end
48
-
49
- def content_type_valid?(file)
50
- file.blob.present? && file.blob.content_type.in?(types)
51
- end
52
- end
53
-
54
- class SizeValidator < ActiveModel::EachValidator
55
- delegate :number_to_human_size, to: ActiveSupport::NumberHelper
56
-
57
- AVAILABLE_CHECKS = [:less_than, :less_than_or_equal_to, :greater_than, :greater_than_or_equal_to, :between]
58
-
59
- def check_validity!
60
- unless (AVAILABLE_CHECKS).any? { |argument| options.has_key?(argument) }
61
- raise ArgumentError, "You must pass either :less_than, :greater_than, or :between to the validator"
62
- end
63
- end
64
-
65
- def validate_each(record, attribute, value)
66
- files = record.send(attribute)
67
- # only attached
68
- return true unless files.attached?
69
-
70
- files = Array.wrap(files)
71
-
72
- files.each do |file|
73
- if content_size_valid?(file)
74
- record.errors.add(attribute, options[:message].presence || "size #{number_to_human_size(file.blob.byte_size)} is not between required range" )
75
- return
76
- end
77
- end
78
- end
79
-
80
- def content_size_valid?(file)
81
- file_size = file.blob.byte_size
82
- case
83
- when options[:between].present?
84
- options[:between].exclude?(file_size)
85
- when options[:less_than].present?
86
- file_size > options[:less_than]
87
- when options[:less_than_or_equal_to].present?
88
- file_size >= options[:less_than_or_equal_to]
89
- when options[:greater_than].present?
90
- file_size < options[:greater_than]
91
- when options[:greater_than_or_equal_to].present?
92
- file_size <= options[:greater_than_or_equal_to]
93
- end
94
- end
95
- end
96
-
97
- end
98
9
 
99
10
  ActiveRecord::Base.send :include, ActiveStorageValidations
@@ -0,0 +1,9 @@
1
+ module ActiveStorageValidations
2
+ class AttachedValidator < ActiveModel::EachValidator
3
+ def validate_each(record, attribute, value)
4
+ unless record.send(attribute).attached?
5
+ record.errors.add(attribute, :blank)
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,40 @@
1
+ module ActiveStorageValidations
2
+ class ContentTypeValidator < ActiveModel::EachValidator
3
+ def validate_each(record, attribute, value)
4
+ files = record.send(attribute)
5
+
6
+ return true unless files.attached?
7
+ return true if types.empty?
8
+
9
+ files = Array.wrap(files)
10
+
11
+ errors_options = { authorized_types: types_to_human_format }
12
+ errors_options[:message] = options[:message] if options[:message].present?
13
+
14
+ files.each do |file|
15
+ unless content_type_valid?(file)
16
+ errors_options[:content_type] = content_type(file)
17
+
18
+ record.errors.add(attribute, :content_type_invalid, errors_options)
19
+ return
20
+ end
21
+ end
22
+ end
23
+
24
+ def types
25
+ Array.wrap(options[:with]) + Array.wrap(options[:in])
26
+ end
27
+
28
+ def types_to_human_format
29
+ types.join(", ")
30
+ end
31
+
32
+ def content_type(file)
33
+ file.blob.content_type
34
+ end
35
+
36
+ def content_type_valid?(file)
37
+ file.blob.present? && file.blob.content_type.in?(types)
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,4 @@
1
+ module ActiveStorageValidations
2
+ class Engine < ::Rails::Engine
3
+ end
4
+ end
@@ -0,0 +1,45 @@
1
+ module ActiveStorageValidations
2
+ class SizeValidator < ActiveModel::EachValidator
3
+ delegate :number_to_human_size, to: ActiveSupport::NumberHelper
4
+
5
+ AVAILABLE_CHECKS = [:less_than, :less_than_or_equal_to, :greater_than, :greater_than_or_equal_to, :between]
6
+
7
+ def check_validity!
8
+ unless (AVAILABLE_CHECKS).any? { |argument| options.has_key?(argument) }
9
+ raise ArgumentError, "You must pass either :less_than, :greater_than, or :between to the validator"
10
+ end
11
+ end
12
+
13
+ def validate_each(record, attribute, value)
14
+ files = record.send(attribute)
15
+ # only attached
16
+ return true unless files.attached?
17
+
18
+ files = Array.wrap(files)
19
+
20
+ files.each do |file|
21
+ if content_size_valid?(file)
22
+ record.errors.add(attribute, options[:message].presence || "size #{number_to_human_size(file.blob.byte_size)} is not between required range" )
23
+ return
24
+ end
25
+ end
26
+ end
27
+
28
+ def content_size_valid?(file)
29
+ file_size = file.blob.byte_size
30
+ case
31
+ when options[:between].present?
32
+ options[:between].exclude?(file_size)
33
+ when options[:less_than].present?
34
+ file_size > options[:less_than]
35
+ when options[:less_than_or_equal_to].present?
36
+ file_size >= options[:less_than_or_equal_to]
37
+ when options[:greater_than].present?
38
+ file_size < options[:greater_than]
39
+ when options[:greater_than_or_equal_to].present?
40
+ file_size <= options[:greater_than_or_equal_to]
41
+ end
42
+ end
43
+ end
44
+
45
+ end
@@ -1,3 +1,3 @@
1
1
  module ActiveStorageValidations
2
- VERSION = '0.5.0'
2
+ VERSION = '0.5.1'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_storage_validations
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Igor Kasyanchuk
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-10-03 00:00:00.000000000 Z
11
+ date: 2018-10-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -64,7 +64,11 @@ files:
64
64
  - Rakefile
65
65
  - config/locales/en.yml
66
66
  - lib/active_storage_validations.rb
67
+ - lib/active_storage_validations/attached_validator.rb
68
+ - lib/active_storage_validations/content_type_validator.rb
69
+ - lib/active_storage_validations/engine.rb
67
70
  - lib/active_storage_validations/railtie.rb
71
+ - lib/active_storage_validations/size_validator.rb
68
72
  - lib/active_storage_validations/version.rb
69
73
  - lib/tasks/active_storage_validations_tasks.rake
70
74
  homepage: https://github.com/igorkasyanchuk
@@ -87,7 +91,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
87
91
  version: '0'
88
92
  requirements: []
89
93
  rubyforge_project:
90
- rubygems_version: 2.7.6
94
+ rubygems_version: 2.6.8
91
95
  signing_key:
92
96
  specification_version: 4
93
97
  summary: Validations for Active Storage