stark_parameters 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/lib/stark_parameters/version.rb +1 -1
- data/lib/stark_parameters.rb +34 -28
- data/spec/lib/stark_parameters_spec.rb +43 -9
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
NzM2YTU1MzI1YzAyNzZhYzVlOWVhMThiN2NlNGVkOGNiNTdmMjgyMQ==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
NWQ0MzljZGIxYTMzMzBlNTI2ZjY3YWIwM2Y3NjY2NWIzN2JjY2JjNg==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
M2Y1NmZiZjgyZjQyNjZlZDJiNDIxZTU0ODk5MzFhY2EwNDRlMDc5NzNiOTU0
|
10
|
+
NmJhNjdjZTlmYmE0MDNjOTM2ZWZiNWQyNDNjNGU4M2U3ZmI4MDUwZGQwYjA4
|
11
|
+
ZmMzMmNiM2Y2N2M2YjYyMDFiOWNlMDM2ZDYwZjEwMWUxMDZmNjM=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
ZjcwNzRkNGUwNTAwNjdhMTFhMzI3ODUwYzhmYTJjY2QxNjYxYTRlNmQxYTY3
|
14
|
+
ZGQ1MDk2MmVhYmE4ZGQwM2QyZWJmNWZlYzUwOThiYzYxNDZiMjE0MzUwZTcz
|
15
|
+
YTFlYjhiOTI5ZmZkN2EzOTlhNWJmNjFkOGQ3ODU1ZjljNDEwN2M=
|
data/lib/stark_parameters.rb
CHANGED
@@ -12,56 +12,67 @@ module StarkParameters
|
|
12
12
|
klass.aliases = {}
|
13
13
|
end
|
14
14
|
|
15
|
+
attr_reader :params
|
16
|
+
|
15
17
|
def initialize(*params)
|
16
|
-
@params = params.
|
17
|
-
hash.merge
|
18
|
+
@params = params.reduce(make_strong_parameter({})) do |hash, param|
|
19
|
+
hash.merge(make_strong_parameter(param))
|
18
20
|
end
|
19
21
|
end
|
20
22
|
|
21
23
|
def params
|
22
|
-
|
24
|
+
all_params = permitted_params.merge(required_params)
|
25
|
+
make_strong_parameter(all_params).permit!
|
23
26
|
end
|
24
27
|
|
25
28
|
private
|
26
29
|
|
27
30
|
def permitted_params
|
28
31
|
self.class.permitted_params.each_with_object({}) do |permitted_param, hash|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
+
permitted_key = permitted_param.is_a?(Hash) ? permitted_param.keys.first : permitted_param
|
33
|
+
name = self.class.aliases[permitted_key] || permitted_key
|
34
|
+
permitted_value = respond_to?(name) ? send(name) : @params.permit(permitted_param)[name]
|
35
|
+
hash[name] = permitted_value unless permitted_value.nil?
|
32
36
|
end
|
33
37
|
end
|
34
38
|
|
35
39
|
def required_params
|
36
|
-
presence_required_params = self.class.presence_required_params.each_with_object({}) do |required_params, hash|
|
37
|
-
param_key = require_one(required_params)
|
38
|
-
hash[(self.class.aliases[param_key] || param_key).to_s] = @params[param_key]
|
39
|
-
end
|
40
|
-
presence_optional_params = self.class.presence_optional_params.each_with_object({}) do |required_params, hash|
|
41
|
-
param_key = require_one(required_params, true)
|
42
|
-
hash[(self.class.aliases[param_key] || param_key).to_s] = @params[param_key]
|
43
|
-
end
|
44
40
|
presence_required_params.merge(presence_optional_params)
|
45
41
|
end
|
46
42
|
|
43
|
+
def presence_optional_params
|
44
|
+
collect_required_params(self.class.presence_optional_params, true)
|
45
|
+
end
|
46
|
+
|
47
|
+
def presence_required_params
|
48
|
+
collect_required_params(self.class.presence_required_params, false)
|
49
|
+
end
|
50
|
+
|
51
|
+
def collect_required_params(keys, allow_nil)
|
52
|
+
keys.each_with_object({}) do |required_params, hash|
|
53
|
+
required_key = require_one(required_params, allow_nil)
|
54
|
+
|
55
|
+
key = self.class.aliases[required_key] || required_key
|
56
|
+
value = respond_to?(key) ? send(key) : @params[required_key]
|
57
|
+
hash[key] = value
|
58
|
+
end
|
59
|
+
end
|
47
60
|
|
48
61
|
def make_strong_parameter(hash)
|
49
62
|
return hash if hash.is_a? ActionController::Parameters
|
50
63
|
ActionController::Parameters.new(hash)
|
51
64
|
end
|
52
65
|
|
53
|
-
def require_one(to_require, allow_nil
|
54
|
-
present_param = to_require.detect
|
55
|
-
|
66
|
+
def require_one(to_require, allow_nil)
|
67
|
+
present_param = to_require.detect{|k| respond_to?(k)} ||
|
68
|
+
to_require.detect {|k| @params.include?(k) && (allow_nil || @params[k]) }
|
69
|
+
|
70
|
+
unless present_param
|
56
71
|
raise ActionController::ParameterMissing.new(to_require.join(" or "))
|
57
72
|
end
|
58
73
|
present_param
|
59
74
|
end
|
60
75
|
|
61
|
-
def value_presence_valid(present_param, allow_nil)
|
62
|
-
allow_nil ? true : !@params.stringify_keys[present_param.to_s].nil?
|
63
|
-
end
|
64
|
-
|
65
76
|
module ClassMethods
|
66
77
|
attr_accessor :permitted_params, :presence_required_params, :presence_optional_params, :aliases
|
67
78
|
|
@@ -78,14 +89,9 @@ module StarkParameters
|
|
78
89
|
end
|
79
90
|
|
80
91
|
def permit(permitted_param, options = {})
|
92
|
+
permitted_key = permitted_param.is_a?(Hash) ? permitted_param.keys.first : permitted_param
|
81
93
|
if new_name = options[:as]
|
82
|
-
|
83
|
-
permitted_param.keys.first
|
84
|
-
else
|
85
|
-
permitted_param
|
86
|
-
end
|
87
|
-
|
88
|
-
@aliases[param_key] = new_name
|
94
|
+
@aliases[permitted_key] = new_name
|
89
95
|
end
|
90
96
|
@permitted_params.push permitted_param
|
91
97
|
end
|
@@ -8,7 +8,6 @@ describe StarkParameters do
|
|
8
8
|
require :name
|
9
9
|
require :id
|
10
10
|
require :email, as: :login
|
11
|
-
permit author: [:id, :name]
|
12
11
|
permit :password, as: :pword
|
13
12
|
permit :awesome
|
14
13
|
require [:last_name, :surname]
|
@@ -21,8 +20,7 @@ describe StarkParameters do
|
|
21
20
|
"name" => "Ryan",
|
22
21
|
"email" => "ryan@6wunderkinder.com",
|
23
22
|
"password" => "fdsafdsa",
|
24
|
-
"surname" => "Levick"
|
25
|
-
"author" => { "id" => 1, "name" => "Steve" }
|
23
|
+
"surname" => "Levick"
|
26
24
|
}
|
27
25
|
end
|
28
26
|
|
@@ -33,7 +31,6 @@ describe StarkParameters do
|
|
33
31
|
|
34
32
|
it { expect(validator.params.to_hash).to include("name" => "Ryan") }
|
35
33
|
it { expect(validator.params.to_hash).to include("surname" => "Levick") }
|
36
|
-
it { expect(validator.params.to_hash).to include("author" => { "id" => 1, "name" => "Steve" }) }
|
37
34
|
it { expect(validator.params.to_hash).to include("login" => "ryan@6wunderkinder.com") }
|
38
35
|
it { expect(validator.params.to_hash.keys).to_not include("email") }
|
39
36
|
|
@@ -74,11 +71,31 @@ describe StarkParameters do
|
|
74
71
|
end
|
75
72
|
end
|
76
73
|
|
74
|
+
context "nested permitted params" do
|
75
|
+
let(:test_klass) do
|
76
|
+
Class.new do
|
77
|
+
include StarkParameters
|
78
|
+
|
79
|
+
permit author: [:name, :surname]
|
80
|
+
end
|
81
|
+
end
|
82
|
+
let(:params) {{"author" => {"name" => "Ryan", "surname" => "Levick", "age" => 25}} }
|
83
|
+
|
84
|
+
it { expect(validator.params.to_hash).to include("author" => {"name" => "Ryan", "surname" => "Levick"}) }
|
85
|
+
end
|
86
|
+
|
77
87
|
context "when permitted param is missing" do
|
78
|
-
let(:
|
88
|
+
let(:test_klass) do
|
89
|
+
Class.new do
|
90
|
+
include StarkParameters
|
79
91
|
|
80
|
-
|
81
|
-
|
92
|
+
permit :name
|
93
|
+
permit :age
|
94
|
+
end
|
95
|
+
end
|
96
|
+
let(:params) {{ "surname" => "Levick", "age" => 25 }}
|
97
|
+
|
98
|
+
it { expect(validator.params.to_hash).to include({"age" => 25}) }
|
82
99
|
end
|
83
100
|
|
84
101
|
context "when permitted param is false" do
|
@@ -89,8 +106,25 @@ describe StarkParameters do
|
|
89
106
|
end
|
90
107
|
|
91
108
|
context "with multiple params" do
|
92
|
-
let(:validator) { test_klass.new(full_params.except("name"), {"name" => "
|
109
|
+
let(:validator) { test_klass.new(full_params.except("name"), {"name" => "Steve"}) }
|
93
110
|
|
94
|
-
it { expect(validator.params.to_hash).to include("name" => "
|
111
|
+
it { expect(validator.params.to_hash).to include("name" => "Steve") }
|
112
|
+
end
|
113
|
+
|
114
|
+
context "with default values provided by methods" do
|
115
|
+
let(:test_klass) do
|
116
|
+
Class.new do
|
117
|
+
include StarkParameters
|
118
|
+
|
119
|
+
require :foo
|
120
|
+
|
121
|
+
def foo
|
122
|
+
"bar"
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
let(:validator) { test_klass.new({}) }
|
128
|
+
it { expect(validator.params.to_hash).to include("foo" => "bar") }
|
95
129
|
end
|
96
130
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: stark_parameters
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- rylev
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-06-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: actionpack
|