explicit-parameters 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/lib/explicit_parameters/controller.rb +12 -4
- data/lib/explicit_parameters/version.rb +1 -1
- data/spec/controller_spec.rb +10 -0
- data/spec/spec_helper.rb +0 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ec2817ab57ba4b8b63d011702245fa8114de7a63
|
4
|
+
data.tar.gz: fead56598d7502cf39395af308febc40f148cd0d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7840be0a953f0ede081f10539d918f2486ee899f86d3075e919a8db0212731f586cf49352b6f5423012938d96b28ba33fe0bf6140a98d2b5a873eef7e2b406d4
|
7
|
+
data.tar.gz: 2f8cfc543b901b3c64c6f41080cbe18f7b76cab75464e11f8741f48af55059eeba1edd451df2c0459eca385d1b6c9351c7e5fc11d2b2f83461f2889758f0d4c0
|
data/README.md
CHANGED
@@ -9,7 +9,6 @@ module ExplicitParameters
|
|
9
9
|
end
|
10
10
|
|
11
11
|
included do
|
12
|
-
self.parameters = {}
|
13
12
|
rescue_from ExplicitParameters::InvalidParameters, with: :render_parameters_error
|
14
13
|
end
|
15
14
|
|
@@ -18,6 +17,7 @@ module ExplicitParameters
|
|
18
17
|
|
19
18
|
def method_added(action)
|
20
19
|
return unless Controller.last_parameters
|
20
|
+
self.parameters ||= {}
|
21
21
|
parameters[action.to_s] = Controller.last_parameters
|
22
22
|
const_set("#{action.to_s.camelize}Parameters", Controller.last_parameters)
|
23
23
|
Controller.last_parameters = nil
|
@@ -28,7 +28,7 @@ module ExplicitParameters
|
|
28
28
|
end
|
29
29
|
|
30
30
|
def parse_parameters_for(action_name, params)
|
31
|
-
if declaration = parameters[action_name
|
31
|
+
if declaration = parameters.try!(:[], action_name)
|
32
32
|
declaration.parse!(params)
|
33
33
|
else
|
34
34
|
params
|
@@ -36,12 +36,20 @@ module ExplicitParameters
|
|
36
36
|
end
|
37
37
|
end
|
38
38
|
|
39
|
-
private
|
40
|
-
|
41
39
|
def params
|
42
40
|
@validated_params ||= self.class.parse_parameters_for(action_name, super)
|
43
41
|
end
|
44
42
|
|
43
|
+
private
|
44
|
+
|
45
|
+
def param_error!(parameter, error)
|
46
|
+
raise ExplicitParameters::InvalidParameters.new({errors: {parameter => [error]}}.to_json)
|
47
|
+
end
|
48
|
+
|
49
|
+
def render_param_error(parameter, error)
|
50
|
+
render json: {errors: {parameter => [error]}}, status: :unprocessable_entity
|
51
|
+
end
|
52
|
+
|
45
53
|
def render_parameters_error(error)
|
46
54
|
render json: error.message, status: :unprocessable_entity
|
47
55
|
end
|
data/spec/controller_spec.rb
CHANGED
@@ -11,6 +11,11 @@ class DummyController < ActionController::Base
|
|
11
11
|
render json: {value: params.page_size, type: params.page_size.class.name}
|
12
12
|
end
|
13
13
|
|
14
|
+
def error
|
15
|
+
param_error!(:page_size, "Out of bound")
|
16
|
+
render text: 'OK'
|
17
|
+
end
|
18
|
+
|
14
19
|
def no_declaration
|
15
20
|
render text: 'OK'
|
16
21
|
end
|
@@ -38,6 +43,11 @@ RSpec.describe DummyController do
|
|
38
43
|
expect(json_response).to be == {errors: {page_size: ['"foobar" is not a valid Integer']}}
|
39
44
|
end
|
40
45
|
|
46
|
+
it 'param_error! interupt the request immediately' do
|
47
|
+
get :error
|
48
|
+
expect(json_response).to be == {errors: {page_size: ['Out of bound']}}
|
49
|
+
end
|
50
|
+
|
41
51
|
private
|
42
52
|
|
43
53
|
attr_reader :response
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: explicit-parameters
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jean Boussier
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-03-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: actionpack
|