auto_strong_parameters 0.0.1 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/auto_strong_parameters/auto_form_params.rb +2 -1
- data/lib/auto_strong_parameters/auto_permit.rb +3 -3
- data/lib/auto_strong_parameters/version.rb +1 -1
- data/lib/auto_strong_parameters.rb +21 -14
- data/test/apps/basic_controller.rb +1 -0
- data/test/apps/user.rb +1 -1
- data/test/auto_form_params_test.rb +1 -0
- data/test/auto_permit_test.rb +2 -2
- data/test/auto_strong_parameters_test.rb +41 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8d888930c9a1649b83470cc64c9c3d4d9477c9d520c3ac262bb9741b72b63f92
|
4
|
+
data.tar.gz: 4dfb6bc178a3b498c35288cf79f75c944e42ffe55a72a8f2603eb7d9f420d212
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b7f3f4a17050f13d2310d0ac4909e6b570223f96bc3147860f389a464c454eb1738557f53d2b7d303b41c6071e28e5e294f21aab9b7b38ae8a750c1f2a600cac
|
7
|
+
data.tar.gz: 5fb38548e76beb1e9e88ac03b0cdcba3b5b819db598f54f08bcaca3665588c22fecff1fcb9450dd3ad346acd5032f1c5b6eb05669fac916d1600b344aad9a695
|
@@ -13,7 +13,8 @@ module AutoStrongParameters::AutoFormParams
|
|
13
13
|
TRACKED_FIELDS = %w(
|
14
14
|
search_field telephone_field date_field time_field datetime_field
|
15
15
|
month_field week_field url_field email_field number_field range_field
|
16
|
-
file_field password_field text_area text_field radio_button
|
16
|
+
file_field password_field text_area text_field radio_button phone_field
|
17
|
+
select
|
17
18
|
)
|
18
19
|
|
19
20
|
TRACKED_FIELDS.each do |name|
|
@@ -3,13 +3,13 @@
|
|
3
3
|
module AutoStrongParameters
|
4
4
|
module AutoPermit
|
5
5
|
def auto_permit!(key)
|
6
|
-
shape = asp_auto_permitted_params
|
6
|
+
shape = asp_auto_permitted_params
|
7
7
|
|
8
8
|
require(key).permit(shape[key])
|
9
9
|
end
|
10
10
|
|
11
|
-
def asp_auto_permitted_params
|
12
|
-
if sig = self[
|
11
|
+
def asp_auto_permitted_params
|
12
|
+
if sig = self[AutoStrongParameters.asp_message_key]
|
13
13
|
AutoStrongParameters.verifier.verify(sig) rescue {}
|
14
14
|
else
|
15
15
|
{}
|
@@ -4,20 +4,6 @@ require 'rails'
|
|
4
4
|
require 'auto_strong_parameters/railtie'
|
5
5
|
|
6
6
|
module AutoStrongParameters
|
7
|
-
# Rails' message_verifier exists with a stable API in all versions of Rails
|
8
|
-
# since 4.2.
|
9
|
-
def self.verifier
|
10
|
-
@verifier ||=
|
11
|
-
ActiveSupport::MessageVerifier.new("auto_strong_parameters", serializer: JSON)
|
12
|
-
end
|
13
|
-
|
14
|
-
# Provide your own custom verifier for AutoStrongParameters. Must respond to
|
15
|
-
# #generate which takes an object and returns a string and #verify which
|
16
|
-
# takes a string and returns an object.
|
17
|
-
def self.verifier=(custom_verifier)
|
18
|
-
@verifier = custom_verifier
|
19
|
-
end
|
20
|
-
|
21
7
|
def self.asp_message_key
|
22
8
|
@asp_message_key ||= :"_asp_params"
|
23
9
|
end
|
@@ -26,6 +12,14 @@ module AutoStrongParameters
|
|
26
12
|
@asp_message_key = val
|
27
13
|
end
|
28
14
|
|
15
|
+
def self.secret=(secret)
|
16
|
+
@secret = secret
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.secret
|
20
|
+
@secret ||= Rails.application.config.secret_key_base
|
21
|
+
end
|
22
|
+
|
29
23
|
def self.to_strong_params_shape(obj)
|
30
24
|
items = Set.new
|
31
25
|
hsh = {}
|
@@ -76,4 +70,17 @@ module AutoStrongParameters
|
|
76
70
|
nil
|
77
71
|
end
|
78
72
|
end
|
73
|
+
|
74
|
+
# Rails' message_verifier exists with a stable API in all versions of Rails
|
75
|
+
# since 4.2.
|
76
|
+
def self.verifier
|
77
|
+
@verifier ||= ActiveSupport::MessageVerifier.new(secret, serializer: JSON)
|
78
|
+
end
|
79
|
+
|
80
|
+
# Provide your own custom verifier for AutoStrongParameters. Must respond to
|
81
|
+
# #generate which takes an object and returns a string and #verify which
|
82
|
+
# takes a string and returns an object.
|
83
|
+
def self.verifier=(custom_verifier)
|
84
|
+
@verifier = custom_verifier
|
85
|
+
end
|
79
86
|
end
|
@@ -17,6 +17,7 @@ class BasicController < ActionController::Base
|
|
17
17
|
<%= f.number_field :age %>
|
18
18
|
<%= f.range_field :years_of_experience %>
|
19
19
|
<%= f.password_field :password %>
|
20
|
+
<%= f.select :location, [['Home',1], ['Work', 2]] %>
|
20
21
|
<%= f.radio_button :preferred_phone_os, :iphone %>
|
21
22
|
<%= f.radio_button :preferred_phone_os, :android %>
|
22
23
|
<%= f.fields_for :parents do |parf| %>
|
data/test/apps/user.rb
CHANGED
@@ -3,7 +3,7 @@ class User
|
|
3
3
|
|
4
4
|
attr_accessor :name, :email, :description, :phone, :dob, :lunch_time,
|
5
5
|
:confirmed_at, :birth_month, :birthday_week, :favorite_url, :age,
|
6
|
-
:years_of_experience, :password, :preferred_phone_os
|
6
|
+
:years_of_experience, :password, :preferred_phone_os, :location
|
7
7
|
|
8
8
|
def parents
|
9
9
|
@parents ||= [Parent.new]
|
data/test/auto_permit_test.rb
CHANGED
@@ -50,7 +50,7 @@ class AutoPermitTest < ActionController::TestCase
|
|
50
50
|
end
|
51
51
|
|
52
52
|
def test_auto_permit
|
53
|
-
post :auto_permit, **process_args(user: user_params
|
53
|
+
post :auto_permit, **process_args(user: user_params, message_key => signature)
|
54
54
|
assert_response :ok
|
55
55
|
j = ActiveSupport::JSON.decode(response.body)
|
56
56
|
|
@@ -61,7 +61,7 @@ class AutoPermitTest < ActionController::TestCase
|
|
61
61
|
end
|
62
62
|
|
63
63
|
def test_auto_permit_incorrect_signature
|
64
|
-
post :auto_permit, **process_args(user: user_params
|
64
|
+
post :auto_permit, **process_args(user: user_params, message_key => 'abc123')
|
65
65
|
assert_response :ok
|
66
66
|
j = ActiveSupport::JSON.decode(response.body)
|
67
67
|
|
@@ -13,4 +13,45 @@ class AutoStrongParametersTest < Minitest::Test
|
|
13
13
|
assert_equal ["name", {"pet" => ["name"]}], to_strong_params_shape({"name" => "Steve", "pet" => { "name" => "Fluffy" }})
|
14
14
|
assert_equal ["name", {"pet" => ["name"]}], to_strong_params_shape({"name" => "Steve", "pet" => { "name" => "Fluffy" }})
|
15
15
|
end
|
16
|
+
|
17
|
+
def test_nested_params
|
18
|
+
params = {
|
19
|
+
"user"=>{
|
20
|
+
"first_name"=>"Steve",
|
21
|
+
"last_name"=>"Sample",
|
22
|
+
"email"=>"steve@example.com",
|
23
|
+
"phone"=>"(123) 234-2345",
|
24
|
+
"work_location"=>"Chicago",
|
25
|
+
"birth_date"=>"1980-01-01",
|
26
|
+
},
|
27
|
+
"address"=>{
|
28
|
+
"id"=>"7",
|
29
|
+
"street_address"=>"123 Example St",
|
30
|
+
"city"=>"Chicago",
|
31
|
+
"state"=>"IL",
|
32
|
+
"zip"=>"12345"
|
33
|
+
},
|
34
|
+
"emergency_contact"=>{
|
35
|
+
"id"=>"16",
|
36
|
+
"first_name"=>"Mary",
|
37
|
+
"last_name"=>"Example",
|
38
|
+
"phone"=>"(123) 123-1234",
|
39
|
+
},
|
40
|
+
}
|
41
|
+
exp = {
|
42
|
+
"user"=>[
|
43
|
+
"first_name", "last_name", "email", "phone", "work_location", "birth_date"
|
44
|
+
],
|
45
|
+
"address"=>[
|
46
|
+
"id", "street_address", "city", "state", "zip"
|
47
|
+
],
|
48
|
+
"emergency_contact"=>[
|
49
|
+
"id", "first_name", "last_name", "phone"
|
50
|
+
],
|
51
|
+
}
|
52
|
+
actual = to_strong_params_shape(params)
|
53
|
+
assert_equal exp["user"], actual["user"]
|
54
|
+
assert_equal exp["address"], actual["address"]
|
55
|
+
assert_equal exp["emergency_contact"], actual["emergency_contact"]
|
56
|
+
end
|
16
57
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: auto_strong_parameters
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Drew Ulmer
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-08-
|
11
|
+
date: 2023-08-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|