mini_defender 0.3.8 → 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: 3723a27bf45fd1c1af3c404e3fee152cc744089dd19861c1e01ebcb9e3b6a9f4
4
- data.tar.gz: 354e0d09d03e2d67dc3b862acd3766594f6a4df0162673b6fcf036ade8d74ddf
3
+ metadata.gz: f577c7a22376c0e4217fdebe7ddf461cb302471e07b2de234e0843ef55c9c603
4
+ data.tar.gz: 6105884dc720a11d506c7c7fc62b24d1f8cd7afc9fa14ce4ed5b44c028e1620c
5
5
  SHA512:
6
- metadata.gz: ed290224f41f28a65234ac12a0f990da65f8e25c8bb1c94d4703f6da02f0f7e66864ee133ea17e2fac97316b5a347214130feff42ce2d372756922fa73f3704f
7
- data.tar.gz: 568d3d3601ac8eacfaaa538a52790654c294dc4cb763ff5e9f310ffd235def326ac7722dbc8dcca8d3a97b4f788704625dac03da9ec832a9ea2e7166b050679a
6
+ metadata.gz: ab08c1074c9c4b06f16c11e545b9f8b05811e4be8cc82c43a98315605e21553821d97b9688b1acdcd446b546a65d8d25af4f0e5b725dad09a189bfa51f74ea85
7
+ data.tar.gz: c325ff0ddfb62768fb5fdd74ca08bb8e97bd76f16841190178e9c924dddd0b4f6e3e010026bfefc417a5a6a845fbbf6c3db7b9a28414aa96a75649adac56f82c
@@ -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.8"
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.8
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-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