active_storage_validations 0.3 → 0.3.1
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 +8 -2
- data/lib/active_storage_validations.rb +1 -9
- data/lib/active_storage_validations/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0f851e68789d12a6cbc4ac6173b78db69614ba6faf29a920f9d5c2ecade36b1c
|
4
|
+
data.tar.gz: 3bff45f9bc8606a75c04311b1a8735ebe2f1e248aaaab8f3297491a8d97cfa7f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c21d42b022f1a06c13d35cbc944f5c57ae594ef795517241e3fd933a3c57a2fd56630999d356b54200ddf601d86aa89823feb627894db1b3fe096b4189eee7cd
|
7
|
+
data.tar.gz: b14226508ca1ac0ecd53ceda8978935e20fb47ee950d6941383c1d8d076d42de35a5085dd14c6713f61cf649d963e33c363b00036a375dfc6964a107f64cdb1b
|
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
#
|
1
|
+
# Active Storage Validations
|
2
2
|
|
3
3
|
If you are using `active_storage` gem and you want to add simple validations for it, like presence or content_type you need to write a custom valiation method.
|
4
4
|
|
@@ -16,7 +16,7 @@ class User < ApplicationRecord
|
|
16
16
|
validates :name, presence: true
|
17
17
|
|
18
18
|
validates :avatar, attached: true, content_type: 'image/png'
|
19
|
-
validates :photos, attached: true, content_type: ['image/png', 'image/jpg']
|
19
|
+
validates :photos, attached: true, content_type: ['image/png', 'image/jpg', 'image/jpeg']
|
20
20
|
end
|
21
21
|
```
|
22
22
|
|
@@ -47,6 +47,12 @@ And then execute:
|
|
47
47
|
$ bundle
|
48
48
|
```
|
49
49
|
|
50
|
+
## Sample
|
51
|
+
|
52
|
+
Very simple example of validation with file attached, content type check and custom error message.
|
53
|
+
|
54
|
+
[](https://raw.githubusercontent.com/igorkasyanchuk/active_storage_validations/master/docs/preview.png)
|
55
|
+
|
50
56
|
## Todo
|
51
57
|
* verify with remote storages
|
52
58
|
* better error message when content_type is invalid
|
@@ -11,17 +11,9 @@ module ActiveStorageValidations
|
|
11
11
|
end
|
12
12
|
|
13
13
|
class ContentTypeValidator < ActiveModel::EachValidator
|
14
|
-
|
15
|
-
# def initialize(options)
|
16
|
-
# super(options)
|
17
|
-
# end
|
18
|
-
|
19
14
|
def validate_each(record, attribute, value)
|
20
15
|
files = record.send(attribute)
|
21
16
|
|
22
|
-
# puts "#{attribute} --- #{value} --- #{options[:with]} --- #{options}"
|
23
|
-
|
24
|
-
# only attached
|
25
17
|
return true unless files.attached?
|
26
18
|
return true if types.empty?
|
27
19
|
|
@@ -40,7 +32,7 @@ module ActiveStorageValidations
|
|
40
32
|
end
|
41
33
|
|
42
34
|
def content_type_valid?(file)
|
43
|
-
file.blob.content_type.in?(types)
|
35
|
+
file.blob.present? && file.blob.content_type.in?(types)
|
44
36
|
end
|
45
37
|
|
46
38
|
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:
|
4
|
+
version: 0.3.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-
|
11
|
+
date: 2018-09-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|