action_parameter 0.0.1 → 0.0.2
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/.coveralls.yml +2 -0
- data/.travis.yml +9 -0
- data/CHANGELOG.md +8 -0
- data/Gemfile +2 -1
- data/README.md +56 -17
- data/README.rdoc +56 -17
- data/lib/action_parameter/base.rb +28 -10
- data/lib/action_parameter/helpers.rb +11 -12
- data/lib/action_parameter/version.rb +1 -1
- data/lib/generators/{parameter → parameters}/USAGE +1 -1
- data/lib/generators/{parameter/parameter_generator.rb → parameters/parameters_generator.rb} +2 -2
- data/lib/generators/{parameter → parameters}/templates/parameter_class.rb +0 -0
- data/test/action_parameter_test.rb +1 -1
- data/test/test_helper.rb +3 -0
- metadata +8 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 20349d935998f29c2416ce507da7a788fe501219
|
4
|
+
data.tar.gz: d70fda5403bfee909f7f3af9fa19133d6b43b31d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 00ea0e919832434b7d2d3954ad0ada4a5d2f91a3270716a8db0d5d19f9eb9fdb4a4e4eb11aded1193a9fcc9bc22930fee2ea3a6ef5706b3152ce545b099bda5c
|
7
|
+
data.tar.gz: 93165ee51a9168c294503f7e76fdc96a0ddc196ac4dd4fa198fb176957849b13ce6a27c0227c76b4fbec458271917aa738e72b9ca6f7514ac375d0804583d126
|
data/.coveralls.yml
ADDED
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
### 0.0.2
|
2
|
+
|
3
|
+
* enhancements
|
4
|
+
* Adds `locals` method to permitted_params to create helpers methods on ActionParameter
|
5
|
+
|
6
|
+
* deprecations
|
7
|
+
* `permitted_params(locals: {})` in favor of `permitted_params.locals({})`
|
8
|
+
* `rails g parameter` rails' generator in favor of `rails g parameters`
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,3 +1,6 @@
|
|
1
|
+
[](http://badge.fury.io/rb/action_parameter)
|
2
|
+
[](https://travis-ci.org/edelpero/action_parameter)
|
3
|
+
[](https://coveralls.io/r/edelpero/action_parameter)
|
1
4
|
[](https://codeclimate.com/github/edelpero/action_parameter)
|
2
5
|
|
3
6
|
ActionParameter
|
@@ -5,10 +8,49 @@ ActionParameter
|
|
5
8
|
|
6
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.
|
7
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
|
+
|
8
50
|
Install
|
9
51
|
-------
|
10
52
|
|
11
|
-
ActionParameter works with Rails 3.0 onwards and Ruby 1.9. You can add it to your Gemfile with:
|
53
|
+
ActionParameter works with Rails 3.0 onwards and Ruby 1.9.3 onwards. You can add it to your Gemfile with:
|
12
54
|
|
13
55
|
```ruby
|
14
56
|
gem 'action_parameter'
|
@@ -22,7 +64,7 @@ Usage
|
|
22
64
|
####Generator
|
23
65
|
|
24
66
|
```ruby
|
25
|
-
rails generate
|
67
|
+
rails generate parameters [MODEL_NAME]
|
26
68
|
```
|
27
69
|
Will create **app/parameters/[model_name]_parameters.rb**.
|
28
70
|
|
@@ -36,9 +78,8 @@ permitted_params(options={})
|
|
36
78
|
|
37
79
|
#####Options Hash
|
38
80
|
|
39
|
-
* **options**
|
40
|
-
* **options[:class]**
|
41
|
-
* **options[:locals]** - Hash used to create helper methods available for the ActionParameter instance.
|
81
|
+
* **options** - Hash with one valid key: **:class**.
|
82
|
+
* **options[:class]** - Symbol value with the name of the Parameters class you want to use.
|
42
83
|
|
43
84
|
#####Example 1
|
44
85
|
|
@@ -80,7 +121,7 @@ class UserParameters < ActionParameter::Base
|
|
80
121
|
end
|
81
122
|
```
|
82
123
|
|
83
|
-
####Parameter Helpers
|
124
|
+
####Parameter Class Helpers
|
84
125
|
|
85
126
|
#####Default Helpers
|
86
127
|
|
@@ -90,11 +131,11 @@ end
|
|
90
131
|
|
91
132
|
#####Creating New Helpers
|
92
133
|
|
93
|
-
If you want to create new helper methods for you parameters class, just
|
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**.
|
94
135
|
|
95
136
|
```ruby
|
96
|
-
permitted_params(class: :user
|
97
|
-
|
137
|
+
permitted_params(class: :user).locals( current_user: @current_user,
|
138
|
+
another_helper: @value )
|
98
139
|
```
|
99
140
|
This will create **current_user** and **another_helper** methods and they will be available in UserParameters class.
|
100
141
|
|
@@ -104,21 +145,19 @@ This will create **current_user** and **another_helper** methods and they will b
|
|
104
145
|
# app/controllers/people_controllers.rb
|
105
146
|
class UsersController < ActionController::Base
|
106
147
|
def create
|
107
|
-
|
108
|
-
# This will call to UserParameters' sign_up method and will also create a
|
109
|
-
# helper method named 'current_user' which will return @current_user
|
148
|
+
User.create(permitted_params.locals(current_user: @current_user).permit) # This will call to PersonParameters' permit method
|
110
149
|
end
|
111
150
|
end
|
112
151
|
```
|
113
152
|
|
114
153
|
```ruby
|
115
|
-
# app/parameters/
|
154
|
+
# app/parameters/person_parameters.rb
|
116
155
|
class UserParameters < ActionParameter::Base
|
117
|
-
def
|
118
|
-
if
|
119
|
-
|
156
|
+
def permit
|
157
|
+
if user.admin?
|
158
|
+
params.require(:person).permit(:name, :age, :admin)
|
120
159
|
else
|
121
|
-
|
160
|
+
params.require(:person).permit(:name, :age)
|
122
161
|
end
|
123
162
|
end
|
124
163
|
end
|
data/README.rdoc
CHANGED
@@ -1,3 +1,6 @@
|
|
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]
|
1
4
|
{<img src="https://codeclimate.com/github/edelpero/action_parameter.png" />}[https://codeclimate.com/github/edelpero/action_parameter]
|
2
5
|
|
3
6
|
ActionParameter
|
@@ -5,10 +8,49 @@ ActionParameter
|
|
5
8
|
|
6
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.
|
7
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
|
+
|
8
50
|
Install
|
9
51
|
-------
|
10
52
|
|
11
|
-
ActionParameter works with Rails 3.0 onwards and Ruby 1.9. You can add it to your Gemfile with:
|
53
|
+
ActionParameter works with Rails 3.0 onwards and Ruby 1.9.3 onwards. You can add it to your Gemfile with:
|
12
54
|
|
13
55
|
```ruby
|
14
56
|
gem 'action_parameter'
|
@@ -22,7 +64,7 @@ Usage
|
|
22
64
|
####Generator
|
23
65
|
|
24
66
|
```ruby
|
25
|
-
rails generate
|
67
|
+
rails generate parameters [MODEL_NAME]
|
26
68
|
```
|
27
69
|
Will create **app/parameters/[model_name]_parameters.rb**.
|
28
70
|
|
@@ -36,9 +78,8 @@ permitted_params(options={})
|
|
36
78
|
|
37
79
|
#####Options Hash
|
38
80
|
|
39
|
-
* **options**
|
40
|
-
* **options[:class]**
|
41
|
-
* **options[:locals]** - Hash used to create helper methods available for the ActionParameter instance.
|
81
|
+
* **options** - Hash with one valid key: **:class**.
|
82
|
+
* **options[:class]** - Symbol value with the name of the Parameters class you want to use.
|
42
83
|
|
43
84
|
#####Example 1
|
44
85
|
|
@@ -80,7 +121,7 @@ class UserParameters < ActionParameter::Base
|
|
80
121
|
end
|
81
122
|
```
|
82
123
|
|
83
|
-
####Parameter Helpers
|
124
|
+
####Parameter Class Helpers
|
84
125
|
|
85
126
|
#####Default Helpers
|
86
127
|
|
@@ -90,11 +131,11 @@ end
|
|
90
131
|
|
91
132
|
#####Creating New Helpers
|
92
133
|
|
93
|
-
If you want to create new helper methods for you parameters class, just
|
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**.
|
94
135
|
|
95
136
|
```ruby
|
96
|
-
permitted_params(class: :user
|
97
|
-
|
137
|
+
permitted_params(class: :user).locals( current_user: @current_user,
|
138
|
+
another_helper: @value )
|
98
139
|
```
|
99
140
|
This will create **current_user** and **another_helper** methods and they will be available in UserParameters class.
|
100
141
|
|
@@ -104,21 +145,19 @@ This will create **current_user** and **another_helper** methods and they will b
|
|
104
145
|
# app/controllers/people_controllers.rb
|
105
146
|
class UsersController < ActionController::Base
|
106
147
|
def create
|
107
|
-
|
108
|
-
# This will call to UserParameters' sign_up method and will also create a
|
109
|
-
# helper method named 'current_user' which will return @current_user
|
148
|
+
User.create(permitted_params.locals(current_user: @current_user).permit) # This will call to PersonParameters' permit method
|
110
149
|
end
|
111
150
|
end
|
112
151
|
```
|
113
152
|
|
114
153
|
```ruby
|
115
|
-
# app/parameters/
|
154
|
+
# app/parameters/person_parameters.rb
|
116
155
|
class UserParameters < ActionParameter::Base
|
117
|
-
def
|
118
|
-
if
|
119
|
-
|
156
|
+
def permit
|
157
|
+
if user.admin?
|
158
|
+
params.require(:person).permit(:name, :age, :admin)
|
120
159
|
else
|
121
|
-
|
160
|
+
params.require(:person).permit(:name, :age)
|
122
161
|
end
|
123
162
|
end
|
124
163
|
end
|
@@ -3,20 +3,43 @@ module ActionParameter
|
|
3
3
|
|
4
4
|
attr_accessor :params
|
5
5
|
|
6
|
-
# initialize: Initialize parameter class.
|
6
|
+
# initialize: Initialize parameter class and creates controller_name and action_name helpers.
|
7
7
|
#
|
8
8
|
# == Options
|
9
9
|
#
|
10
10
|
# * <tt>params</tt> - The ActionController::Parameters instance from the controller who initialize this.
|
11
|
-
|
12
|
-
def initialize(params, locals = {})
|
11
|
+
def initialize(params)
|
13
12
|
@params = params
|
13
|
+
create_base_helpers
|
14
|
+
end
|
15
|
+
|
16
|
+
# locals: Creates helper methods for the ActionParameter instace.
|
17
|
+
#
|
18
|
+
# == Options
|
19
|
+
#
|
20
|
+
# * <tt>locals</tt> - Hash used to create helper methods available for the ActionParameter instance.
|
21
|
+
#
|
22
|
+
# == Examples
|
23
|
+
#
|
24
|
+
# * locals(new_method: @value, another_method: @other_value)
|
25
|
+
# # => 'ActionParameter instace'
|
26
|
+
#
|
27
|
+
# Returns the ActionParameter instace.
|
28
|
+
def locals(locals = {})
|
14
29
|
create_methods(locals)
|
30
|
+
self
|
15
31
|
end
|
16
32
|
|
17
33
|
protected
|
18
34
|
|
19
|
-
#
|
35
|
+
# create_base_helpers: Creates controller_name and action_name helper methods, every time an ActionParameter instace is created.
|
36
|
+
def create_base_helpers
|
37
|
+
locals = { controller_name: params[:controller],
|
38
|
+
action_name: params[:action] }
|
39
|
+
create_methods(locals)
|
40
|
+
end
|
41
|
+
|
42
|
+
# create_methods: Creates instance methods using locals hash's keys and values.
|
20
43
|
#
|
21
44
|
# == Options
|
22
45
|
#
|
@@ -26,18 +49,13 @@ module ActionParameter
|
|
26
49
|
#
|
27
50
|
# create_methods(current_user: @user, another_key: @another_variable)
|
28
51
|
#
|
29
|
-
# Will create 'current_user'
|
52
|
+
# Will create 'current_user' and 'another_key' instance methods.
|
30
53
|
# This methods will be aviable only in the current parameter class where create_method was called.
|
31
54
|
# 'current_user' will return @user.
|
32
55
|
# 'another_key' will return @another_variable
|
33
|
-
# 'controller_name' will return the controller instance's name
|
34
|
-
# 'action_name' will return the controller action instance's name
|
35
56
|
def create_methods(locals = {})
|
36
57
|
locals = {} unless locals
|
37
58
|
|
38
|
-
locals.merge!(controller_name: params[:controller],
|
39
|
-
action_name: params[:action])
|
40
|
-
|
41
59
|
klass = class << self; self; end
|
42
60
|
|
43
61
|
locals.each do |method_name, value|
|
@@ -7,37 +7,36 @@ module ActionParameter
|
|
7
7
|
#
|
8
8
|
# == Options
|
9
9
|
#
|
10
|
-
# * <tt>options</tt> - Hash with
|
10
|
+
# * <tt>options</tt> - Hash with one valid key: class.
|
11
11
|
# * <tt>options[:class]</tt> - Symbol value with the name of the Parameters class you want to use.
|
12
|
-
# * <tt>options[:locals]</tt> - Hash used to create helper methods available for the ActionParameter instance.
|
13
12
|
#
|
14
13
|
# == Examples
|
15
14
|
#
|
16
|
-
# permitted_params(class: customer
|
17
|
-
#
|
15
|
+
# permitted_params(class: customer) # called from UsersController
|
16
|
+
#
|
18
17
|
# This will create an instance of CustomerParameters and also will make
|
19
|
-
# '
|
18
|
+
# 'params', 'controller_name' and 'action_name' helper methods
|
20
19
|
# available on the CustomerParameters instace.
|
21
20
|
def permitted_params(options = {})
|
22
21
|
parameter_class = permitted_params_class(options[:class])
|
23
|
-
@permitted_params ||= parameter_class.new(params
|
22
|
+
@permitted_params ||= parameter_class.new(params)
|
24
23
|
end
|
25
24
|
|
26
25
|
# permitted_params_class: Returns a Parameters class.
|
27
26
|
#
|
28
27
|
# == Options
|
29
28
|
#
|
30
|
-
# * <tt>
|
29
|
+
# * <tt>class_name</tt> - Symbol value with the name of the Parameters class you want to use.
|
31
30
|
#
|
32
31
|
# == Examples
|
33
32
|
#
|
34
|
-
# permitted_params_class(
|
35
|
-
# # => CustomerParameters
|
33
|
+
# permitted_params_class(:customer) # called from PeopleController
|
34
|
+
# # => CustomerParameters
|
36
35
|
#
|
37
|
-
# permitted_params_class(
|
38
|
-
# # =>
|
36
|
+
# permitted_params_class(:customers) # called from PeopleController
|
37
|
+
# # => CustomerParameters
|
39
38
|
#
|
40
|
-
# permitted_params_class()
|
39
|
+
# permitted_params_class() # called from PeopleController
|
41
40
|
# # => PersonParameters
|
42
41
|
def permitted_params_class(class_name = nil)
|
43
42
|
class_name = class_name || params[:controller].to_s.singularize
|
@@ -1,10 +1,10 @@
|
|
1
1
|
require 'rails/generators'
|
2
2
|
require 'rails/generators/named_base'
|
3
3
|
|
4
|
-
class
|
4
|
+
class ParametersGenerator < Rails::Generators::NamedBase
|
5
5
|
source_root File.expand_path('../templates', __FILE__)
|
6
6
|
|
7
|
-
def
|
7
|
+
def parameters
|
8
8
|
template "parameter_class.rb", "app/parameters/#{name.underscore}_parameters.rb"
|
9
9
|
end
|
10
10
|
|
File without changes
|
data/test/test_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
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.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ezequiel Delpero
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-12-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -57,7 +57,10 @@ executables: []
|
|
57
57
|
extensions: []
|
58
58
|
extra_rdoc_files: []
|
59
59
|
files:
|
60
|
+
- .coveralls.yml
|
60
61
|
- .gitignore
|
62
|
+
- .travis.yml
|
63
|
+
- CHANGELOG.md
|
61
64
|
- Gemfile
|
62
65
|
- MIT-LICENSE
|
63
66
|
- README.md
|
@@ -69,9 +72,9 @@ files:
|
|
69
72
|
- lib/action_parameter/helpers.rb
|
70
73
|
- lib/action_parameter/railtie.rb
|
71
74
|
- lib/action_parameter/version.rb
|
72
|
-
- lib/generators/
|
73
|
-
- lib/generators/
|
74
|
-
- lib/generators/
|
75
|
+
- lib/generators/parameters/USAGE
|
76
|
+
- lib/generators/parameters/parameters_generator.rb
|
77
|
+
- lib/generators/parameters/templates/parameter_class.rb
|
75
78
|
- test/action_parameter_test.rb
|
76
79
|
- test/test_helper.rb
|
77
80
|
homepage: https://github.com/edelpero/action_parameter
|