nested_strong_parameters 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +23 -0
- data/README.md +180 -0
- data/Rakefile +11 -0
- data/lib/nested_strong_parameters.rb +5 -0
- data/lib/nested_strong_parameters/nested_strong_parameters.rb +55 -0
- data/lib/nested_strong_parameters/version.rb +3 -0
- data/nested_strong_parameters.gemspec +26 -0
- data/test/nested_strong_parameters_test.rb +64 -0
- data/test/test_helper.rb +5 -0
- metadata +128 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: cc0f70a0c0e5426698354f4a4a106ab46b4eeb72
|
4
|
+
data.tar.gz: d61d41f5b3f407091e736be640cb877ccc75c19a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4aee309e0e9cd4b3cf460db78d15eea68f15dc371d1dce3dcbc7584ea565ce844ae33e0a6ea0ce7299b24d5f6f5d9d0eaa6fc82adcd71802fa356fd72f5687f9
|
7
|
+
data.tar.gz: 50b8ca2417e6e92570cdf4d41808fb162951f258a2876d566fe90f8152ab3e3a9ea33066fcd7d4dba7d60155e3ada8fb9e14f58749eb51c81030c72e80668a3b
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
Copyright (c) 2014 Alejandro Babio
|
2
|
+
|
3
|
+
|
4
|
+
MIT License
|
5
|
+
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
7
|
+
a copy of this software and associated documentation files (the
|
8
|
+
"Software"), to deal in the Software without restriction, including
|
9
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
10
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
11
|
+
permit persons to whom the Software is furnished to do so, subject to
|
12
|
+
the following conditions:
|
13
|
+
|
14
|
+
The above copyright notice and this permission notice shall be
|
15
|
+
included in all copies or substantial portions of the Software.
|
16
|
+
|
17
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
18
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
19
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
20
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
21
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
22
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
23
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,180 @@
|
|
1
|
+
# NestedStrongParameters
|
2
|
+
|
3
|
+
If in your Rails app you use strong_parameters with nested_attributes, and believe the old attr_accessible were easier to use. This gem is for you.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'nested_strong_parameters'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install nested_strong_parameters
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
In your models use `strong_fields` to define the whitelist of fields that can be updated by params. If a model accepts nested attributes for `some_model`, add the field `<some_model>_attributes`. Also you can set a role with :as option, and works fine with STI. Yes, just like we used to do with protected_attributes. And an array parameter must be write in the same fashion at we do with strong_parameters.
|
24
|
+
|
25
|
+
And then use on permit this way: `params.require(:<model>).permit(<Model>.whitelist)`
|
26
|
+
|
27
|
+
An example:
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
Class Project < ActiveRecord::Base
|
31
|
+
has_many :tasks
|
32
|
+
has_many :taggings
|
33
|
+
has_many :tags, through: :taggings
|
34
|
+
|
35
|
+
accepts_nested_attributes_for :tasks
|
36
|
+
|
37
|
+
strong_fields :name, :description, :tasks_attributes, tag_ids: []
|
38
|
+
strong_fields :budget, as: :admin
|
39
|
+
end
|
40
|
+
|
41
|
+
Class Task < ActiveRecord::Base
|
42
|
+
belongs_to :project
|
43
|
+
|
44
|
+
strong_fields :name, :start, :end
|
45
|
+
end
|
46
|
+
```
|
47
|
+
|
48
|
+
Now call permit on params is really easy:
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
# at console
|
52
|
+
# with Project.whitelist, I got:
|
53
|
+
~/ (main) > Project.whitelist
|
54
|
+
=> [:name, :description, {:tasks_attributes=>[:name, :start, :end]},
|
55
|
+
{tag_ids: []}]
|
56
|
+
|
57
|
+
# or with admin role:
|
58
|
+
~/ (main) > Project.whitelist(:admin)
|
59
|
+
=> [:name, :description, {:tasks_attributes=>[:name, :start, :end]},
|
60
|
+
{tag_ids: []}, :budget]
|
61
|
+
|
62
|
+
# used to call permit
|
63
|
+
@project.update_attributes(params.require(:project).permit(Project.whitelist))
|
64
|
+
```
|
65
|
+
|
66
|
+
Can I avoid error prone with whitelist? Take a look to this real life example:
|
67
|
+
|
68
|
+
```ruby
|
69
|
+
# irb:
|
70
|
+
~/rails/rfinan (main) > Dealing.whitelist
|
71
|
+
[:due_date,
|
72
|
+
:note,
|
73
|
+
:status,
|
74
|
+
{:items_attributes=>
|
75
|
+
[:amount,
|
76
|
+
:dealing_id,
|
77
|
+
:product_selector,
|
78
|
+
:price,
|
79
|
+
:sign,
|
80
|
+
:reference_value,
|
81
|
+
:chk_item,
|
82
|
+
:sequence,
|
83
|
+
{:security_items_attributes=>
|
84
|
+
[:commission,
|
85
|
+
:commission_rate,
|
86
|
+
:interest,
|
87
|
+
:interest_rate,
|
88
|
+
:net_value,
|
89
|
+
:security_id,
|
90
|
+
:item_id,
|
91
|
+
:sequence,
|
92
|
+
{:check_attributes=>
|
93
|
+
[:amount,
|
94
|
+
:note,
|
95
|
+
:payment_day,
|
96
|
+
:commodity_id,
|
97
|
+
:owner,
|
98
|
+
:branch,
|
99
|
+
:number,
|
100
|
+
:bank_lookup,
|
101
|
+
:signer_lookup,
|
102
|
+
:chk_refuse]},
|
103
|
+
{:loan_attributes=>
|
104
|
+
[:amount,
|
105
|
+
:note,
|
106
|
+
:payment_day,
|
107
|
+
:commodity_id,
|
108
|
+
:owner,
|
109
|
+
:due_date,
|
110
|
+
:params]}]},
|
111
|
+
:payer_sequence,
|
112
|
+
:paid_items_count]},
|
113
|
+
{:person_attributes=>
|
114
|
+
[:name,
|
115
|
+
:phone,
|
116
|
+
:mobile,
|
117
|
+
:address,
|
118
|
+
:city,
|
119
|
+
:province,
|
120
|
+
:estate,
|
121
|
+
:postal_code,
|
122
|
+
:email,
|
123
|
+
:note]}]
|
124
|
+
|
125
|
+
# controller code with nested_strong_parameters:
|
126
|
+
Dealing.create(params.require(:dealing).permit(Dealing.whitelist))
|
127
|
+
|
128
|
+
# without nested_strong_parameters:
|
129
|
+
Dealing.create(params.require(:dealing).permit(
|
130
|
+
[
|
131
|
+
:due_date, :note, :status,
|
132
|
+
{
|
133
|
+
:items_attributes=>
|
134
|
+
[
|
135
|
+
:amount, :dealing_id, :product_selector, :price, :sign,
|
136
|
+
:reference_value, :chk_item, :sequence,
|
137
|
+
{
|
138
|
+
:security_items_attributes=>
|
139
|
+
[
|
140
|
+
:commission, :commission_rate, :interest, :interest_rate,
|
141
|
+
:net_value, :security_id, :item_id, :sequence,
|
142
|
+
{
|
143
|
+
:check_attributes=>
|
144
|
+
[
|
145
|
+
:amount, :note, :payment_day, :commodity_id, :owner,
|
146
|
+
:branch, :number, :bank_lookup, :signer_lookup,
|
147
|
+
:chk_refuse
|
148
|
+
]
|
149
|
+
},
|
150
|
+
{
|
151
|
+
:loan_attributes=>
|
152
|
+
[
|
153
|
+
:amount, :note, :payment_day, :commodity_id, :owner,
|
154
|
+
:due_date, :params
|
155
|
+
]
|
156
|
+
}
|
157
|
+
]
|
158
|
+
},
|
159
|
+
:payer_sequence, :paid_items_count
|
160
|
+
]
|
161
|
+
},
|
162
|
+
{
|
163
|
+
:person_attributes=>
|
164
|
+
[
|
165
|
+
:name, :phone, :mobile, :address, :city, :province, :estate,
|
166
|
+
:postal_code, :email, :note
|
167
|
+
]
|
168
|
+
}
|
169
|
+
]
|
170
|
+
)
|
171
|
+
|
172
|
+
```
|
173
|
+
|
174
|
+
## Contributing
|
175
|
+
|
176
|
+
1. Fork it ( https://github.com/alejandrobabio/nested_strong_parameters/fork )
|
177
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
178
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
179
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
180
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
require "active_support/concern"
|
2
|
+
require "active_support/core_ext"
|
3
|
+
|
4
|
+
module NestedStrongParameters
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
class_attribute :_strong_fields, instance_accessors: false
|
9
|
+
self._strong_fields ||= {}
|
10
|
+
end
|
11
|
+
|
12
|
+
module ClassMethods
|
13
|
+
def strong_fields(*args)
|
14
|
+
options = extract_option_as(args)
|
15
|
+
roles = options[:as] || :default
|
16
|
+
|
17
|
+
self._strong_fields = self._strong_fields.deep_dup
|
18
|
+
Array(roles).each do |role|
|
19
|
+
self._strong_fields[role] ||= []
|
20
|
+
self._strong_fields[role] += args
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def whitelist(role = nil)
|
25
|
+
map_params(self, role)
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def extract_option_as(args)
|
31
|
+
if args.last.is_a?(Hash) && args.last[:as]
|
32
|
+
args.pop
|
33
|
+
else
|
34
|
+
{}
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def map_params(model, role = nil)
|
39
|
+
fields = model._strong_fields[:default]
|
40
|
+
fields += model._strong_fields.fetch(role) { [] }
|
41
|
+
|
42
|
+
fields.uniq.map do |field|
|
43
|
+
if field.to_s.match /_attributes$/
|
44
|
+
{ field => map_params(nested_model(field), role) }
|
45
|
+
else
|
46
|
+
field
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def nested_model(field)
|
52
|
+
field.to_s.gsub(/_attributes$/, '').classify.constantize
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'nested_strong_parameters/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "nested_strong_parameters"
|
8
|
+
spec.version = NestedStrongParameters::VERSION
|
9
|
+
spec.authors = ["Alejandro Babio\n"]
|
10
|
+
spec.email = ["alejandro.e.babio@hotmail.com"]
|
11
|
+
spec.summary = %q{Make simple the use of nested_attributes with strong_parameters}
|
12
|
+
spec.description = %q{Make simple the use of nested_attributes with strong_parameters}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency "activerecord", "~> 4.0"
|
22
|
+
spec.add_dependency "activesupport", "~> 4.0"
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
24
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
25
|
+
spec.add_development_dependency "minitest", "~> 4.7"
|
26
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require 'test_helper'
|
3
|
+
|
4
|
+
class Parent < ActiveRecord::Base
|
5
|
+
strong_fields :name, :child_attributes, tags: []
|
6
|
+
end
|
7
|
+
|
8
|
+
class Child < ActiveRecord::Base
|
9
|
+
strong_fields :name, :age, :toys_attributes
|
10
|
+
end
|
11
|
+
|
12
|
+
class Toy < ActiveRecord::Base
|
13
|
+
strong_fields :name, :tag
|
14
|
+
strong_fields :price, as: :admin
|
15
|
+
end
|
16
|
+
|
17
|
+
class CarToy < Toy
|
18
|
+
end
|
19
|
+
|
20
|
+
class Father < Parent
|
21
|
+
strong_fields :wife, as: :admin
|
22
|
+
end
|
23
|
+
|
24
|
+
describe NestedStrongParameters do
|
25
|
+
|
26
|
+
it 'admin whitelist' do
|
27
|
+
toys_attributes = [:name, :tag, :price]
|
28
|
+
child_attributes = [:name, :age, toys_attributes: toys_attributes]
|
29
|
+
|
30
|
+
Parent.whitelist(:admin).must_equal [
|
31
|
+
:name, { child_attributes: child_attributes }, { tags: [] }
|
32
|
+
]
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'admin whitelist on STI Father' do
|
36
|
+
toys_attributes = [:name, :tag, :price]
|
37
|
+
child_attributes = [:name, :age, toys_attributes: toys_attributes]
|
38
|
+
|
39
|
+
Father.whitelist(:admin).must_equal [
|
40
|
+
:name, { child_attributes: child_attributes }, { tags: [] }, :wife
|
41
|
+
]
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'default whitelist' do
|
45
|
+
toys_attributes = [:name, :tag]
|
46
|
+
child_attributes = [:name, :age, toys_attributes: toys_attributes]
|
47
|
+
|
48
|
+
Parent.whitelist.must_equal [
|
49
|
+
:name, { child_attributes: child_attributes }, { tags: [] }
|
50
|
+
]
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'admin whitelist without nested attributes' do
|
54
|
+
Toy.whitelist(:admin).must_equal [:name, :tag, :price]
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'admin whitelist without nested attributes on STI' do
|
58
|
+
CarToy.whitelist(:admin).must_equal [:name, :tag, :price]
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'role without strong_fields equal default whitelist' do
|
62
|
+
Parent.whitelist.must_equal Parent.whitelist(:undefined_role)
|
63
|
+
end
|
64
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,128 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: nested_strong_parameters
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- |
|
8
|
+
Alejandro Babio
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2014-10-23 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activerecord
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - "~>"
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '4.0'
|
21
|
+
type: :runtime
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - "~>"
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '4.0'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: activesupport
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - "~>"
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '4.0'
|
35
|
+
type: :runtime
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - "~>"
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '4.0'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: bundler
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - "~>"
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '1.7'
|
49
|
+
type: :development
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - "~>"
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '1.7'
|
56
|
+
- !ruby/object:Gem::Dependency
|
57
|
+
name: rake
|
58
|
+
requirement: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - "~>"
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '10.0'
|
63
|
+
type: :development
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - "~>"
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '10.0'
|
70
|
+
- !ruby/object:Gem::Dependency
|
71
|
+
name: minitest
|
72
|
+
requirement: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - "~>"
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '4.7'
|
77
|
+
type: :development
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - "~>"
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '4.7'
|
84
|
+
description: Make simple the use of nested_attributes with strong_parameters
|
85
|
+
email:
|
86
|
+
- alejandro.e.babio@hotmail.com
|
87
|
+
executables: []
|
88
|
+
extensions: []
|
89
|
+
extra_rdoc_files: []
|
90
|
+
files:
|
91
|
+
- ".gitignore"
|
92
|
+
- Gemfile
|
93
|
+
- LICENSE.txt
|
94
|
+
- README.md
|
95
|
+
- Rakefile
|
96
|
+
- lib/nested_strong_parameters.rb
|
97
|
+
- lib/nested_strong_parameters/nested_strong_parameters.rb
|
98
|
+
- lib/nested_strong_parameters/version.rb
|
99
|
+
- nested_strong_parameters.gemspec
|
100
|
+
- test/nested_strong_parameters_test.rb
|
101
|
+
- test/test_helper.rb
|
102
|
+
homepage: ''
|
103
|
+
licenses:
|
104
|
+
- MIT
|
105
|
+
metadata: {}
|
106
|
+
post_install_message:
|
107
|
+
rdoc_options: []
|
108
|
+
require_paths:
|
109
|
+
- lib
|
110
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
111
|
+
requirements:
|
112
|
+
- - ">="
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '0'
|
115
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
116
|
+
requirements:
|
117
|
+
- - ">="
|
118
|
+
- !ruby/object:Gem::Version
|
119
|
+
version: '0'
|
120
|
+
requirements: []
|
121
|
+
rubyforge_project:
|
122
|
+
rubygems_version: 2.2.2
|
123
|
+
signing_key:
|
124
|
+
specification_version: 4
|
125
|
+
summary: Make simple the use of nested_attributes with strong_parameters
|
126
|
+
test_files:
|
127
|
+
- test/nested_strong_parameters_test.rb
|
128
|
+
- test/test_helper.rb
|