mini_defender 0.3.8 → 0.3.10

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: 3723a27bf45fd1c1af3c404e3fee152cc744089dd19861c1e01ebcb9e3b6a9f4
4
- data.tar.gz: 354e0d09d03e2d67dc3b862acd3766594f6a4df0162673b6fcf036ade8d74ddf
3
+ metadata.gz: 3d86988a2bdf736a616cdae14ff90b0a1f094846a1a248b6602ccc5a8dc30148
4
+ data.tar.gz: 27e7e95fc4c4f1d2f9aa2d6406e2f057a3b501e7dfa2f0de006cecea1bba2194
5
5
  SHA512:
6
- metadata.gz: ed290224f41f28a65234ac12a0f990da65f8e25c8bb1c94d4703f6da02f0f7e66864ee133ea17e2fac97316b5a347214130feff42ce2d372756922fa73f3704f
7
- data.tar.gz: 568d3d3601ac8eacfaaa538a52790654c294dc4cb763ff5e9f310ffd235def326ac7722dbc8dcca8d3a97b4f788704625dac03da9ec832a9ea2e7166b050679a
6
+ metadata.gz: 88bb1908e129dabc940595220e0c2f5ea7244cb08a5904f1fae9380a6c0cc0ff62465c83666dcffbaf9f83e8d0268db1223ea0e89fe9d20f0786f9c3f3f354e9
7
+ data.tar.gz: 944893f26ae2e989951bdba092d4af6a5d5dcceeff89a0fdc61f6216f7dcff23cc5ff36c25b391a1937b73425554f13eeed0cfa4148fe3be3ad8427aa2725d5d
@@ -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,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/core_ext/hash'
4
+
5
+ module ValidationHelpers
6
+ extend ActiveSupport::Concern
7
+
8
+ def validation_errors
9
+ @validation_errors ||= flash[:validation_errors] || {}
10
+ end
11
+
12
+ def has_validation_errors?
13
+ validation_errors.length > 0
14
+ end
15
+
16
+ def old_values
17
+ @old_values ||= flash[:old_values] || {}
18
+ end
19
+
20
+ def old_value(field, default = nil)
21
+ # Transform Rails foo[bar] convention to foo.bar
22
+ field = field.gsub('[', '.').gsub(']', '')
23
+ field = field.split('.')
24
+
25
+ result = old_values.deep_stringify_keys
26
+
27
+ while (key = field.shift)
28
+ key = key.to_i if Array === result
29
+
30
+ if Hash === result[key] || Array === result[key] || field.empty?
31
+ result = result[key]
32
+ end
33
+ end
34
+
35
+ result || default
36
+ end
37
+
38
+ def field_errors(field)
39
+ raise ArgumentError, 'Expected a block.' unless block_given?
40
+
41
+ errors = validation_errors[field] || []
42
+ errors.each { |e| yield(e) }
43
+ end
44
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MiniDefender
4
- VERSION = "0.3.8"
4
+ VERSION = "0.3.10"
5
5
  end
data/lib/mini_defender.rb CHANGED
@@ -8,6 +8,8 @@ require_relative 'mini_defender/rules_expander'
8
8
  require_relative 'mini_defender/rules_factory'
9
9
  require_relative 'mini_defender/validation_error'
10
10
  require_relative 'mini_defender/validates_input'
11
+ require_relative 'mini_defender/handles_validation_errors'
12
+ require_relative 'mini_defender/validation_helpers'
11
13
 
12
14
  # Extensions to Ruby Core
13
15
  require_relative 'mini_defender/extensions/enumerable'
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.8
4
+ version: 0.3.10
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-09-10 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