active_storage_validations 0.1 → 0.2

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
2
  SHA1:
3
- metadata.gz: f5f1df5984d83688b37806498ea07254ac323c01
4
- data.tar.gz: a9b84d279013e32bdb26faa1174abff6fd130955
3
+ metadata.gz: 0cd3e51c1736b2926e53a3ca8ffbde43a42d23cc
4
+ data.tar.gz: d84e8f896c60622cffd597d7480549b05eaa5b84
5
5
  SHA512:
6
- metadata.gz: 7cd0835f1ce81bb40a370ed1b282ce3c6b4e50171d11531d1b38619c3eec3ef48774948f5c09f9413fda9316077f0fcb68586553a507696faba5b8827859a76c
7
- data.tar.gz: be602197bfd8157eba51524ef0126d3b98d3150448387b54a52ac357a13a3afa3cb412e7b20893020719868af5dc3c3625b38041bdf54f3705f52b7632367111
6
+ metadata.gz: 038e132b37ea4248c54dd604dd5e5f57f1ae239a692105366ebe34e561eba220e1d95924418a83f807d3ba87b39f889f18facac92a64781c7fc6d0a3fed0fbd6
7
+ data.tar.gz: 6a4af9ce49ff16d25bfe975a68f3c04faad4dd83636a59017ff8d9382657133a33e362a33ad7ed5bf9c9b89cb7ce2750c3121c30394adc7f1fce4a6f89be12b1
data/README.md CHANGED
@@ -1,10 +1,27 @@
1
- # ActiveStorageValidations
2
- Short description and motivation.
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
- How to use my plugin.
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
- Or install it yourself as:
20
- ```bash
21
- $ gem install active_storage_validations
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
 
@@ -1,3 +1,3 @@
1
1
  module ActiveStorageValidations
2
- VERSION = '0.1'
2
+ VERSION = '0.2'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_storage_validations
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.1'
4
+ version: '0.2'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Igor Kasyanchuk