mini_defender 0.3.7 → 0.3.9

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: '0058922331b65722f8c953a4be41c6f7de1762a96584821d0b3b4774748d937a'
4
- data.tar.gz: 97335de65f3caf810f04b3a16ce10dfd4dc3bc8bcdb0c8bebdfc6d04c06099bf
3
+ metadata.gz: f577c7a22376c0e4217fdebe7ddf461cb302471e07b2de234e0843ef55c9c603
4
+ data.tar.gz: 6105884dc720a11d506c7c7fc62b24d1f8cd7afc9fa14ce4ed5b44c028e1620c
5
5
  SHA512:
6
- metadata.gz: 49f1c55b33b9ddea8144415d154cc92633552135535d3b7fe409bb80e464c3a5231bc6402cae509386675291c3693119d310244727312023d136cf5b68c98d80
7
- data.tar.gz: be407d3c9aed3bc44ac00db7a85967c3c759d5ee9cf26d8fc22b286009e313aa4775877c24e53eeacdded71ed6fb04bf269487ffc4579373ed0772b8278e8d26
6
+ metadata.gz: ab08c1074c9c4b06f16c11e545b9f8b05811e4be8cc82c43a98315605e21553821d97b9688b1acdcd446b546a65d8d25af4f0e5b725dad09a189bfa51f74ea85
7
+ data.tar.gz: c325ff0ddfb62768fb5fdd74ca08bb8e97bd76f16841190178e9c924dddd0b4f6e3e010026bfefc417a5a6a845fbbf6c3db7b9a28414aa96a75649adac56f82c
@@ -19,7 +19,7 @@ class Hash
19
19
  [k, v.is_a?(Hash) ? v.compact_keys : v]
20
20
  end
21
21
 
22
- if result.all? { |k, _v| k.match?(/\A\d+\z/) }
22
+ if !result.empty? && result.all? { |k, _v| k.match?(/\A\d+\z/) }
23
23
  result.values
24
24
  else
25
25
  result
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HandlesValidationErrors
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ rescue_from MiniDefender::ValidationError, with: :handle_validation_error
8
+ end
9
+
10
+ private
11
+
12
+ def handle_validation_error(error)
13
+ respond_to do |format|
14
+ format.html do
15
+ flash[:error] = error.message
16
+ flash[:validation_errors] = error.errors
17
+ flash[:old_values] = safe_values
18
+
19
+ redirect_back fallback_location: root_path
20
+ end
21
+
22
+ format.json do
23
+ render json: { message: error.message, errors: error.errors }, status: :unprocessable_entity
24
+ end
25
+ end
26
+ end
27
+
28
+ def safe_values
29
+ forbidden_keys = %w[password password_confirmation]
30
+
31
+ params.to_unsafe_h
32
+ .deep_stringify_keys
33
+ .reject { |k, _| forbidden_keys.include?(k) }
34
+ end
35
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ValidationHelpers
4
+ extend ActiveSupport::Concern
5
+
6
+ def validation_errors
7
+ @validation_errors ||= flash[:validation_errors] || {}
8
+ end
9
+
10
+ def has_validation_errors?
11
+ validation_errors.length > 0
12
+ end
13
+
14
+ def old_values
15
+ @old_values ||= flash[:old_values] || {}
16
+ end
17
+
18
+ def old_value(field, default = nil)
19
+ # Transform Rails foo[bar] convention to foo.bar
20
+ field = field.gsub('[', '.').gsub(']', '')
21
+ field = field.split('.')
22
+
23
+ result = old_values.deep_stringify_keys
24
+
25
+ while (key = field.shift)
26
+ key = key.to_i if Array === result
27
+
28
+ if Hash === result[key] || Array === result[key] || field.empty?
29
+ result = result[key]
30
+ end
31
+ end
32
+
33
+ result || default
34
+ end
35
+
36
+ def field_errors(field)
37
+ raise ArgumentError, 'Expected a block.' unless block_given?
38
+
39
+ errors = validation_errors[field] || []
40
+ errors.each { |e| yield(e) }
41
+ end
42
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MiniDefender
4
- VERSION = "0.3.7"
4
+ VERSION = "0.3.9"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mini_defender
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.7
4
+ version: 0.3.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ali Alhoshaiyan
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-07-18 00:00:00.000000000 Z
11
+ date: 2023-09-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -84,6 +84,7 @@ files:
84
84
  - lib/mini_defender.rb
85
85
  - lib/mini_defender/extensions/enumerable.rb
86
86
  - lib/mini_defender/extensions/hash.rb
87
+ - lib/mini_defender/handles_validation_errors.rb
87
88
  - lib/mini_defender/rule.rb
88
89
  - lib/mini_defender/rules.rb
89
90
  - lib/mini_defender/rules/accepted.rb
@@ -177,6 +178,7 @@ files:
177
178
  - lib/mini_defender/rules_factory.rb
178
179
  - lib/mini_defender/validates_input.rb
179
180
  - lib/mini_defender/validation_error.rb
181
+ - lib/mini_defender/validation_helpers.rb
180
182
  - lib/mini_defender/validator.rb
181
183
  - lib/mini_defender/version.rb
182
184
  - mini_defender.gemspec