stark_parameters 0.0.3 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/stark_parameters/version.rb +1 -1
- data/lib/stark_parameters.rb +17 -6
- data/spec/lib/stark_parameters_spec.rb +28 -0
- metadata +2 -2
data/lib/stark_parameters.rb
CHANGED
@@ -6,8 +6,9 @@ require "action_controller/metal/strong_parameters"
|
|
6
6
|
module StarkParameters
|
7
7
|
def self.included(klass)
|
8
8
|
klass.send :extend, ClassMethods
|
9
|
-
klass.required_params = []
|
10
9
|
klass.permitted_params = []
|
10
|
+
klass.presence_required_params = []
|
11
|
+
klass.presence_optional_params = []
|
11
12
|
klass.aliases = {}
|
12
13
|
end
|
13
14
|
|
@@ -32,10 +33,15 @@ module StarkParameters
|
|
32
33
|
end
|
33
34
|
|
34
35
|
def required_params
|
35
|
-
self.class.
|
36
|
+
presence_required_params = self.class.presence_required_params.each_with_object({}) do |required_params, hash|
|
36
37
|
param_key = require_one(required_params)
|
37
38
|
hash[(self.class.aliases[param_key] || param_key).to_s] = @params[param_key]
|
38
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
|
+
presence_required_params.merge(presence_optional_params)
|
39
45
|
end
|
40
46
|
|
41
47
|
|
@@ -44,23 +50,28 @@ module StarkParameters
|
|
44
50
|
ActionController::Parameters.new(hash)
|
45
51
|
end
|
46
52
|
|
47
|
-
def require_one(to_require)
|
53
|
+
def require_one(to_require, allow_nil = false)
|
48
54
|
present_param = to_require.detect {|p| @params.include?(p) }
|
49
|
-
|
55
|
+
value_presence_valid = allow_nil ? true : !@params.stringify_keys[present_param.to_s].nil?
|
56
|
+
unless present_param && value_presence_valid
|
50
57
|
raise ActionController::ParameterMissing.new(to_require.join(" or "))
|
51
58
|
end
|
52
59
|
present_param
|
53
60
|
end
|
54
61
|
|
55
62
|
module ClassMethods
|
56
|
-
attr_accessor :
|
63
|
+
attr_accessor :permitted_params, :presence_required_params, :presence_optional_params, :aliases
|
57
64
|
|
58
65
|
def require(required_params, options = {})
|
59
66
|
required_params = Array(required_params)
|
60
67
|
if new_name = options[:as]
|
61
68
|
required_params.each { |rp| @aliases[rp] = new_name }
|
62
69
|
end
|
63
|
-
|
70
|
+
if options[:allow_nil]
|
71
|
+
@presence_optional_params.push required_params
|
72
|
+
else
|
73
|
+
@presence_required_params.push required_params
|
74
|
+
end
|
64
75
|
end
|
65
76
|
|
66
77
|
def permit(permitted_param, options = {})
|
@@ -45,6 +45,34 @@ describe StarkParameters do
|
|
45
45
|
it { expect{ validator.params }.to raise_error(ActionController::ParameterMissing) }
|
46
46
|
end
|
47
47
|
|
48
|
+
context "when required param is nil" do
|
49
|
+
let(:params) { full_params["name"] = nil; full_params }
|
50
|
+
|
51
|
+
it { expect{ validator.params }.to raise_error(ActionController::ParameterMissing) }
|
52
|
+
end
|
53
|
+
|
54
|
+
describe "required param presence: true" do
|
55
|
+
let(:test_klass) do
|
56
|
+
Class.new do
|
57
|
+
include StarkParameters
|
58
|
+
|
59
|
+
require :name, allow_nil: true
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
context "when param is missing" do
|
64
|
+
let(:params) { {} }
|
65
|
+
|
66
|
+
it { expect{ validator.params }.to raise_error(ActionController::ParameterMissing) }
|
67
|
+
end
|
68
|
+
|
69
|
+
context "when param is nil" do
|
70
|
+
let(:params) { {"name" => nil} }
|
71
|
+
|
72
|
+
it { expect(validator.params).to eql(params) }
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
48
76
|
context "when permitted param is missing" do
|
49
77
|
let(:params) { full_params.except("author") }
|
50
78
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: stark_parameters
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-03-
|
12
|
+
date: 2014-03-17 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: actionpack
|