active_storage_validations 0.3.1 → 0.4.0

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: 0f851e68789d12a6cbc4ac6173b78db69614ba6faf29a920f9d5c2ecade36b1c
4
- data.tar.gz: 3bff45f9bc8606a75c04311b1a8735ebe2f1e248aaaab8f3297491a8d97cfa7f
2
+ SHA1:
3
+ metadata.gz: 640238a3efac3a30376f3d5079a630774727ba91
4
+ data.tar.gz: 01a6ff35bcdf26f1b8b0f0a5fdc55be666c47f43
5
5
  SHA512:
6
- metadata.gz: c21d42b022f1a06c13d35cbc944f5c57ae594ef795517241e3fd933a3c57a2fd56630999d356b54200ddf601d86aa89823feb627894db1b3fe096b4189eee7cd
7
- data.tar.gz: b14226508ca1ac0ecd53ceda8978935e20fb47ee950d6941383c1d8d076d42de35a5085dd14c6713f61cf649d963e33c363b00036a375dfc6964a107f64cdb1b
6
+ metadata.gz: 54c834d284344bf828b048eee45e97766892eccbdc5793b3eae5e8ae7ab9e7e5914960d467fc4fae169e738ba9caaf8f050cd326ba42fb12420ec4fff9f12f0f
7
+ data.tar.gz: 2e643131d9f35f5707fac32a680460b348bad03e63b985f9135f38c3de18b17e6785fab42858c8a14651378af55351d07024c171530c81e071a0e7bb8e064c35
data/README.md CHANGED
@@ -34,6 +34,29 @@ class Project < ApplicationRecord
34
34
  end
35
35
  ```
36
36
 
37
+ ## Internationalization (I18n)
38
+
39
+ Active Storage Validations use I18n for errors messages. For this add there keys in your translation file :
40
+
41
+ ```yml
42
+ en:
43
+ errors:
44
+ messages:
45
+ content_type_invalid: "has an invalid content type"
46
+ ```
47
+
48
+ In some cases Active Storage Validations provides variables to help you customize messages :
49
+
50
+ The "content_type_invalid" key has two variables that you can use, a variable named "content_type" containing the content type of the send file and a variable named "authorized_type" containing the list of authorized content types.
51
+
52
+ It's variables are not used by default to leave the choice to the user.
53
+
54
+ For example :
55
+
56
+ ```yml
57
+ content_type_invalid: "has an invalid content type : %{content_type}"
58
+ ```
59
+
37
60
  ## Installation
38
61
 
39
62
  Add this line to your application's Gemfile:
@@ -55,7 +78,6 @@ Very simple example of validation with file attached, content type check and cus
55
78
 
56
79
  ## Todo
57
80
  * verify with remote storages
58
- * better error message when content_type is invalid
59
81
  * travis CI
60
82
  * validation for file size
61
83
 
@@ -66,7 +88,12 @@ To run tests in root folder of gem `rake test`.
66
88
  To play with app `cd test/dummy` and `rails s -b 0.0.0.0` (before `rake db:migrate`).
67
89
 
68
90
  ## Contributing
69
- Contribution directions go here.
91
+ You are welcome to contribute.
92
+
93
+ ## Contributors (BIG THANK YOU)
94
+ - https://github.com/schweigert
95
+ - https://github.com/tleneveu
96
+
70
97
 
71
98
  ## License
72
99
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,4 @@
1
+ en:
2
+ errors:
3
+ messages:
4
+ content_type_invalid: "has an invalid content type"
@@ -1,6 +1,8 @@
1
1
  require "active_storage_validations/railtie"
2
2
 
3
3
  module ActiveStorageValidations
4
+ class Engine < ::Rails::Engine
5
+ end
4
6
 
5
7
  class AttachedValidator < ActiveModel::EachValidator
6
8
  def validate_each(record, attribute, value)
@@ -19,9 +21,14 @@ module ActiveStorageValidations
19
21
 
20
22
  files = Array.wrap(files)
21
23
 
24
+ errors_options = { authorized_types: types_to_human_format }
25
+ errors_options[:message] = options[:message] if options[:message].present?
26
+
22
27
  files.each do |file|
23
- unless content_type_valid?(file)
24
- record.errors.add(attribute, options[:message].presence || :invalid)
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)
25
32
  return
26
33
  end
27
34
  end
@@ -31,6 +38,14 @@ module ActiveStorageValidations
31
38
  Array.wrap(options[:with]) + Array.wrap(options[:in])
32
39
  end
33
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
+
34
49
  def content_type_valid?(file)
35
50
  file.blob.present? && file.blob.content_type.in?(types)
36
51
  end
@@ -1,3 +1,3 @@
1
1
  module ActiveStorageValidations
2
- VERSION = '0.3.1'
2
+ VERSION = '0.4.0'
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.3.1
4
+ version: 0.4.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: 2018-09-18 00:00:00.000000000 Z
11
+ date: 2018-10-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -62,6 +62,7 @@ files:
62
62
  - MIT-LICENSE
63
63
  - README.md
64
64
  - Rakefile
65
+ - config/locales/en.yml
65
66
  - lib/active_storage_validations.rb
66
67
  - lib/active_storage_validations/railtie.rb
67
68
  - lib/active_storage_validations/version.rb
@@ -86,7 +87,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
86
87
  version: '0'
87
88
  requirements: []
88
89
  rubyforge_project:
89
- rubygems_version: 2.7.6
90
+ rubygems_version: 2.6.8
90
91
  signing_key:
91
92
  specification_version: 4
92
93
  summary: Validations for Active Storage