active_storage_validations 0.6.0 → 0.6.1

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
  SHA256:
3
- metadata.gz: 0eeb2c72a1719accd4a287eaad2d12fd0b60dd3ea196d0e68afeeb87a8acca03
4
- data.tar.gz: 748af563170ca1c931a000a2126792e97aaa4e775ccf61d0d3938412c6f741de
3
+ metadata.gz: c5c26b96ccb72cbb6ca57cf20df590f334703e2e30e51e4f2ccc1ff5891a74f5
4
+ data.tar.gz: 401043685c41419a8ab9018de42f844a15d2d1219b74f0f1c37473840129bdea
5
5
  SHA512:
6
- metadata.gz: deb20600c0abc0abd12db58705fd64d8a5b4de214861c78ce18396b47178c7c42820c66e2137ed3ae259dde94a0fc71fd179b7907d98849d4bcc6ac795b84e3e
7
- data.tar.gz: cefcd5c92ca650366f3eccc4431fe4431b1543ae211013a00c997f2dd8c249b429758f8a419e46ae79f94b9e2313aa7fc6edd503f9d79e0885fe8e7549842cbe
6
+ metadata.gz: c0cda1fa4d225a0bf381fb08a2db575d1f3a2baef5b85bd954473455a26c454af2efc0de4b40ecc9156a9b553f38cd8653cb78f7c70a05c87291d0a0a01df94e
7
+ data.tar.gz: c98ff98594d610a57cc649f66bc7a5bd3ac135723dae52f68d807e6ccf0131e76fc5e9e577708a36badf20583ce08f61b7a0c9f3286e53f119e4e39f04714407
data/MIT-LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright 2018 Igor Kasyanchuk
1
+ Copyright 2018-2019 Igor Kasyanchuk
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/Rakefile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  begin
2
4
  require 'bundler/setup'
3
5
  rescue LoadError
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'active_storage_validations/railtie'
2
4
  require 'active_storage_validations/engine'
3
5
  require 'active_storage_validations/attached_validator'
@@ -6,5 +8,5 @@ require 'active_storage_validations/size_validator'
6
8
  require 'active_storage_validations/limit_validator'
7
9
 
8
10
  ActiveSupport.on_load(:active_record) do
9
- self.send :include, ActiveStorageValidations
11
+ send :include, ActiveStorageValidations
10
12
  end
@@ -1,9 +1,11 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveStorageValidations
2
- class AttachedValidator < ActiveModel::EachValidator
3
- def validate_each(record, attribute, value)
4
- unless record.send(attribute).attached?
5
- record.errors.add(attribute, :blank)
6
- end
4
+ class AttachedValidator < ActiveModel::EachValidator # :nodoc:
5
+ def validate_each(record, attribute, _value)
6
+ return if record.send(attribute).attached?
7
+
8
+ record.errors.add(attribute, :blank)
7
9
  end
8
10
  end
9
11
  end
@@ -1,10 +1,11 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveStorageValidations
2
- class ContentTypeValidator < ActiveModel::EachValidator
3
- def validate_each(record, attribute, value)
4
+ class ContentTypeValidator < ActiveModel::EachValidator # :nodoc:
5
+ def validate_each(record, attribute, _value)
4
6
  files = record.send(attribute)
5
7
 
6
- return true unless files.attached?
7
- return true if types.empty?
8
+ return true if !files.attached? || types.empty?
8
9
 
9
10
  files = Array.wrap(files)
10
11
 
@@ -12,12 +13,11 @@ module ActiveStorageValidations
12
13
  errors_options[:message] = options[:message] if options[:message].present?
13
14
 
14
15
  files.each do |file|
15
- unless content_type_valid?(file)
16
- errors_options[:content_type] = content_type(file)
16
+ next if content_type_valid?(file)
17
17
 
18
- record.errors.add(attribute, :content_type_invalid, errors_options)
19
- return
20
- end
18
+ errors_options[:content_type] = content_type(file)
19
+ record.errors.add(attribute, :content_type_invalid, errors_options)
20
+ break
21
21
  end
22
22
  end
23
23
 
@@ -26,7 +26,7 @@ module ActiveStorageValidations
26
26
  end
27
27
 
28
28
  def types_to_human_format
29
- types.join(", ")
29
+ types.join(', ')
30
30
  end
31
31
 
32
32
  def content_type(file)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveStorageValidations
2
4
  class Engine < ::Rails::Engine
3
5
  end
@@ -1,36 +1,38 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveStorageValidations
2
- class LimitValidator < ActiveModel::EachValidator
3
- AVAILABLE_CHECKS = [:max, :min]
4
+ class LimitValidator < ActiveModel::EachValidator # :nodoc:
5
+ AVAILABLE_CHECKS = %i[max min].freeze
4
6
 
5
7
  def check_validity!
6
- unless (AVAILABLE_CHECKS).any? { |argument| options.has_key?(argument) }
7
- raise ArgumentError, "You must pass either :max or :min to the validator"
8
- end
8
+ return true if AVAILABLE_CHECKS.any? { |argument| options.key?(argument) }
9
+
10
+ raise ArgumentError, 'You must pass either :max or :min to the validator'
9
11
  end
10
12
 
11
- def validate_each(record, attribute, value)
13
+ def validate_each(record, attribute, _value)
12
14
  files = record.send(attribute)
13
15
 
16
+ return true unless files.attached?
17
+
14
18
  files = Array.wrap(files)
15
19
 
16
20
  errors_options = {}
17
21
  errors_options[:min] = options[:min]
18
22
  errors_options[:max] = options[:max]
19
23
 
20
- unless files_count_valid?(files.count)
21
- record.errors.add(attribute, options[:message].presence || :limit_out_of_range, errors_options)
22
- return
23
- end
24
+ return true if files_count_valid?(files.count)
25
+
26
+ record.errors.add(attribute, options[:message].presence || :limit_out_of_range, errors_options)
24
27
  end
25
28
 
26
29
  def files_count_valid?(count)
27
- case
28
- when options[:max].present? && options[:min].present?
29
- count >= options[:min] && count <= options[:max]
30
- when options[:max].present?
31
- count <= options[:max]
32
- when options[:min].present?
33
- count >= options[:min]
30
+ if options[:max].present? && options[:min].present?
31
+ count >= options[:min] && count <= options[:max]
32
+ elsif options[:max].present?
33
+ count <= options[:max]
34
+ elsif options[:min].present?
35
+ count >= options[:min]
34
36
  end
35
37
  end
36
38
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveStorageValidations
2
4
  class Railtie < ::Rails::Railtie
3
5
  end
@@ -1,45 +1,43 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveStorageValidations
2
- class SizeValidator < ActiveModel::EachValidator
4
+ class SizeValidator < ActiveModel::EachValidator # :nodoc:
3
5
  delegate :number_to_human_size, to: ActiveSupport::NumberHelper
4
6
 
5
- AVAILABLE_CHECKS = [:less_than, :less_than_or_equal_to, :greater_than, :greater_than_or_equal_to, :between]
7
+ AVAILABLE_CHECKS = %i[less_than less_than_or_equal_to greater_than greater_than_or_equal_to between].freeze
6
8
 
7
9
  def check_validity!
8
- unless (AVAILABLE_CHECKS).any? { |argument| options.has_key?(argument) }
9
- raise ArgumentError, "You must pass either :less_than, :greater_than, or :between to the validator"
10
- end
10
+ return true if AVAILABLE_CHECKS.any? { |argument| options.key?(argument) }
11
+
12
+ raise ArgumentError, 'You must pass either :less_than, :greater_than, or :between to the validator'
11
13
  end
12
14
 
13
- def validate_each(record, attribute, value)
14
- files = record.send(attribute)
15
+ def validate_each(record, attribute, _value)
15
16
  # only attached
16
- return true unless files.attached?
17
+ return true unless record.send(attribute).attached?
17
18
 
18
- files = Array.wrap(files)
19
+ files = Array.wrap(record.send(attribute))
19
20
 
20
21
  files.each do |file|
21
- if content_size_valid?(file)
22
- record.errors.add(attribute, options[:message].presence || "size #{number_to_human_size(file.blob.byte_size)} is not between required range" )
23
- return
24
- end
22
+ next if content_size_valid?(file.blob.byte_size)
23
+
24
+ record.errors.add(attribute, options[:message].presence || "size #{number_to_human_size(file.blob.byte_size)} is not between required range")
25
+ break
25
26
  end
26
27
  end
27
28
 
28
- def content_size_valid?(file)
29
- file_size = file.blob.byte_size
30
- case
31
- when options[:between].present?
32
- options[:between].exclude?(file_size)
33
- when options[:less_than].present?
34
- file_size > options[:less_than]
35
- when options[:less_than_or_equal_to].present?
36
- file_size >= options[:less_than_or_equal_to]
37
- when options[:greater_than].present?
38
- file_size < options[:greater_than]
39
- when options[:greater_than_or_equal_to].present?
40
- file_size <= options[:greater_than_or_equal_to]
29
+ def content_size_valid?(file_size)
30
+ if options[:between].present?
31
+ options[:between].include?(file_size)
32
+ elsif options[:less_than].present?
33
+ file_size < options[:less_than]
34
+ elsif options[:less_than_or_equal_to].present?
35
+ file_size <= options[:less_than_or_equal_to]
36
+ elsif options[:greater_than].present?
37
+ file_size > options[:greater_than]
38
+ elsif options[:greater_than_or_equal_to].present?
39
+ file_size >= options[:greater_than_or_equal_to]
41
40
  end
42
41
  end
43
42
  end
44
-
45
43
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveStorageValidations
2
- VERSION = '0.6.0'
4
+ VERSION = '0.6.1'
3
5
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # desc "Explaining what the task does"
2
4
  # task :active_storage_validations do
3
5
  # # Task goes here
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.6.0
4
+ version: 0.6.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: 2019-01-29 00:00:00.000000000 Z
11
+ date: 2019-02-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rubocop
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: sqlite3
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -91,7 +105,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
91
105
  - !ruby/object:Gem::Version
92
106
  version: '0'
93
107
  requirements: []
94
- rubygems_version: 3.0.2
108
+ rubyforge_project:
109
+ rubygems_version: 2.7.7
95
110
  signing_key:
96
111
  specification_version: 4
97
112
  summary: Validations for Active Storage