strong_arms 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.travis.yml +7 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +48 -0
- data/LICENSE.txt +21 -0
- data/README.md +191 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/strong_arms/assertions.rb +29 -0
- data/lib/strong_arms/errors.rb +7 -0
- data/lib/strong_arms/exception_methods.rb +30 -0
- data/lib/strong_arms/utilities.rb +79 -0
- data/lib/strong_arms/version.rb +3 -0
- data/lib/strong_arms.rb +112 -0
- data/strong_arms.gemspec +49 -0
- metadata +119 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 0141fa031130367f64912bf360d670154e5e430105f516571b407353df1e32c3
|
4
|
+
data.tar.gz: 6fe45f23829d4faba9ab85ef8440e48baad05718fec7f817306d80d00afd2b1e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: bc34e47b39dc89a4d763293c1a9ea929103d08003718ebe735e5c6ea379de83b6b53a2b93175c1ebf79506923f51999ee92da4fe7dfdb90a0bbd2b39e620e335
|
7
|
+
data.tar.gz: 463c3d93e8bdc7c398bece9bb7eefe70ef696058f5ff0e5ff6457ea41b2ebfaaf00508405cba2902a21c9c70d73d229b61bc2fd8f9018c4489ddc6f52752db17
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at nate@mrjones.io. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
strong_arms (0.1.0)
|
5
|
+
activesupport (>= 5.1.6)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
activesupport (5.2.1)
|
11
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
12
|
+
i18n (>= 0.7, < 2)
|
13
|
+
minitest (~> 5.1)
|
14
|
+
tzinfo (~> 1.1)
|
15
|
+
concurrent-ruby (1.1.3)
|
16
|
+
diff-lcs (1.3)
|
17
|
+
i18n (1.1.1)
|
18
|
+
concurrent-ruby (~> 1.0)
|
19
|
+
minitest (5.11.3)
|
20
|
+
rake (10.5.0)
|
21
|
+
rspec (3.8.0)
|
22
|
+
rspec-core (~> 3.8.0)
|
23
|
+
rspec-expectations (~> 3.8.0)
|
24
|
+
rspec-mocks (~> 3.8.0)
|
25
|
+
rspec-core (3.8.0)
|
26
|
+
rspec-support (~> 3.8.0)
|
27
|
+
rspec-expectations (3.8.2)
|
28
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
29
|
+
rspec-support (~> 3.8.0)
|
30
|
+
rspec-mocks (3.8.0)
|
31
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
32
|
+
rspec-support (~> 3.8.0)
|
33
|
+
rspec-support (3.8.0)
|
34
|
+
thread_safe (0.3.6)
|
35
|
+
tzinfo (1.2.5)
|
36
|
+
thread_safe (~> 0.1)
|
37
|
+
|
38
|
+
PLATFORMS
|
39
|
+
ruby
|
40
|
+
|
41
|
+
DEPENDENCIES
|
42
|
+
bundler (~> 1.17)
|
43
|
+
rake (~> 10.0)
|
44
|
+
rspec (~> 3.0)
|
45
|
+
strong_arms!
|
46
|
+
|
47
|
+
BUNDLED WITH
|
48
|
+
1.17.1
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 mrjonesbot
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,191 @@
|
|
1
|
+
# Strong Arms
|
2
|
+
|
3
|
+
A nested parameters friendly alternative to [strong_parameters](https://github.com/rails/strong_parameters) found in Rails.
|
4
|
+
|
5
|
+
We love `strong_parameters`, but wanted a cleaner approach for handling deeply nested parameters.
|
6
|
+
|
7
|
+
`permit()` is great for handling a few parameters, but becomes tough to read and prone to user error when describing a deeply nested structure.
|
8
|
+
|
9
|
+
`require()` is great for ensuring top level keys are present, but does not provide a great interface for flagging specific nested parameters.
|
10
|
+
|
11
|
+
Strong Arms provides an interface which makes declaring nested relationships easy, in addition to whitelisting parameters with `permit`, `require` and `ignore`.
|
12
|
+
|
13
|
+
## Installation
|
14
|
+
|
15
|
+
Add this line to your application's Gemfile:
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
gem 'strong_arms'
|
19
|
+
```
|
20
|
+
|
21
|
+
And then execute:
|
22
|
+
|
23
|
+
$ bundle
|
24
|
+
|
25
|
+
Or install it yourself as:
|
26
|
+
|
27
|
+
$ gem install strong_arms
|
28
|
+
|
29
|
+
## Usage
|
30
|
+
|
31
|
+
**Controller Definition**
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
|
35
|
+
class PostController < ApplicationController
|
36
|
+
def create
|
37
|
+
post = Post.create(post_params)
|
38
|
+
render json: post
|
39
|
+
end
|
40
|
+
|
41
|
+
def update
|
42
|
+
post = Post.find(params[:id])
|
43
|
+
post.update(post_params)
|
44
|
+
render json: post
|
45
|
+
end
|
46
|
+
|
47
|
+
private
|
48
|
+
|
49
|
+
def post_params
|
50
|
+
PostStrongArm.flex(params[:post])
|
51
|
+
end
|
52
|
+
end
|
53
|
+
```
|
54
|
+
|
55
|
+
**Strong Arms Definition**
|
56
|
+
|
57
|
+
```ruby
|
58
|
+
# app/strong_arms
|
59
|
+
|
60
|
+
class PostStrongArm
|
61
|
+
ignore :created_at, :updated_at
|
62
|
+
|
63
|
+
permit :id
|
64
|
+
permit :title
|
65
|
+
permit :description
|
66
|
+
permit :user_id, required: true
|
67
|
+
|
68
|
+
many_nested :comments
|
69
|
+
end
|
70
|
+
|
71
|
+
class CommentStrongArm
|
72
|
+
ignore :created_at, :updated_at
|
73
|
+
|
74
|
+
permit :id
|
75
|
+
permit :body
|
76
|
+
permit :user_id, required: true
|
77
|
+
end
|
78
|
+
```
|
79
|
+
|
80
|
+
## Permitting Parameters
|
81
|
+
|
82
|
+
Permitted parameters are defined in Strong Arms with the `permit` method.
|
83
|
+
|
84
|
+
If parameters are passed to Strong Arms without being permitted, Strong Arms will raise an exception.
|
85
|
+
|
86
|
+
```ruby
|
87
|
+
class UserStrongArm
|
88
|
+
permit :id
|
89
|
+
end
|
90
|
+
```
|
91
|
+
|
92
|
+
## Requiring Parameters
|
93
|
+
|
94
|
+
Permitted parameters can be "required", by passing the `:required` option.
|
95
|
+
|
96
|
+
When required, Strong Arms expects parameter data to be present.
|
97
|
+
|
98
|
+
If it is absent, Strong Arms will raise an exception.
|
99
|
+
|
100
|
+
```ruby
|
101
|
+
class UserStrongArm
|
102
|
+
permit :id
|
103
|
+
permit :email, required: true
|
104
|
+
end
|
105
|
+
```
|
106
|
+
|
107
|
+
## Ignoring Parameters
|
108
|
+
|
109
|
+
Parameters passed to Strong Arms can be ignored, with the `ignore` method.
|
110
|
+
|
111
|
+
Commonly ignored parameters include auto incremented or optional (nillable) values.
|
112
|
+
|
113
|
+
```ruby
|
114
|
+
class UserStrongArm
|
115
|
+
ignore :created_at, :updated_at
|
116
|
+
|
117
|
+
permit :id
|
118
|
+
permit :email, required: true
|
119
|
+
end
|
120
|
+
```
|
121
|
+
|
122
|
+
## Nested Parameters
|
123
|
+
|
124
|
+
StrongArm handles nested parameters, by specifying a relationship with `many_nested` or `one_nested` methods.
|
125
|
+
|
126
|
+
This is similar to how Rails handles association declarations with `has_many` and `has_one` methods.
|
127
|
+
|
128
|
+
```ruby
|
129
|
+
class UserStrongArm
|
130
|
+
ignore :created_at, :updated_at
|
131
|
+
|
132
|
+
permit :id
|
133
|
+
permit :email, required: true
|
134
|
+
|
135
|
+
many_nested :posts # has_many :posts
|
136
|
+
one_nested :profile # has_one :profile
|
137
|
+
end
|
138
|
+
|
139
|
+
class PostStrongArm
|
140
|
+
ignore :created_at, :updated_at
|
141
|
+
|
142
|
+
permit :id
|
143
|
+
permit :title
|
144
|
+
permit :user_id, required: true
|
145
|
+
end
|
146
|
+
|
147
|
+
class ProfileStrongArm
|
148
|
+
ignore :created_at, :updated_at
|
149
|
+
|
150
|
+
permit :id
|
151
|
+
permit :name
|
152
|
+
permit :birthday
|
153
|
+
permit :user_id, required: true
|
154
|
+
end
|
155
|
+
```
|
156
|
+
|
157
|
+
Strong Arms expects nested parameters to follow the `accepts_nested_attributes_for` convention.
|
158
|
+
|
159
|
+
```ruby
|
160
|
+
many_nested :posts # { posts_attributes: [] }
|
161
|
+
one_nested :profile # { profile_attributes: {} }
|
162
|
+
```
|
163
|
+
|
164
|
+
If you do not wish to use `accepts_nested_attributes_for`, provide the `:format` option.
|
165
|
+
|
166
|
+
```ruby
|
167
|
+
many_nested :posts, format: false # { posts: [] }
|
168
|
+
one_nested :profile, format: false # { profile: {} }
|
169
|
+
```
|
170
|
+
|
171
|
+
If Strong Arms cannot find the relevant Strong Arms class for the nested resource, it will raise an exception.
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
## Development
|
176
|
+
|
177
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
178
|
+
|
179
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
180
|
+
|
181
|
+
## Contributing
|
182
|
+
|
183
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/launchpadlab/strong_arms. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
184
|
+
|
185
|
+
## License
|
186
|
+
|
187
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
188
|
+
|
189
|
+
## Code of Conduct
|
190
|
+
|
191
|
+
Everyone interacting in the Strong Arms project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/launchpadlab/strong_arms/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "strong_arms"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
module Assertions
|
2
|
+
def required_input_value_missing?(options, value)
|
3
|
+
required_input?(options) && value.blank?
|
4
|
+
end
|
5
|
+
|
6
|
+
def required_input?(options)
|
7
|
+
options[:required]
|
8
|
+
end
|
9
|
+
|
10
|
+
# NOTE: TBD
|
11
|
+
# def all_values_empty?(values)
|
12
|
+
# values.all? do |value|
|
13
|
+
# value.nil? || value == ""
|
14
|
+
# end
|
15
|
+
# end
|
16
|
+
|
17
|
+
def multiple_attributes?(attribute)
|
18
|
+
length_is_greater_than?(attribute, 1)
|
19
|
+
end
|
20
|
+
|
21
|
+
def unhandled_keys_present?(args)
|
22
|
+
unhandled_keys(args).present?
|
23
|
+
end
|
24
|
+
|
25
|
+
# ActiveSupport definition
|
26
|
+
# def blank?
|
27
|
+
# respond_to?(:empty?) ? !!empty? : !self
|
28
|
+
# end
|
29
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module ExceptionMethods
|
2
|
+
def missing_parser_exception
|
3
|
+
raise ArgumentError,
|
4
|
+
"#{name}: No parser specified for input with multiple values."
|
5
|
+
end
|
6
|
+
|
7
|
+
def unhandled_keys_exception(args)
|
8
|
+
keys = unhandled_keys(args)
|
9
|
+
Errors::UnhandledKeys.
|
10
|
+
new("#{name} received unhandled keys: #{keys.join(', ')}.")
|
11
|
+
end
|
12
|
+
|
13
|
+
def empty_arguments_exception
|
14
|
+
ArgumentError.new('No values were passed.')
|
15
|
+
end
|
16
|
+
|
17
|
+
def missing_parsers_for_multiple_attributes_exception
|
18
|
+
ArgumentError.
|
19
|
+
new("#{name} no parser specified for input with multiple values.")
|
20
|
+
end
|
21
|
+
|
22
|
+
def multiple_attributes_exception
|
23
|
+
ArgumentError.
|
24
|
+
new("#{name} recieved multiple attributes for a single input.")
|
25
|
+
end
|
26
|
+
|
27
|
+
def missing_value_for_required_input_exception(name)
|
28
|
+
ArgumentError.new("No value for required input: #{name}.")
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
module Utilities
|
2
|
+
def build_handler(name, options, type:)
|
3
|
+
{
|
4
|
+
name: name,
|
5
|
+
options: options,
|
6
|
+
type: type,
|
7
|
+
}
|
8
|
+
end
|
9
|
+
|
10
|
+
def expose_data_key_if_present(args)
|
11
|
+
if args[:data].present?
|
12
|
+
args[:data]
|
13
|
+
else
|
14
|
+
args
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def value_is_absent?(value, allow_nil: false)
|
19
|
+
return false if allow_nil
|
20
|
+
return true if value.nil?
|
21
|
+
|
22
|
+
if value.is_a? Hash
|
23
|
+
value.blank?
|
24
|
+
else
|
25
|
+
[value].flatten.blank?
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def model_name_from_attributes_key(name)
|
30
|
+
name.to_s.gsub('s_attributes', '').camelize
|
31
|
+
end
|
32
|
+
|
33
|
+
def find_strong_arm(nested_attributes_key)
|
34
|
+
model_name = model_name_from_attributes_key(nested_attributes_key)
|
35
|
+
strong_arm = model_name + "StrongArm"
|
36
|
+
strong_arm.constantize
|
37
|
+
end
|
38
|
+
|
39
|
+
def unhandled_keys(args)
|
40
|
+
keys = symbolized_keys_array(args)
|
41
|
+
keys - handlers_keys - keys_to_ignore
|
42
|
+
end
|
43
|
+
|
44
|
+
def handlers_keys
|
45
|
+
handlers.keys.flatten.uniq
|
46
|
+
end
|
47
|
+
|
48
|
+
def handlers_values
|
49
|
+
handlers.values
|
50
|
+
end
|
51
|
+
|
52
|
+
def symbolized_keys_array(args)
|
53
|
+
args.keys.map(&:to_sym)
|
54
|
+
end
|
55
|
+
|
56
|
+
def accessible_hash(args)
|
57
|
+
args.to_unsafe_h.with_indifferent_access
|
58
|
+
end
|
59
|
+
|
60
|
+
def action_controller_args?(args)
|
61
|
+
args.class.name == 'ActionController::Parameters'
|
62
|
+
end
|
63
|
+
|
64
|
+
def length_is_greater_than?(array, value)
|
65
|
+
array.length > value
|
66
|
+
end
|
67
|
+
|
68
|
+
def length_is_equal_to?(array, value)
|
69
|
+
array.length == value
|
70
|
+
end
|
71
|
+
|
72
|
+
def to_class_name(object)
|
73
|
+
to_name(object.class)
|
74
|
+
end
|
75
|
+
|
76
|
+
def to_name(klass)
|
77
|
+
klass.name
|
78
|
+
end
|
79
|
+
end
|
data/lib/strong_arms.rb
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
require "strong_arms/version"
|
2
|
+
require "strong_arms/utilities"
|
3
|
+
require "strong_arms/errors"
|
4
|
+
require "strong_arms/exception_methods"
|
5
|
+
require "strong_arms/assertions"
|
6
|
+
require "active_support/all"
|
7
|
+
|
8
|
+
module StrongArms
|
9
|
+
include Utilities
|
10
|
+
include Errors
|
11
|
+
include ExceptionMethods
|
12
|
+
include Assertions
|
13
|
+
include ActiveSupport
|
14
|
+
|
15
|
+
def handlers
|
16
|
+
@handlers ||= {}
|
17
|
+
end
|
18
|
+
|
19
|
+
def ignore(*args)
|
20
|
+
@keys_to_ignore = args
|
21
|
+
end
|
22
|
+
|
23
|
+
def keys_to_ignore
|
24
|
+
@keys_to_ignore ||= []
|
25
|
+
end
|
26
|
+
|
27
|
+
def permit(attribute, **options)
|
28
|
+
attributes = [attribute].flatten
|
29
|
+
|
30
|
+
if multiple_attributes?(attributes)
|
31
|
+
raise multiple_attributes_exception
|
32
|
+
end
|
33
|
+
|
34
|
+
set_handler(attribute, options, type: :input)
|
35
|
+
end
|
36
|
+
|
37
|
+
def one_nested(association, format: true)
|
38
|
+
modified_key = format ? nested_attributes_key(association) : association
|
39
|
+
set_handler(modified_key, { has_many: false }, type: :association)
|
40
|
+
end
|
41
|
+
|
42
|
+
def many_nested(association, format: true)
|
43
|
+
modified_key = format ? nested_attributes_key(association) : association
|
44
|
+
set_handler(modified_key, { has_many: true }, type: :association)
|
45
|
+
end
|
46
|
+
|
47
|
+
def flex(args)
|
48
|
+
_args = action_controller_args?(args) ? accessible_hash(args) : args
|
49
|
+
exposed_args = expose_data_key_if_present(_args)
|
50
|
+
|
51
|
+
raise empty_arguments_exception if exposed_args.blank?
|
52
|
+
if unhandled_keys_present?(exposed_args)
|
53
|
+
raise unhandled_keys_exception(exposed_args)
|
54
|
+
end
|
55
|
+
|
56
|
+
reduce_handlers(handlers_values, exposed_args)
|
57
|
+
end
|
58
|
+
|
59
|
+
def nested_attributes_key(association)
|
60
|
+
"#{association}_attributes".to_sym
|
61
|
+
end
|
62
|
+
|
63
|
+
def set_handler(name, options, type:)
|
64
|
+
handlers[name] =
|
65
|
+
build_handler(name, options, type: type)
|
66
|
+
end
|
67
|
+
|
68
|
+
def reduce_handlers(handlers, args)
|
69
|
+
return {} if handlers.empty?
|
70
|
+
|
71
|
+
handlers.reduce({}) do |new_hash, handler|
|
72
|
+
parsed_handler = extract_handler_values_and_parse(handler, args)
|
73
|
+
new_hash.merge(parsed_handler)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def extract_handler_values_and_parse(handler, args)
|
78
|
+
name = handler[:name]
|
79
|
+
options = handler[:options]
|
80
|
+
type = handler[:type]
|
81
|
+
allow_nil = options[:allow_nil]
|
82
|
+
value_at_name = args[name]
|
83
|
+
|
84
|
+
if required_input_value_missing?(options, value_at_name)
|
85
|
+
raise missing_value_for_required_input_exception(name)
|
86
|
+
end
|
87
|
+
|
88
|
+
return {} if value_is_absent?(value_at_name, allow_nil: allow_nil)
|
89
|
+
|
90
|
+
send("parse_#{type}", name: name, value: value_at_name, options: options)
|
91
|
+
end
|
92
|
+
|
93
|
+
def parse_input(name:, value:, options:) # options goes unused
|
94
|
+
{ name => value }
|
95
|
+
end
|
96
|
+
|
97
|
+
def parse_association(name:, value:, options:)
|
98
|
+
strong_arm = find_strong_arm(name)
|
99
|
+
wrapped_values = [value].flatten
|
100
|
+
has_many = options[:has_many]
|
101
|
+
|
102
|
+
strained_values = wrapped_values.map do |wrapped_value|
|
103
|
+
strong_arm.flex(wrapped_value)
|
104
|
+
end
|
105
|
+
|
106
|
+
if has_many
|
107
|
+
{ name => strained_values }
|
108
|
+
else
|
109
|
+
{ name => strained_values.pop }
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
data/strong_arms.gemspec
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "strong_arms/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "strong_arms"
|
8
|
+
spec.version = StrongArms::VERSION
|
9
|
+
spec.authors = ["mrjonesbot"]
|
10
|
+
spec.email = ["nate@mrjones.io"]
|
11
|
+
|
12
|
+
spec.summary = %q{Strong Parameters alternative for Ruby/Rails applications}
|
13
|
+
# spec.description = %q{TODO: Write a longer description or delete this line.}
|
14
|
+
spec.homepage = "https://github.com/launchpadlab/strong_arms"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
19
|
+
# if spec.respond_to?(:metadata)
|
20
|
+
# spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
|
21
|
+
|
22
|
+
# spec.metadata["homepage_uri"] = spec.homepage
|
23
|
+
# spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
|
24
|
+
# spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
|
25
|
+
# else
|
26
|
+
# raise "RubyGems 2.0 or newer is required to protect against " \
|
27
|
+
# "public gem pushes."
|
28
|
+
# end
|
29
|
+
|
30
|
+
spec.required_ruby_version = '>= 2.0.0' if spec.respond_to? :required_ruby_version=
|
31
|
+
spec.required_rubygems_version = Gem::Requirement.new(">= 0") if spec.respond_to? :required_rubygems_version=
|
32
|
+
spec.metadata = { "allowed_push_host" => "https://rubygems.org" } if spec.respond_to? :metadata=
|
33
|
+
spec.rubygems_version = "2.5.3"
|
34
|
+
|
35
|
+
# Specify which files should be added to the gem when it is released.
|
36
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
37
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
38
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
39
|
+
end
|
40
|
+
spec.bindir = "exe"
|
41
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
42
|
+
spec.require_paths = ["lib"]
|
43
|
+
|
44
|
+
spec.add_development_dependency "bundler", "~> 1.17"
|
45
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
46
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
47
|
+
|
48
|
+
spec.add_runtime_dependency(%q<activesupport>, [">= 5.1.6"])
|
49
|
+
end
|
metadata
ADDED
@@ -0,0 +1,119 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: strong_arms
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- mrjonesbot
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-11-29 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.17'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.17'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: activesupport
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 5.1.6
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 5.1.6
|
69
|
+
description:
|
70
|
+
email:
|
71
|
+
- nate@mrjones.io
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- ".travis.yml"
|
79
|
+
- CODE_OF_CONDUCT.md
|
80
|
+
- Gemfile
|
81
|
+
- Gemfile.lock
|
82
|
+
- LICENSE.txt
|
83
|
+
- README.md
|
84
|
+
- Rakefile
|
85
|
+
- bin/console
|
86
|
+
- bin/setup
|
87
|
+
- lib/strong_arms.rb
|
88
|
+
- lib/strong_arms/assertions.rb
|
89
|
+
- lib/strong_arms/errors.rb
|
90
|
+
- lib/strong_arms/exception_methods.rb
|
91
|
+
- lib/strong_arms/utilities.rb
|
92
|
+
- lib/strong_arms/version.rb
|
93
|
+
- strong_arms.gemspec
|
94
|
+
homepage: https://github.com/launchpadlab/strong_arms
|
95
|
+
licenses:
|
96
|
+
- MIT
|
97
|
+
metadata:
|
98
|
+
allowed_push_host: https://rubygems.org
|
99
|
+
post_install_message:
|
100
|
+
rdoc_options: []
|
101
|
+
require_paths:
|
102
|
+
- lib
|
103
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
104
|
+
requirements:
|
105
|
+
- - ">="
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: 2.0.0
|
108
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
109
|
+
requirements:
|
110
|
+
- - ">="
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
version: '0'
|
113
|
+
requirements: []
|
114
|
+
rubyforge_project:
|
115
|
+
rubygems_version: 2.7.6
|
116
|
+
signing_key:
|
117
|
+
specification_version: 4
|
118
|
+
summary: Strong Parameters alternative for Ruby/Rails applications
|
119
|
+
test_files: []
|