active_storage_validations 0.5.3 → 0.6.0
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.
- checksums.yaml +4 -4
- data/README.md +22 -0
- data/config/locales/en.yml +1 -0
- data/lib/active_storage_validations.rb +1 -0
- data/lib/active_storage_validations/limit_validator.rb +37 -0
- data/lib/active_storage_validations/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0eeb2c72a1719accd4a287eaad2d12fd0b60dd3ea196d0e68afeeb87a8acca03
|
4
|
+
data.tar.gz: 748af563170ca1c931a000a2126792e97aaa4e775ccf61d0d3938412c6f741de
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: deb20600c0abc0abd12db58705fd64d8a5b4de214861c78ce18396b47178c7c42820c66e2137ed3ae259dde94a0fc71fd179b7907d98849d4bcc6ac795b84e3e
|
7
|
+
data.tar.gz: cefcd5c92ca650366f3eccc4431fe4431b1543ae211013a00c997f2dd8c249b429758f8a419e46ae79f94b9e2313aa7fc6edd503f9d79e0885fe8e7549842cbe
|
data/README.md
CHANGED
@@ -4,6 +4,16 @@ If you are using `active_storage` gem and you want to add simple validations for
|
|
4
4
|
|
5
5
|
This gems doing it for you. Just use `attached: true` or `content_type: 'image/png'` validation.
|
6
6
|
|
7
|
+
[](https://travis-ci.org/igorkasyanchuk/active_storage_validations)
|
8
|
+
|
9
|
+
## What it can do
|
10
|
+
|
11
|
+
* validates if file(s) attached
|
12
|
+
* validates content type
|
13
|
+
* validates size of files
|
14
|
+
* validates number of uploaded files (min/max required)
|
15
|
+
* custom error messages
|
16
|
+
|
7
17
|
## Usage
|
8
18
|
|
9
19
|
For example you have a model like this and you want to add validation.
|
@@ -26,11 +36,13 @@ or
|
|
26
36
|
class Project < ApplicationRecord
|
27
37
|
has_one_attached :preview
|
28
38
|
has_one_attached :attachment
|
39
|
+
has_many_attached :documents
|
29
40
|
|
30
41
|
validates :title, presence: true
|
31
42
|
|
32
43
|
validates :preview, attached: true, size: { less_than: 100.megabytes , message: 'is not given between size' }
|
33
44
|
validates :attachment, attached: true, content_type: { in: 'application/pdf', message: 'is not a PDF' }
|
45
|
+
validates :documents, limit: { min: 1, max: 3 }
|
34
46
|
end
|
35
47
|
```
|
36
48
|
|
@@ -43,6 +55,7 @@ en:
|
|
43
55
|
errors:
|
44
56
|
messages:
|
45
57
|
content_type_invalid: "has an invalid content type"
|
58
|
+
limit_out_of_range: "total number is out of range"
|
46
59
|
```
|
47
60
|
|
48
61
|
In some cases Active Storage Validations provides variables to help you customize messages :
|
@@ -57,6 +70,14 @@ For example :
|
|
57
70
|
content_type_invalid: "has an invalid content type : %{content_type}"
|
58
71
|
```
|
59
72
|
|
73
|
+
Also the "limit_out_of_range" key supports two variables the "min" and "max".
|
74
|
+
|
75
|
+
For example :
|
76
|
+
|
77
|
+
```yml
|
78
|
+
limit_out_of_range: "total number is out of range. range: [%{min}, %{max}]"
|
79
|
+
```
|
80
|
+
|
60
81
|
## Installation
|
61
82
|
|
62
83
|
Add this line to your application's Gemfile:
|
@@ -107,6 +128,7 @@ You are welcome to contribute.
|
|
107
128
|
- https://github.com/reckerswartz
|
108
129
|
- https://github.com/Uysim
|
109
130
|
- https://github.com/D-system
|
131
|
+
- https://github.com/ivanelrey
|
110
132
|
|
111
133
|
## License
|
112
134
|
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/config/locales/en.yml
CHANGED
@@ -3,6 +3,7 @@ require 'active_storage_validations/engine'
|
|
3
3
|
require 'active_storage_validations/attached_validator'
|
4
4
|
require 'active_storage_validations/content_type_validator'
|
5
5
|
require 'active_storage_validations/size_validator'
|
6
|
+
require 'active_storage_validations/limit_validator'
|
6
7
|
|
7
8
|
ActiveSupport.on_load(:active_record) do
|
8
9
|
self.send :include, ActiveStorageValidations
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module ActiveStorageValidations
|
2
|
+
class LimitValidator < ActiveModel::EachValidator
|
3
|
+
AVAILABLE_CHECKS = [:max, :min]
|
4
|
+
|
5
|
+
def check_validity!
|
6
|
+
unless (AVAILABLE_CHECKS).any? { |argument| options.has_key?(argument) }
|
7
|
+
raise ArgumentError, "You must pass either :max or :min to the validator"
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def validate_each(record, attribute, value)
|
12
|
+
files = record.send(attribute)
|
13
|
+
|
14
|
+
files = Array.wrap(files)
|
15
|
+
|
16
|
+
errors_options = {}
|
17
|
+
errors_options[:min] = options[:min]
|
18
|
+
errors_options[:max] = options[:max]
|
19
|
+
|
20
|
+
unless files_count_valid?(files.count)
|
21
|
+
record.errors.add(attribute, options[:message].presence || :limit_out_of_range, errors_options)
|
22
|
+
return
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def files_count_valid?(count)
|
27
|
+
case
|
28
|
+
when options[:max].present? && options[:min].present?
|
29
|
+
count >= options[:min] && count <= options[:max]
|
30
|
+
when options[:max].present?
|
31
|
+
count <= options[:max]
|
32
|
+
when options[:min].present?
|
33
|
+
count >= options[:min]
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
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.
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Igor Kasyanchuk
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-01-
|
11
|
+
date: 2019-01-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -67,6 +67,7 @@ files:
|
|
67
67
|
- lib/active_storage_validations/attached_validator.rb
|
68
68
|
- lib/active_storage_validations/content_type_validator.rb
|
69
69
|
- lib/active_storage_validations/engine.rb
|
70
|
+
- lib/active_storage_validations/limit_validator.rb
|
70
71
|
- lib/active_storage_validations/railtie.rb
|
71
72
|
- lib/active_storage_validations/size_validator.rb
|
72
73
|
- lib/active_storage_validations/version.rb
|