wrap_attributes 0.1.0
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/MIT-LICENSE +20 -0
- data/README.md +50 -0
- data/Rakefile +27 -0
- data/lib/tasks/wrap_attributes_tasks.rake +4 -0
- data/lib/wrap_attributes/attributes_wrapper.rb +41 -0
- data/lib/wrap_attributes/converter.rb +39 -0
- data/lib/wrap_attributes/railtie.rb +4 -0
- data/lib/wrap_attributes/version.rb +3 -0
- data/lib/wrap_attributes.rb +4 -0
- metadata +108 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 6ad0381547689374084f65e0d47b22e4319053a7dd31b80323683a09c392bfcf
|
4
|
+
data.tar.gz: e62cc1e2eb866d18b34eff5e7938eab1ec7e75143c892a3bbe2e3ceb228032fe
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 39ed0f2829520f1791cbb34dc6451d0433572ab077fc2dbdd4997db2c79bf7ee57d1d4f4e33ebbf7495f3910c1be6c7a17eb71f679beca1952e470b825ed6cdb
|
7
|
+
data.tar.gz: 994ed0dc6da21d1b56c94f80d2e14ef6f936cf1de083ef14f4c8548cabae7c7e4471dc59fee52608f2f88f7aaabbd494574a20cc80ab3caa54d4d7bd11df464e
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2019 tanmen
|
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,50 @@
|
|
1
|
+
# WrapAttributes
|
2
|
+
add '_attributes' suffix in rails
|
3
|
+
|
4
|
+
## Usage
|
5
|
+
```ruby
|
6
|
+
class ApplicationController < ActionController::API
|
7
|
+
include WrapAttributes::AttributesWrapper # <- add
|
8
|
+
end
|
9
|
+
```
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
# request parameter
|
13
|
+
# { 'username' => 'tanmen', 'posts' => [{'text' => 'hallo'}] }
|
14
|
+
# ↓
|
15
|
+
# wrapped parameter
|
16
|
+
# { 'username' => 'tanmen', 'posts_attributes' => [{'text' => 'hallo'}] }
|
17
|
+
class UsersController < ApplicationController
|
18
|
+
wrap_attributes :posts # <- add
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def user_params
|
23
|
+
params.require(:user).permit(:username, :posts_attributes)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
```
|
27
|
+
|
28
|
+
|
29
|
+
## Installation
|
30
|
+
Add this line to your application's Gemfile:
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
gem 'wrap_attributes'
|
34
|
+
```
|
35
|
+
|
36
|
+
And then execute:
|
37
|
+
```bash
|
38
|
+
$ bundle
|
39
|
+
```
|
40
|
+
|
41
|
+
Or install it yourself as:
|
42
|
+
```bash
|
43
|
+
$ gem install wrap_attributes
|
44
|
+
```
|
45
|
+
|
46
|
+
## Contributing
|
47
|
+
Contribution directions go here.
|
48
|
+
|
49
|
+
## License
|
50
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'WrapAttributes'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.md')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
require 'bundler/gem_tasks'
|
18
|
+
|
19
|
+
require 'rake/testtask'
|
20
|
+
|
21
|
+
Rake::TestTask.new(:test) do |t|
|
22
|
+
t.libs << 'test'
|
23
|
+
t.pattern = 'test/**/*_test.rb'
|
24
|
+
t.verbose = false
|
25
|
+
end
|
26
|
+
|
27
|
+
task default: :test
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require_relative 'converter'
|
2
|
+
|
3
|
+
module WrapAttributes
|
4
|
+
module AttributesWrapper
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
class_attribute :_attribute_options, default: []
|
9
|
+
end
|
10
|
+
|
11
|
+
module ClassMethods
|
12
|
+
def wrap_attributes(*keys)
|
13
|
+
self._attribute_options = keys
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def process_action(*args)
|
18
|
+
_perform_attribute_parameter_wrapping
|
19
|
+
super
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def _wrap_attribute_parameters(parameters, attributes)
|
25
|
+
WrapAttributes::convert(parameters, attributes)
|
26
|
+
end
|
27
|
+
|
28
|
+
def _perform_attribute_parameter_wrapping
|
29
|
+
wrapped_keys = request.request_parameters.keys
|
30
|
+
wrap_attribute_parameters = _wrap_attribute_parameters(request.request_parameters,
|
31
|
+
_attribute_options)
|
32
|
+
filtered_attribute_parameters = _wrap_attribute_parameters(request.filtered_parameters.slice(*wrapped_keys),
|
33
|
+
_attribute_options)
|
34
|
+
|
35
|
+
request.parameters.merge! wrap_attribute_parameters
|
36
|
+
request.request_parameters.merge! wrap_attribute_parameters
|
37
|
+
|
38
|
+
request.filtered_parameters.merge! filtered_attribute_parameters
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module WrapAttributes
|
2
|
+
def self.convert(parameters, attributes)
|
3
|
+
if parameters.is_a?(Symbol) || parameters.is_a?(String)
|
4
|
+
parameters
|
5
|
+
elsif parameters.is_a?(Hash)
|
6
|
+
if attributes.is_a?(Symbol) || attributes.is_a?(String)
|
7
|
+
parameters.map { |key, value| key.to_sym == attributes ? [attribute_key(key), value] : [key, value] }.to_h
|
8
|
+
elsif attributes.is_a?(Hash)
|
9
|
+
parameters.map do |key, params|
|
10
|
+
attr = attributes&.[](key.to_sym)
|
11
|
+
attr.present? ? [attribute_key(key), convert(params, attr)] : [key, params]
|
12
|
+
end.to_h
|
13
|
+
elsif attributes.is_a?(Array)
|
14
|
+
parameters.map do |key, params|
|
15
|
+
attributes.reduce([key, params]) do |pre, cur|
|
16
|
+
if cur.is_a?(Symbol) && cur == key.to_sym
|
17
|
+
[attribute_key(key), params]
|
18
|
+
elsif cur.is_a?(Hash) && cur.keys.include?(key.to_sym)
|
19
|
+
[attribute_key(key), convert(params, cur[key.to_sym])]
|
20
|
+
else
|
21
|
+
pre
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end.to_h
|
25
|
+
end
|
26
|
+
elsif parameters.is_a?(Array)
|
27
|
+
parameters.map do |params|
|
28
|
+
convert(params, attributes)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
|
36
|
+
def self.attribute_key(attribute)
|
37
|
+
(+attribute.to_s).concat('_attributes')
|
38
|
+
end
|
39
|
+
end
|
metadata
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: wrap_attributes
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- tanmen
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-12-15 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 6.0.1
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 6.0.1
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: sqlite3
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: bundler
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: convert request parameter to suffix '_attributes'
|
70
|
+
email:
|
71
|
+
- yt.prog@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- MIT-LICENSE
|
77
|
+
- README.md
|
78
|
+
- Rakefile
|
79
|
+
- lib/tasks/wrap_attributes_tasks.rake
|
80
|
+
- lib/wrap_attributes.rb
|
81
|
+
- lib/wrap_attributes/attributes_wrapper.rb
|
82
|
+
- lib/wrap_attributes/converter.rb
|
83
|
+
- lib/wrap_attributes/railtie.rb
|
84
|
+
- lib/wrap_attributes/version.rb
|
85
|
+
homepage: https://github.com/tanmen/wrap_attributes
|
86
|
+
licenses:
|
87
|
+
- MIT
|
88
|
+
metadata: {}
|
89
|
+
post_install_message:
|
90
|
+
rdoc_options: []
|
91
|
+
require_paths:
|
92
|
+
- lib
|
93
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0'
|
103
|
+
requirements: []
|
104
|
+
rubygems_version: 3.0.3
|
105
|
+
signing_key:
|
106
|
+
specification_version: 4
|
107
|
+
summary: convert request parameter to suffix '_attributes'
|
108
|
+
test_files: []
|