action_parameter 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/Gemfile +2 -4
- data/README.md +48 -8
- data/action_parameter.gemspec +4 -2
- data/lib/action_parameter/helpers.rb +3 -4
- data/lib/action_parameter/version.rb +1 -1
- data/test/action_parameter_test.rb +38 -6
- metadata +17 -16
- data/README.rdoc +0 -164
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6ca9c847488299c399725c330b52fdb3a7c42b86
|
4
|
+
data.tar.gz: 2acb6e8b30dd04005179ffbd9183a2a0f2e97460
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1abbdd2cdb36f50236e0ba283965d17e7914ab32a2c270b57f3976db3b99f8e1d5371e0d1bd01a96a31aabc92dff651a3f4e22eb4ce6b8c530461d80880d24ed
|
7
|
+
data.tar.gz: 9cb63658d70cadbf52579739dddf864f6e69b935a8a05f0f8ab1586a85eafcaedcc98da40ce79664ef61d4597d003d684a5729d6b7682677a02395cf49a21f89
|
data/CHANGELOG.md
CHANGED
data/Gemfile
CHANGED
@@ -2,8 +2,6 @@ source 'https://rubygems.org'
|
|
2
2
|
|
3
3
|
gemspec
|
4
4
|
|
5
|
-
gem '
|
6
|
-
gem 'activesupport', '~> 3.0.0'
|
7
|
-
gem 'mocha', '~> 0.13.2', require: false
|
5
|
+
gem 'mocha', '~> 0.13.2', require: false
|
8
6
|
gem 'rake'
|
9
|
-
gem 'coveralls',
|
7
|
+
gem 'coveralls', require: false
|
data/README.md
CHANGED
@@ -2,6 +2,7 @@
|
|
2
2
|
[](https://travis-ci.org/edelpero/action_parameter)
|
3
3
|
[](https://coveralls.io/r/edelpero/action_parameter)
|
4
4
|
[](https://codeclimate.com/github/edelpero/action_parameter)
|
5
|
+
[](https://bitdeli.com/free "Bitdeli Badge")
|
5
6
|
|
6
7
|
ActionParameter
|
7
8
|
===============
|
@@ -40,7 +41,7 @@ end
|
|
40
41
|
|
41
42
|
```ruby
|
42
43
|
# app/parameters/user_parameters.rb
|
43
|
-
class
|
44
|
+
class UserParameters < ActionParameter::Base
|
44
45
|
def permit
|
45
46
|
params.require(:user).permit(:name, :age)
|
46
47
|
end
|
@@ -131,27 +132,27 @@ end
|
|
131
132
|
|
132
133
|
#####Creating New Helpers
|
133
134
|
|
134
|
-
If you want to create new helper methods for
|
135
|
+
If you want to create new helper methods for your parameters class, just call **locals** method over **permitted_params**. Let say you want to make **@current_user** available for the UserParameter's class under the **user** method, then you'll need to use the **locals** method to tell the UserParameters class to create a new helper that returns **@current_user**.
|
135
136
|
|
136
137
|
```ruby
|
137
|
-
permitted_params(class: :user).locals(
|
138
|
+
permitted_params(class: :user).locals( user: @current_user,
|
138
139
|
another_helper: @value )
|
139
140
|
```
|
140
|
-
This will create **
|
141
|
+
This will create **user** and **another_helper** methods and they will be available in UserParameters class.
|
141
142
|
|
142
143
|
#####Example
|
143
144
|
|
144
145
|
```ruby
|
145
|
-
# app/controllers/
|
146
|
+
# app/controllers/users_controllers.rb
|
146
147
|
class UsersController < ActionController::Base
|
147
148
|
def create
|
148
|
-
User.create(permitted_params.locals(
|
149
|
+
User.create(permitted_params.locals(user: @current_user).permit) # This will call to UserParameters' permit method
|
149
150
|
end
|
150
151
|
end
|
151
152
|
```
|
152
153
|
|
153
154
|
```ruby
|
154
|
-
# app/parameters/
|
155
|
+
# app/parameters/user_parameters.rb
|
155
156
|
class UserParameters < ActionParameter::Base
|
156
157
|
def permit
|
157
158
|
if user.admin?
|
@@ -161,4 +162,43 @@ class UserParameters < ActionParameter::Base
|
|
161
162
|
end
|
162
163
|
end
|
163
164
|
end
|
164
|
-
```
|
165
|
+
```
|
166
|
+
|
167
|
+
RSpec
|
168
|
+
-----
|
169
|
+
|
170
|
+
This example shows how to test using RSpec.
|
171
|
+
|
172
|
+
Theses tests require your **test.rb** configured to **config.action_controller.action_on_unpermitted_parameters = :raise**.
|
173
|
+
|
174
|
+
```ruby
|
175
|
+
# spec/parameters/user_parameters_spec.rb
|
176
|
+
require "spec_helper"
|
177
|
+
|
178
|
+
describe UserParameters do
|
179
|
+
describe ".permit" do
|
180
|
+
describe "when permitted parameters" do
|
181
|
+
it "returns the cleaned parameters" do
|
182
|
+
user_params = { first_name: "John", last_name: "Doe" }
|
183
|
+
params = ActionController::Parameters.new(user: user_params)
|
184
|
+
|
185
|
+
permitted_params = UserParameters.new(params).permit
|
186
|
+
|
187
|
+
expect(permitted_params).to eq user_params.with_indifferent_access
|
188
|
+
end
|
189
|
+
end
|
190
|
+
|
191
|
+
describe "when unpermitted parameters" do
|
192
|
+
it "raises error" do
|
193
|
+
user_params = { foo: "bar" }
|
194
|
+
params = ActionController::Parameters.new(user: user_params)
|
195
|
+
|
196
|
+
expect{ UserParameters.new(params).permit }.
|
197
|
+
to raise_error(ActionController::UnpermittedParameters)
|
198
|
+
end
|
199
|
+
end
|
200
|
+
end
|
201
|
+
end
|
202
|
+
|
203
|
+
```
|
204
|
+
|
data/action_parameter.gemspec
CHANGED
@@ -17,6 +17,8 @@ Gem::Specification.new do |s|
|
|
17
17
|
s.test_files = `git ls-files -- test/*`.split("\n")
|
18
18
|
s.require_paths = ["lib"]
|
19
19
|
|
20
|
-
s.
|
21
|
-
|
20
|
+
s.add_development_dependency('minitest', '~> 4.2')
|
21
|
+
|
22
|
+
s.add_dependency('activesupport', '>= 3.0.0')
|
23
|
+
s.add_dependency('actionpack', '>= 3.0.0')
|
22
24
|
end
|
@@ -34,14 +34,13 @@ module ActionParameter
|
|
34
34
|
# # => CustomerParameters
|
35
35
|
#
|
36
36
|
# permitted_params_class(:customers) # called from PeopleController
|
37
|
-
# # =>
|
37
|
+
# # => CustomersParameters
|
38
38
|
#
|
39
39
|
# permitted_params_class() # called from PeopleController
|
40
40
|
# # => PersonParameters
|
41
41
|
def permitted_params_class(class_name = nil)
|
42
|
-
class_name
|
43
|
-
|
44
|
-
@permitted_params_class ||= "#{formatted_class_name}Parameters".constantize
|
42
|
+
class_name = class_name || self.class.name.sub(/Controller$/, '').singularize
|
43
|
+
@permitted_params_class ||= "#{class_name.to_s.camelcase}Parameters".constantize
|
45
44
|
end
|
46
45
|
|
47
46
|
end
|
@@ -1,8 +1,13 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
|
-
|
4
|
-
|
3
|
+
UserParameters = Class.new(ActionParameter::Base)
|
4
|
+
CustomerParameters = Class.new(ActionParameter::Base)
|
5
|
+
|
6
|
+
Admin = Module.new
|
7
|
+
Admin::UserParameters = Class.new(ActionParameter::Base)
|
8
|
+
Admin::CustomerParameters = Class.new(ActionParameter::Base)
|
5
9
|
|
10
|
+
module SharedActionMethods
|
6
11
|
def permitted_params_without_arguments
|
7
12
|
permitted_params
|
8
13
|
render nothing: true
|
@@ -13,17 +18,25 @@ class UsersController < ApplicationController
|
|
13
18
|
render nothing: true
|
14
19
|
end
|
15
20
|
|
21
|
+
def permitted_params_with_namespaced_class_attribute
|
22
|
+
permitted_params(class: 'admin/customer')
|
23
|
+
render nothing: true
|
24
|
+
end
|
25
|
+
|
16
26
|
def permitted_params_with_locals
|
17
27
|
permitted_params.locals(current_user: 'user')
|
18
28
|
render nothing: true
|
19
29
|
end
|
20
|
-
|
21
30
|
end
|
22
31
|
|
23
|
-
class
|
32
|
+
class Admin::UsersController < ApplicationController
|
33
|
+
include ActionParameter::Helpers
|
34
|
+
include SharedActionMethods
|
24
35
|
end
|
25
36
|
|
26
|
-
class
|
37
|
+
class UsersController < ApplicationController
|
38
|
+
include ActionParameter::Helpers
|
39
|
+
include SharedActionMethods
|
27
40
|
end
|
28
41
|
|
29
42
|
class ActionParameterTest < ActionController::TestCase
|
@@ -58,4 +71,23 @@ class ActionParameterTest < ActionController::TestCase
|
|
58
71
|
get :permitted_params_with_locals
|
59
72
|
assert_equal(true, assigns(:permitted_params).respond_to?(:current_user))
|
60
73
|
end
|
61
|
-
end
|
74
|
+
end
|
75
|
+
|
76
|
+
class ActionParameterNamespaceTest < ActionController::TestCase
|
77
|
+
tests Admin::UsersController
|
78
|
+
|
79
|
+
def test_permitted_params_without_arguments_is_an_instance_of_user_parameters
|
80
|
+
get :permitted_params_without_arguments
|
81
|
+
assert_equal('Admin::UserParameters', assigns(:permitted_params).class.to_s)
|
82
|
+
end
|
83
|
+
|
84
|
+
def test_permitted_params_with_class_attribute_returns_an_instance_of_customer_parameters
|
85
|
+
get :permitted_params_with_class_attribute
|
86
|
+
assert_equal('CustomerParameters', assigns(:permitted_params).class.to_s)
|
87
|
+
end
|
88
|
+
|
89
|
+
def test_permitted_params_with_namespaced_class_attribute_returns_an_instance_of_customer_parameters
|
90
|
+
get :permitted_params_with_namespaced_class_attribute
|
91
|
+
assert_equal('Admin::CustomerParameters', assigns(:permitted_params).class.to_s)
|
92
|
+
end
|
93
|
+
end
|
metadata
CHANGED
@@ -1,15 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: action_parameter
|
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
|
- Ezequiel Delpero
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2014-05-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: minitest
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.2'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4.2'
|
13
27
|
- !ruby/object:Gem::Dependency
|
14
28
|
name: activesupport
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -17,9 +31,6 @@ dependencies:
|
|
17
31
|
- - '>='
|
18
32
|
- !ruby/object:Gem::Version
|
19
33
|
version: 3.0.0
|
20
|
-
- - <
|
21
|
-
- !ruby/object:Gem::Version
|
22
|
-
version: '4.1'
|
23
34
|
type: :runtime
|
24
35
|
prerelease: false
|
25
36
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -27,9 +38,6 @@ dependencies:
|
|
27
38
|
- - '>='
|
28
39
|
- !ruby/object:Gem::Version
|
29
40
|
version: 3.0.0
|
30
|
-
- - <
|
31
|
-
- !ruby/object:Gem::Version
|
32
|
-
version: '4.1'
|
33
41
|
- !ruby/object:Gem::Dependency
|
34
42
|
name: actionpack
|
35
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -37,9 +45,6 @@ dependencies:
|
|
37
45
|
- - '>='
|
38
46
|
- !ruby/object:Gem::Version
|
39
47
|
version: 3.0.0
|
40
|
-
- - <
|
41
|
-
- !ruby/object:Gem::Version
|
42
|
-
version: '4.1'
|
43
48
|
type: :runtime
|
44
49
|
prerelease: false
|
45
50
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -47,9 +52,6 @@ dependencies:
|
|
47
52
|
- - '>='
|
48
53
|
- !ruby/object:Gem::Version
|
49
54
|
version: 3.0.0
|
50
|
-
- - <
|
51
|
-
- !ruby/object:Gem::Version
|
52
|
-
version: '4.1'
|
53
55
|
description: ActionParameter helps you move all your parameter's logic into it's own
|
54
56
|
class. This way you'll keep your controllers dry and they would be easier to test.
|
55
57
|
email: edelpero@gmail.com
|
@@ -64,7 +66,6 @@ files:
|
|
64
66
|
- Gemfile
|
65
67
|
- MIT-LICENSE
|
66
68
|
- README.md
|
67
|
-
- README.rdoc
|
68
69
|
- Rakefile
|
69
70
|
- action_parameter.gemspec
|
70
71
|
- lib/action_parameter.rb
|
@@ -97,7 +98,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
97
98
|
version: '0'
|
98
99
|
requirements: []
|
99
100
|
rubyforge_project:
|
100
|
-
rubygems_version: 2.
|
101
|
+
rubygems_version: 2.0.3
|
101
102
|
signing_key:
|
102
103
|
specification_version: 4
|
103
104
|
summary: Single Responsability Principle for Rails Controller's Parameters.
|
data/README.rdoc
DELETED
@@ -1,164 +0,0 @@
|
|
1
|
-
{<img src="https://badge.fury.io/rb/action_parameter.png" alt="Gem Version" />}[http://badge.fury.io/rb/action_parameter]
|
2
|
-
{<img src="https://travis-ci.org/edelpero/action_parameter.png?branch=master" alt="Build Status" />}[https://travis-ci.org/edelpero/action_parameter]
|
3
|
-
{<img src="https://coveralls.io/repos/edelpero/action_parameter/badge.png" alt="Coverage Status" />}[https://coveralls.io/r/edelpero/action_parameter]
|
4
|
-
{<img src="https://codeclimate.com/github/edelpero/action_parameter.png" />}[https://codeclimate.com/github/edelpero/action_parameter]
|
5
|
-
|
6
|
-
ActionParameter
|
7
|
-
===============
|
8
|
-
|
9
|
-
ActionParameter helps you move all your parameter's logic into it's own class. This way you'll keep your controllers dry and they would be easier to test.
|
10
|
-
|
11
|
-
Before
|
12
|
-
------
|
13
|
-
|
14
|
-
```ruby
|
15
|
-
# app/controllers/users_controllers.rb
|
16
|
-
class UsersController < ActionController::Base
|
17
|
-
def create
|
18
|
-
User.create(user_params)
|
19
|
-
end
|
20
|
-
|
21
|
-
private
|
22
|
-
def user_params
|
23
|
-
params.require(:user).permit(:name, :age)
|
24
|
-
end
|
25
|
-
end
|
26
|
-
```
|
27
|
-
|
28
|
-
After
|
29
|
-
-----
|
30
|
-
|
31
|
-
```ruby
|
32
|
-
# app/controllers/users_controllers.rb
|
33
|
-
class UsersController < ActionController::Base
|
34
|
-
def create
|
35
|
-
# It automatically deduces which Parameters class from Controller's name
|
36
|
-
User.create(permitted_params.permit)
|
37
|
-
end
|
38
|
-
end
|
39
|
-
```
|
40
|
-
|
41
|
-
```ruby
|
42
|
-
# app/parameters/user_parameters.rb
|
43
|
-
class UserParameter < ActionParameter::Base
|
44
|
-
def permit
|
45
|
-
params.require(:user).permit(:name, :age)
|
46
|
-
end
|
47
|
-
end
|
48
|
-
```
|
49
|
-
|
50
|
-
Install
|
51
|
-
-------
|
52
|
-
|
53
|
-
ActionParameter works with Rails 3.0 onwards and Ruby 1.9.3 onwards. You can add it to your Gemfile with:
|
54
|
-
|
55
|
-
```ruby
|
56
|
-
gem 'action_parameter'
|
57
|
-
```
|
58
|
-
|
59
|
-
Run the bundle command to install it.
|
60
|
-
|
61
|
-
Usage
|
62
|
-
-----
|
63
|
-
|
64
|
-
####Generator
|
65
|
-
|
66
|
-
```ruby
|
67
|
-
rails generate parameters [MODEL_NAME]
|
68
|
-
```
|
69
|
-
Will create **app/parameters/[model_name]_parameters.rb**.
|
70
|
-
|
71
|
-
####Controller Helpers
|
72
|
-
|
73
|
-
- **permitted_params:** Returns an ActionParameter instance.
|
74
|
-
|
75
|
-
```ruby
|
76
|
-
permitted_params(options={})
|
77
|
-
```
|
78
|
-
|
79
|
-
#####Options Hash
|
80
|
-
|
81
|
-
* **options** - Hash with one valid key: **:class**.
|
82
|
-
* **options[:class]** - Symbol value with the name of the Parameters class you want to use.
|
83
|
-
|
84
|
-
#####Example 1
|
85
|
-
|
86
|
-
```ruby
|
87
|
-
# app/controllers/people_controllers.rb
|
88
|
-
class PeopleController < ActionController::Base
|
89
|
-
def create
|
90
|
-
Person.create(permitted_params.permit) # This will call to PersonParameters' permit method
|
91
|
-
end
|
92
|
-
end
|
93
|
-
```
|
94
|
-
|
95
|
-
```ruby
|
96
|
-
# app/parameters/person_parameters.rb
|
97
|
-
class PersonParameters < ActionParameter::Base
|
98
|
-
def permit
|
99
|
-
params.require(:person).permit(:name, :age)
|
100
|
-
end
|
101
|
-
end
|
102
|
-
```
|
103
|
-
|
104
|
-
#####Example 2
|
105
|
-
|
106
|
-
```ruby
|
107
|
-
# app/controllers/people_controllers.rb
|
108
|
-
class PeopleController < ActionController::Base
|
109
|
-
def create
|
110
|
-
Person.create(permitted_params(class: :user).sign_up) # This will call to UserParameters' sign_up method
|
111
|
-
end
|
112
|
-
end
|
113
|
-
```
|
114
|
-
|
115
|
-
```ruby
|
116
|
-
# app/parameters/user_parameters.rb
|
117
|
-
class UserParameters < ActionParameter::Base
|
118
|
-
def sign_up
|
119
|
-
params.require(:person).permit(:name, :age)
|
120
|
-
end
|
121
|
-
end
|
122
|
-
```
|
123
|
-
|
124
|
-
####Parameter Class Helpers
|
125
|
-
|
126
|
-
#####Default Helpers
|
127
|
-
|
128
|
-
- **params:** Returns params from the current controller request which instantiated the Parameter class.
|
129
|
-
- **controller_name:** Returns the controller's name from which the Parameter class was instantiated.
|
130
|
-
- **action_name:** Returns the action's name from the controller from which the Parameter class was instantiated.
|
131
|
-
|
132
|
-
#####Creating New Helpers
|
133
|
-
|
134
|
-
If you want to create new helper methods for you parameters class, just call **:locals** over **permitted_params**. Let say you want to make **@current_user** available for the UserParameter's class, then you'll need to use the **:locals** method to tell the UserParameters class to create a new helper that returns **@current_user**.
|
135
|
-
|
136
|
-
```ruby
|
137
|
-
permitted_params(class: :user).locals( current_user: @current_user,
|
138
|
-
another_helper: @value )
|
139
|
-
```
|
140
|
-
This will create **current_user** and **another_helper** methods and they will be available in UserParameters class.
|
141
|
-
|
142
|
-
#####Example
|
143
|
-
|
144
|
-
```ruby
|
145
|
-
# app/controllers/people_controllers.rb
|
146
|
-
class UsersController < ActionController::Base
|
147
|
-
def create
|
148
|
-
User.create(permitted_params.locals(current_user: @current_user).permit) # This will call to PersonParameters' permit method
|
149
|
-
end
|
150
|
-
end
|
151
|
-
```
|
152
|
-
|
153
|
-
```ruby
|
154
|
-
# app/parameters/person_parameters.rb
|
155
|
-
class UserParameters < ActionParameter::Base
|
156
|
-
def permit
|
157
|
-
if user.admin?
|
158
|
-
params.require(:person).permit(:name, :age, :admin)
|
159
|
-
else
|
160
|
-
params.require(:person).permit(:name, :age)
|
161
|
-
end
|
162
|
-
end
|
163
|
-
end
|
164
|
-
```
|