action_parameter 0.0.1
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 +7 -0
- data/.gitignore +9 -0
- data/Gemfile +8 -0
- data/MIT-LICENSE +20 -0
- data/README.md +125 -0
- data/README.rdoc +125 -0
- data/Rakefile +29 -0
- data/action_parameter.gemspec +22 -0
- data/lib/action_parameter.rb +11 -0
- data/lib/action_parameter/base.rb +51 -0
- data/lib/action_parameter/helpers.rb +49 -0
- data/lib/action_parameter/railtie.rb +13 -0
- data/lib/action_parameter/version.rb +3 -0
- data/lib/generators/parameter/USAGE +8 -0
- data/lib/generators/parameter/parameter_generator.rb +11 -0
- data/lib/generators/parameter/templates/parameter_class.rb +3 -0
- data/test/action_parameter_test.rb +61 -0
- data/test/test_helper.rb +30 -0
- metadata +103 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: aa9cc72cb78003cacc9033f3fd23a76bdf326123
|
4
|
+
data.tar.gz: a718df9d730d24e5950ace918081cf65048a3798
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: dd62ba7e769620aa895c2d5942c3144be5395acc9785cdf25e8a014053f71edbe84bcf7117e2380758990b28740af78ac56faa8fb775262cdfbb6542538d9736
|
7
|
+
data.tar.gz: 9065aa9389ec2ab881ddfffe93cf20bab5a6c7510a46d024ac615275f19d9b2424c2a029c63aba07764c3dad2543333f33e86721b2822af6192626b46dce8bb2
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2009-2013 Ezequiel Delpero. http://github.com/edelpero
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,125 @@
|
|
1
|
+
[](https://codeclimate.com/github/edelpero/action_parameter)
|
2
|
+
|
3
|
+
ActionParameter
|
4
|
+
===============
|
5
|
+
|
6
|
+
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
|
+
|
8
|
+
Install
|
9
|
+
-------
|
10
|
+
|
11
|
+
ActionParameter works with Rails 3.0 onwards and Ruby 1.9. You can add it to your Gemfile with:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'action_parameter'
|
15
|
+
```
|
16
|
+
|
17
|
+
Run the bundle command to install it.
|
18
|
+
|
19
|
+
Usage
|
20
|
+
-----
|
21
|
+
|
22
|
+
####Generator
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
rails generate parameter [MODEL_NAME]
|
26
|
+
```
|
27
|
+
Will create **app/parameters/[model_name]_parameters.rb**.
|
28
|
+
|
29
|
+
####Controller Helpers
|
30
|
+
|
31
|
+
- **permitted_params:** Returns an ActionParameter instance.
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
permitted_params(options={})
|
35
|
+
```
|
36
|
+
|
37
|
+
#####Options Hash
|
38
|
+
|
39
|
+
* **options** - Hash with two valid keys **:class** and **:locals**.
|
40
|
+
* **options[:class]** - Symbol value with the name of the Parameters class you want to use.
|
41
|
+
* **options[:locals]** - Hash used to create helper methods available for the ActionParameter instance.
|
42
|
+
|
43
|
+
#####Example 1
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
# app/controllers/people_controllers.rb
|
47
|
+
class PeopleController < ActionController::Base
|
48
|
+
def create
|
49
|
+
Person.create(permitted_params.permit) # This will call to PersonParameters' permit method
|
50
|
+
end
|
51
|
+
end
|
52
|
+
```
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
# app/parameters/person_parameters.rb
|
56
|
+
class PersonParameters < ActionParameter::Base
|
57
|
+
def permit
|
58
|
+
params.require(:person).permit(:name, :age)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
```
|
62
|
+
|
63
|
+
#####Example 2
|
64
|
+
|
65
|
+
```ruby
|
66
|
+
# app/controllers/people_controllers.rb
|
67
|
+
class PeopleController < ActionController::Base
|
68
|
+
def create
|
69
|
+
Person.create(permitted_params(class: :user).sign_up) # This will call to UserParameters' sign_up method
|
70
|
+
end
|
71
|
+
end
|
72
|
+
```
|
73
|
+
|
74
|
+
```ruby
|
75
|
+
# app/parameters/user_parameters.rb
|
76
|
+
class UserParameters < ActionParameter::Base
|
77
|
+
def sign_up
|
78
|
+
params.require(:person).permit(:name, :age)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
```
|
82
|
+
|
83
|
+
####Parameter Helpers
|
84
|
+
|
85
|
+
#####Default Helpers
|
86
|
+
|
87
|
+
- **params:** Returns params from the current controller request which instantiated the Parameter class.
|
88
|
+
- **controller_name:** Returns the controller's name from which the Parameter class was instantiated.
|
89
|
+
- **action_name:** Returns the action's name from the controller from which the Parameter class was instantiated.
|
90
|
+
|
91
|
+
#####Creating New Helpers
|
92
|
+
|
93
|
+
If you want to create new helper methods for you parameters class, just pass a hash using **:locals**. Let say you want to make **@current_user** available for the UserParameter's class, then you'll need to use the **:locals** option to tell the UserParameters class to create a new helper that returns **@current_user**.
|
94
|
+
|
95
|
+
```ruby
|
96
|
+
permitted_params(class: :user, locals: { current_user: @current_user,
|
97
|
+
another_helper: @value })
|
98
|
+
```
|
99
|
+
This will create **current_user** and **another_helper** methods and they will be available in UserParameters class.
|
100
|
+
|
101
|
+
#####Example
|
102
|
+
|
103
|
+
```ruby
|
104
|
+
# app/controllers/people_controllers.rb
|
105
|
+
class UsersController < ActionController::Base
|
106
|
+
def create
|
107
|
+
Person.create(permitted_params(locals: { current_user: @current_user }).sign_up)
|
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
|
110
|
+
end
|
111
|
+
end
|
112
|
+
```
|
113
|
+
|
114
|
+
```ruby
|
115
|
+
# app/parameters/user_parameters.rb
|
116
|
+
class UserParameters < ActionParameter::Base
|
117
|
+
def sign_up
|
118
|
+
if current_user # Method created using :locals option
|
119
|
+
params.permit!
|
120
|
+
else
|
121
|
+
params.require(:person).permit(:name, :age)
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
125
|
+
```
|
data/README.rdoc
ADDED
@@ -0,0 +1,125 @@
|
|
1
|
+
{<img src="https://codeclimate.com/github/edelpero/action_parameter.png" />}[https://codeclimate.com/github/edelpero/action_parameter]
|
2
|
+
|
3
|
+
ActionParameter
|
4
|
+
===============
|
5
|
+
|
6
|
+
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
|
+
|
8
|
+
Install
|
9
|
+
-------
|
10
|
+
|
11
|
+
ActionParameter works with Rails 3.0 onwards and Ruby 1.9. You can add it to your Gemfile with:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'action_parameter'
|
15
|
+
```
|
16
|
+
|
17
|
+
Run the bundle command to install it.
|
18
|
+
|
19
|
+
Usage
|
20
|
+
-----
|
21
|
+
|
22
|
+
####Generator
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
rails generate parameter [MODEL_NAME]
|
26
|
+
```
|
27
|
+
Will create **app/parameters/[model_name]_parameters.rb**.
|
28
|
+
|
29
|
+
####Controller Helpers
|
30
|
+
|
31
|
+
- **permitted_params:** Returns an ActionParameter instance.
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
permitted_params(options={})
|
35
|
+
```
|
36
|
+
|
37
|
+
#####Options Hash
|
38
|
+
|
39
|
+
* **options** - Hash with two valid keys **:class** and **:locals**.
|
40
|
+
* **options[:class]** - Symbol value with the name of the Parameters class you want to use.
|
41
|
+
* **options[:locals]** - Hash used to create helper methods available for the ActionParameter instance.
|
42
|
+
|
43
|
+
#####Example 1
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
# app/controllers/people_controllers.rb
|
47
|
+
class PeopleController < ActionController::Base
|
48
|
+
def create
|
49
|
+
Person.create(permitted_params.permit) # This will call to PersonParameters' permit method
|
50
|
+
end
|
51
|
+
end
|
52
|
+
```
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
# app/parameters/person_parameters.rb
|
56
|
+
class PersonParameters < ActionParameter::Base
|
57
|
+
def permit
|
58
|
+
params.require(:person).permit(:name, :age)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
```
|
62
|
+
|
63
|
+
#####Example 2
|
64
|
+
|
65
|
+
```ruby
|
66
|
+
# app/controllers/people_controllers.rb
|
67
|
+
class PeopleController < ActionController::Base
|
68
|
+
def create
|
69
|
+
Person.create(permitted_params(class: :user).sign_up) # This will call to UserParameters' sign_up method
|
70
|
+
end
|
71
|
+
end
|
72
|
+
```
|
73
|
+
|
74
|
+
```ruby
|
75
|
+
# app/parameters/user_parameters.rb
|
76
|
+
class UserParameters < ActionParameter::Base
|
77
|
+
def sign_up
|
78
|
+
params.require(:person).permit(:name, :age)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
```
|
82
|
+
|
83
|
+
####Parameter Helpers
|
84
|
+
|
85
|
+
#####Default Helpers
|
86
|
+
|
87
|
+
- **params:** Returns params from the current controller request which instantiated the Parameter class.
|
88
|
+
- **controller_name:** Returns the controller's name from which the Parameter class was instantiated.
|
89
|
+
- **action_name:** Returns the action's name from the controller from which the Parameter class was instantiated.
|
90
|
+
|
91
|
+
#####Creating New Helpers
|
92
|
+
|
93
|
+
If you want to create new helper methods for you parameters class, just pass a hash using **:locals**. Let say you want to make **@current_user** available for the UserParameter's class, then you'll need to use the **:locals** option to tell the UserParameters class to create a new helper that returns **@current_user**.
|
94
|
+
|
95
|
+
```ruby
|
96
|
+
permitted_params(class: :user, locals: { current_user: @current_user,
|
97
|
+
another_helper: @value })
|
98
|
+
```
|
99
|
+
This will create **current_user** and **another_helper** methods and they will be available in UserParameters class.
|
100
|
+
|
101
|
+
#####Example
|
102
|
+
|
103
|
+
```ruby
|
104
|
+
# app/controllers/people_controllers.rb
|
105
|
+
class UsersController < ActionController::Base
|
106
|
+
def create
|
107
|
+
Person.create(permitted_params(locals: { current_user: @current_user }).sign_up)
|
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
|
110
|
+
end
|
111
|
+
end
|
112
|
+
```
|
113
|
+
|
114
|
+
```ruby
|
115
|
+
# app/parameters/user_parameters.rb
|
116
|
+
class UserParameters < ActionParameter::Base
|
117
|
+
def sign_up
|
118
|
+
if current_user # Method created using :locals option
|
119
|
+
params.permit!
|
120
|
+
else
|
121
|
+
params.require(:person).permit(:name, :age)
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
125
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
begin
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'bundler/gem_tasks'
|
5
|
+
rescue LoadError
|
6
|
+
raise 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
7
|
+
end
|
8
|
+
|
9
|
+
require 'rdoc/task'
|
10
|
+
|
11
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
12
|
+
rdoc.rdoc_dir = 'rdoc'
|
13
|
+
rdoc.title = 'ActionParameter'
|
14
|
+
rdoc.options << '--line-numbers'
|
15
|
+
rdoc_main = 'README.md'
|
16
|
+
rdoc.rdoc_files.include('README.md')
|
17
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
18
|
+
end
|
19
|
+
|
20
|
+
require 'rake/testtask'
|
21
|
+
|
22
|
+
Rake::TestTask.new(:test) do |t|
|
23
|
+
t.libs << 'lib'
|
24
|
+
t.libs << 'test'
|
25
|
+
t.pattern = 'test/**/*_test.rb'
|
26
|
+
t.verbose = false
|
27
|
+
end
|
28
|
+
|
29
|
+
task :default => :test
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "action_parameter/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "action_parameter"
|
7
|
+
s.version = ActionParameter::VERSION.dup
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.summary = "Single Responsability Principle for Rails Controller's Parameters."
|
10
|
+
s.email = "edelpero@gmail.com"
|
11
|
+
s.homepage = "https://github.com/edelpero/action_parameter"
|
12
|
+
s.description = "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."
|
13
|
+
s.authors = ['Ezequiel Delpero']
|
14
|
+
s.license = "MIT"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- test/*`.split("\n")
|
18
|
+
s.require_paths = ["lib"]
|
19
|
+
|
20
|
+
s.add_dependency('activesupport', '>= 3.0.0', '< 4.1')
|
21
|
+
s.add_dependency('actionpack', '>= 3.0.0', '< 4.1')
|
22
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'action_pack'
|
2
|
+
require 'active_support'
|
3
|
+
require 'action_parameter/railtie' if defined?(Rails)
|
4
|
+
|
5
|
+
module ActionParameter
|
6
|
+
extend ActiveSupport::Autoload
|
7
|
+
|
8
|
+
autoload :Base, 'action_parameter/base.rb'
|
9
|
+
autoload :Helpers, 'action_parameter/helpers.rb'
|
10
|
+
|
11
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module ActionParameter
|
2
|
+
class Base
|
3
|
+
|
4
|
+
attr_accessor :params
|
5
|
+
|
6
|
+
# initialize: Initialize parameter class.
|
7
|
+
#
|
8
|
+
# == Options
|
9
|
+
#
|
10
|
+
# * <tt>params</tt> - The ActionController::Parameters instance from the controller who initialize this.
|
11
|
+
# * <tt>locals</tt> - Hash used to create helper methods available for the ActionParameter instance.
|
12
|
+
def initialize(params, locals = {})
|
13
|
+
@params = params
|
14
|
+
create_methods(locals)
|
15
|
+
end
|
16
|
+
|
17
|
+
protected
|
18
|
+
|
19
|
+
# create_methods: Creates instance methods using locals hash's keys and values, params[:controller] and params[:action].
|
20
|
+
#
|
21
|
+
# == Options
|
22
|
+
#
|
23
|
+
# * <tt>locals</tt> - Hash used to create helper methods available for the ActionParameter instance. Methods will be named using the hash keys and they'll return the hash values.
|
24
|
+
#
|
25
|
+
# == Examples
|
26
|
+
#
|
27
|
+
# create_methods(current_user: @user, another_key: @another_variable)
|
28
|
+
#
|
29
|
+
# Will create 'current_user', 'another_key', 'controller_name' and 'action_name' instance methods.
|
30
|
+
# This methods will be aviable only in the current parameter class where create_method was called.
|
31
|
+
# 'current_user' will return @user.
|
32
|
+
# '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
|
+
def create_methods(locals = {})
|
36
|
+
locals = {} unless locals
|
37
|
+
|
38
|
+
locals.merge!(controller_name: params[:controller],
|
39
|
+
action_name: params[:action])
|
40
|
+
|
41
|
+
klass = class << self; self; end
|
42
|
+
|
43
|
+
locals.each do |method_name, value|
|
44
|
+
klass.send(:define_method, method_name) do
|
45
|
+
value
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
module ActionParameter
|
2
|
+
module Helpers
|
3
|
+
|
4
|
+
protected
|
5
|
+
|
6
|
+
# permitted_params: Returns an ActionParameter instance.
|
7
|
+
#
|
8
|
+
# == Options
|
9
|
+
#
|
10
|
+
# * <tt>options</tt> - Hash with two valid keys: class and locals.
|
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
|
+
#
|
14
|
+
# == Examples
|
15
|
+
#
|
16
|
+
# permitted_params(class: customer, locals: { current_user: @user }) # called from UsersController
|
17
|
+
#
|
18
|
+
# This will create an instance of CustomerParameters and also will make
|
19
|
+
# 'current_user', 'params', 'controller_name' and 'action_name' helper methods
|
20
|
+
# available on the CustomerParameters instace.
|
21
|
+
def permitted_params(options = {})
|
22
|
+
parameter_class = permitted_params_class(options[:class])
|
23
|
+
@permitted_params ||= parameter_class.new(params, options[:locals])
|
24
|
+
end
|
25
|
+
|
26
|
+
# permitted_params_class: Returns a Parameters class.
|
27
|
+
#
|
28
|
+
# == Options
|
29
|
+
#
|
30
|
+
# * <tt>options[:class]</tt> - Symbol value with the name of the Parameters class you want to use.
|
31
|
+
#
|
32
|
+
# == Examples
|
33
|
+
#
|
34
|
+
# permitted_params_class(class: :customer) # called from PeopleController
|
35
|
+
# # => CustomerParameters
|
36
|
+
#
|
37
|
+
# permitted_params_class(class: :customers) # called from PeopleController
|
38
|
+
# # => CustomersParameters
|
39
|
+
#
|
40
|
+
# permitted_params_class() # called from PeopleController
|
41
|
+
# # => PersonParameters
|
42
|
+
def permitted_params_class(class_name = nil)
|
43
|
+
class_name = class_name || params[:controller].to_s.singularize
|
44
|
+
formatted_class_name = class_name.to_s.camelcase
|
45
|
+
@permitted_params_class ||= "#{formatted_class_name}Parameters".constantize
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'rails/generators'
|
2
|
+
require 'rails/generators/named_base'
|
3
|
+
|
4
|
+
class ParameterGenerator < Rails::Generators::NamedBase
|
5
|
+
source_root File.expand_path('../templates', __FILE__)
|
6
|
+
|
7
|
+
def parameter
|
8
|
+
template "parameter_class.rb", "app/parameters/#{name.underscore}_parameters.rb"
|
9
|
+
end
|
10
|
+
|
11
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class UsersController < ApplicationController
|
4
|
+
include ActionParameter::Helpers
|
5
|
+
|
6
|
+
def permitted_params_without_arguments
|
7
|
+
permitted_params
|
8
|
+
render nothing: true
|
9
|
+
end
|
10
|
+
|
11
|
+
def permitted_params_with_class_attribute
|
12
|
+
permitted_params(class: :customer)
|
13
|
+
render nothing: true
|
14
|
+
end
|
15
|
+
|
16
|
+
def permitted_params_with_locals
|
17
|
+
permitted_params(locals: { current_user: 'user'})
|
18
|
+
render nothing: true
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
class UserParameters < ActionParameter::Base
|
24
|
+
end
|
25
|
+
|
26
|
+
class CustomerParameters < ActionParameter::Base
|
27
|
+
end
|
28
|
+
|
29
|
+
class ActionParameterTest < ActionController::TestCase
|
30
|
+
tests UsersController
|
31
|
+
|
32
|
+
def test_permitted_params_without_arguments_is_an_instance_of_user_parameters
|
33
|
+
get :permitted_params_without_arguments
|
34
|
+
assert_equal('UserParameters', assigns(:permitted_params).class.to_s)
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_permitted_params_with_class_attribute_returns_an_instance_of_customer_parameters
|
38
|
+
get :permitted_params_with_class_attribute
|
39
|
+
assert_equal('CustomerParameters', assigns(:permitted_params).class.to_s)
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_permitted_params_instance_params_helper_method
|
43
|
+
get :permitted_params_without_arguments
|
44
|
+
assert_equal(@request.params, assigns(:permitted_params).params)
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_permitted_params_instance_controller_name_helper_method
|
48
|
+
get :permitted_params_without_arguments
|
49
|
+
assert_equal(@request.params['controller'], assigns(:permitted_params).controller_name)
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_permitted_params_instance_action_name_helper_method
|
53
|
+
get :permitted_params_without_arguments
|
54
|
+
assert_equal(@request.params['action'], assigns(:permitted_params).action_name)
|
55
|
+
end
|
56
|
+
|
57
|
+
def test_permitted_params_with_locals_should_respond_to_current_user_method
|
58
|
+
get :permitted_params_with_locals
|
59
|
+
assert_equal(true, assigns(:permitted_params).respond_to?(:current_user))
|
60
|
+
end
|
61
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'bundler'
|
2
|
+
|
3
|
+
Bundler.setup
|
4
|
+
require 'test/unit'
|
5
|
+
require 'mocha/setup'
|
6
|
+
|
7
|
+
# Configure Rails
|
8
|
+
ENV["RAILS_ENV"] = "test"
|
9
|
+
|
10
|
+
require 'active_support'
|
11
|
+
require 'action_controller'
|
12
|
+
require 'action_dispatch/middleware/flash'
|
13
|
+
|
14
|
+
$:.unshift File.expand_path('../../lib', __FILE__)
|
15
|
+
require 'action_parameter'
|
16
|
+
|
17
|
+
ActionParameter::Routes = ActionDispatch::Routing::RouteSet.new
|
18
|
+
ActionParameter::Routes.draw do
|
19
|
+
get '/:controller(/:action(/:id))'
|
20
|
+
end
|
21
|
+
|
22
|
+
class ApplicationController < ActionController::Base
|
23
|
+
include ActionParameter::Routes.url_helpers
|
24
|
+
end
|
25
|
+
|
26
|
+
class ActiveSupport::TestCase
|
27
|
+
setup do
|
28
|
+
@routes = ActionParameter::Routes
|
29
|
+
end
|
30
|
+
end
|
metadata
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: action_parameter
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ezequiel Delpero
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-10-08 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 3.0.0
|
20
|
+
- - <
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '4.1'
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 3.0.0
|
30
|
+
- - <
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '4.1'
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: actionpack
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - '>='
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: 3.0.0
|
40
|
+
- - <
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '4.1'
|
43
|
+
type: :runtime
|
44
|
+
prerelease: false
|
45
|
+
version_requirements: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - '>='
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: 3.0.0
|
50
|
+
- - <
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '4.1'
|
53
|
+
description: ActionParameter helps you move all your parameter's logic into it's own
|
54
|
+
class. This way you'll keep your controllers dry and they would be easier to test.
|
55
|
+
email: edelpero@gmail.com
|
56
|
+
executables: []
|
57
|
+
extensions: []
|
58
|
+
extra_rdoc_files: []
|
59
|
+
files:
|
60
|
+
- .gitignore
|
61
|
+
- Gemfile
|
62
|
+
- MIT-LICENSE
|
63
|
+
- README.md
|
64
|
+
- README.rdoc
|
65
|
+
- Rakefile
|
66
|
+
- action_parameter.gemspec
|
67
|
+
- lib/action_parameter.rb
|
68
|
+
- lib/action_parameter/base.rb
|
69
|
+
- lib/action_parameter/helpers.rb
|
70
|
+
- lib/action_parameter/railtie.rb
|
71
|
+
- lib/action_parameter/version.rb
|
72
|
+
- lib/generators/parameter/USAGE
|
73
|
+
- lib/generators/parameter/parameter_generator.rb
|
74
|
+
- lib/generators/parameter/templates/parameter_class.rb
|
75
|
+
- test/action_parameter_test.rb
|
76
|
+
- test/test_helper.rb
|
77
|
+
homepage: https://github.com/edelpero/action_parameter
|
78
|
+
licenses:
|
79
|
+
- MIT
|
80
|
+
metadata: {}
|
81
|
+
post_install_message:
|
82
|
+
rdoc_options: []
|
83
|
+
require_paths:
|
84
|
+
- lib
|
85
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - '>='
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
requirements: []
|
96
|
+
rubyforge_project:
|
97
|
+
rubygems_version: 2.1.3
|
98
|
+
signing_key:
|
99
|
+
specification_version: 4
|
100
|
+
summary: Single Responsability Principle for Rails Controller's Parameters.
|
101
|
+
test_files:
|
102
|
+
- test/action_parameter_test.rb
|
103
|
+
- test/test_helper.rb
|