active_storage_validations 0.1 → 0.2
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 +25 -7
- data/lib/active_storage_validations.rb +31 -1
- data/lib/active_storage_validations/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0cd3e51c1736b2926e53a3ca8ffbde43a42d23cc
|
4
|
+
data.tar.gz: d84e8f896c60622cffd597d7480549b05eaa5b84
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 038e132b37ea4248c54dd604dd5e5f57f1ae239a692105366ebe34e561eba220e1d95924418a83f807d3ba87b39f889f18facac92a64781c7fc6d0a3fed0fbd6
|
7
|
+
data.tar.gz: 6a4af9ce49ff16d25bfe975a68f3c04faad4dd83636a59017ff8d9382657133a33e362a33ad7ed5bf9c9b89cb7ce2750c3121c30394adc7f1fce4a6f89be12b1
|
data/README.md
CHANGED
@@ -1,10 +1,27 @@
|
|
1
|
-
#
|
2
|
-
|
1
|
+
# ActiveStorage Validations
|
2
|
+
|
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
|
+
|
5
|
+
This gems doing it for you. Just use `attached: true` or `content_type: 'image/png'` validation.
|
3
6
|
|
4
7
|
## Usage
|
5
|
-
|
8
|
+
|
9
|
+
For example you have a model like this and you want to add validation.
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
class User < ApplicationRecord
|
13
|
+
has_one_attached :avatar
|
14
|
+
has_many_attached :photos
|
15
|
+
|
16
|
+
validates :name, presence: true
|
17
|
+
|
18
|
+
validates :avatar, attached: true, content_type: 'image/png'
|
19
|
+
validates :photos, attached: true, content_type: ['image/png', 'image/jpg']
|
20
|
+
end
|
21
|
+
```
|
6
22
|
|
7
23
|
## Installation
|
24
|
+
|
8
25
|
Add this line to your application's Gemfile:
|
9
26
|
|
10
27
|
```ruby
|
@@ -16,10 +33,11 @@ And then execute:
|
|
16
33
|
$ bundle
|
17
34
|
```
|
18
35
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
36
|
+
## Tests & Contributing
|
37
|
+
|
38
|
+
To run tests in root folder of gem `rake test`.
|
39
|
+
|
40
|
+
To play with app `cd test/dummy` and `rails s -b 0.0.0.0` (before `rake db:migrate`).
|
23
41
|
|
24
42
|
## Contributing
|
25
43
|
Contribution directions go here.
|
@@ -8,7 +8,37 @@ module ActiveStorageValidations
|
|
8
8
|
record.errors.add(attribute, :blank)
|
9
9
|
end
|
10
10
|
end
|
11
|
-
end
|
11
|
+
end
|
12
|
+
|
13
|
+
class ContentTypeValidator < ActiveModel::EachValidator
|
14
|
+
def validate_each(record, attribute, value)
|
15
|
+
files = record.send(attribute)
|
16
|
+
|
17
|
+
# puts "#{attribute} --- #{value} --- #{options[:with]} --- #{options}"
|
18
|
+
|
19
|
+
# only attached
|
20
|
+
return true unless files.attached?
|
21
|
+
return true if types.empty?
|
22
|
+
|
23
|
+
files = Array.wrap(files)
|
24
|
+
|
25
|
+
files.each do |file|
|
26
|
+
unless content_type_valid?(file)
|
27
|
+
record.errors.add(attribute, :invalid)
|
28
|
+
return
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def types
|
34
|
+
Array.wrap(options[:with]) + Array.wrap(options[:in])
|
35
|
+
end
|
36
|
+
|
37
|
+
def content_type_valid?(file)
|
38
|
+
file.blob.content_type.in?(types)
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
12
42
|
|
13
43
|
end
|
14
44
|
|