active_storage_validations 0.4.0 → 0.5.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
- SHA1:
3
- metadata.gz: 640238a3efac3a30376f3d5079a630774727ba91
4
- data.tar.gz: 01a6ff35bcdf26f1b8b0f0a5fdc55be666c47f43
2
+ SHA256:
3
+ metadata.gz: 0b1779b51e62cc8db0e51d18a471ce349a2ee7d7a6867921cfa879e9e0e696f9
4
+ data.tar.gz: 1f4972a779e7a2938a0d5ef8c322da49300644ec155e1d2a883af13363afc4c4
5
5
  SHA512:
6
- metadata.gz: 54c834d284344bf828b048eee45e97766892eccbdc5793b3eae5e8ae7ab9e7e5914960d467fc4fae169e738ba9caaf8f050cd326ba42fb12420ec4fff9f12f0f
7
- data.tar.gz: 2e643131d9f35f5707fac32a680460b348bad03e63b985f9135f38c3de18b17e6785fab42858c8a14651378af55351d07024c171530c81e071a0e7bb8e064c35
6
+ metadata.gz: 96539daee539f2ca6faeb7f276e6bec9f102fb62f54e32bee3509eedd996570366b89594349d548121ebb4f268088e6cd389a07b895736433a9f57389c3bdab6
7
+ data.tar.gz: 90ef6e23cb8347c25cc5cf9f4d1a78c1001b650d7f352e56794a2590fbb00abf1581dd5e3dd7ddb4c5184d3ccee8ebcc4f01eefe888179d649cd22d3f718a085
data/README.md CHANGED
@@ -29,7 +29,7 @@ class Project < ApplicationRecord
29
29
 
30
30
  validates :title, presence: true
31
31
 
32
- validates :preview, attached: true
32
+ validates :preview, attached: true, size: { less_than: 100.megabytes , message: 'is not given between size' }
33
33
  validates :attachment, attached: true, content_type: { in: 'application/pdf', message: 'is not a PDF' }
34
34
  end
35
35
  ```
@@ -78,14 +78,15 @@ Very simple example of validation with file attached, content type check and cus
78
78
 
79
79
  ## Todo
80
80
  * verify with remote storages
81
+ * better error message when content_size is invalid
81
82
  * travis CI
82
- * validation for file size
83
+ * split validation classes into own classes
83
84
 
84
85
  ## Tests & Contributing
85
86
 
86
87
  To run tests in root folder of gem `rake test`.
87
88
 
88
- To play with app `cd test/dummy` and `rails s -b 0.0.0.0` (before `rake db:migrate`).
89
+ To play with app `cd test/dummy` and `rails s -b 0.0.0.0` (before `rails db:migrate`).
89
90
 
90
91
  ## Contributing
91
92
  You are welcome to contribute.
@@ -49,7 +49,49 @@ module ActiveStorageValidations
49
49
  def content_type_valid?(file)
50
50
  file.blob.present? && file.blob.content_type.in?(types)
51
51
  end
52
+ end
53
+
54
+ class SizeValidator < ActiveModel::EachValidator
55
+ delegate :number_to_human_size, to: ActiveSupport::NumberHelper
56
+
57
+ AVAILABLE_CHECKS = [:less_than, :less_than_or_equal_to, :greater_than, :greater_than_or_equal_to, :between]
58
+
59
+ def check_validity!
60
+ unless (AVAILABLE_CHECKS).any? { |argument| options.has_key?(argument) }
61
+ raise ArgumentError, "You must pass either :less_than, :greater_than, or :between to the validator"
62
+ end
63
+ end
64
+
65
+ def validate_each(record, attribute, value)
66
+ files = record.send(attribute)
67
+ # only attached
68
+ return true unless files.attached?
52
69
 
70
+ files = Array.wrap(files)
71
+
72
+ files.each do |file|
73
+ if content_size_valid?(file)
74
+ record.errors.add(attribute, options[:message].presence || "size #{number_to_human_size(file.blob.byte_size)} is not between required range" )
75
+ return
76
+ end
77
+ end
78
+ end
79
+
80
+ def content_size_valid?(file)
81
+ file_size = file.blob.byte_size
82
+ case
83
+ when options[:between].present?
84
+ options[:between].exclude?(file_size)
85
+ when options[:less_than].present?
86
+ file_size > options[:less_than]
87
+ when options[:less_than_or_equal_to].present?
88
+ file_size >= options[:less_than_or_equal_to]
89
+ when options[:greater_than].present?
90
+ file_size < options[:greater_than]
91
+ when options[:greater_than_or_equal_to].present?
92
+ file_size <= options[:greater_than_or_equal_to]
93
+ end
94
+ end
53
95
  end
54
96
 
55
97
  end
@@ -1,3 +1,3 @@
1
1
  module ActiveStorageValidations
2
- VERSION = '0.4.0'
2
+ VERSION = '0.5.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.4.0
4
+ version: 0.5.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-10-02 00:00:00.000000000 Z
11
+ date: 2018-10-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -87,7 +87,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
87
87
  version: '0'
88
88
  requirements: []
89
89
  rubyforge_project:
90
- rubygems_version: 2.6.8
90
+ rubygems_version: 2.7.6
91
91
  signing_key:
92
92
  specification_version: 4
93
93
  summary: Validations for Active Storage