params_purifier 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/.coversall +1 -0
- data/.gitignore +22 -0
- data/.rspec +2 -0
- data/.travis.yml +15 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +46 -0
- data/Rakefile +9 -0
- data/lib/params_purifier.rb +3 -0
- data/lib/params_purifier/purifiable.rb +18 -0
- data/lib/params_purifier/purification.rb +27 -0
- data/lib/params_purifier/version.rb +3 -0
- data/params_purifier.gemspec +29 -0
- data/spec/params_purifier/purifiable_spec.rb +43 -0
- data/spec/params_purifier/purification_spec.rb +58 -0
- data/spec/spec_helper.rb +9 -0
- metadata +176 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 65ca2c00f140198093f158e342bb4fd13ed5b005
|
4
|
+
data.tar.gz: 51a3507ce09f6cec1733ec4e107a7b3be0b115d6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 387560945b02be6e5f6f1deb49ccaad66c631064813ee7d838fdfe63173edcf5ef16c181f89a42527a0e2e83fcdb9b401649da1451ee313a455ea5f678668faa
|
7
|
+
data.tar.gz: e604e044f5a2d0de0fb4db7d3b8eea7ce93b130806ebb5ed7619869f4f6b39fe4d62c3aedfa58a46c99b87d021353fdc49fe849b2b2af5dc5902bc6dcb68ee19
|
data/.coversall
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
service_name: travis-ci
|
data/.gitignore
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Karol Galanciak
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
# ParamsPurifier
|
2
|
+
|
3
|
+
Have you ever had problems with multiple select and empty strings being sent in array params? (see [discussion](http://stackoverflow.com/questions/8929230/why-is-the-first-element-always-blank-in-my-rails-multi-select-using-an-embedde/)). Now you can easily solve that problem by using ParamsPurifier!
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'params_purifier'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install params_purifier
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
Just include ParamsPurifier::Purifiable module in your controller (e.g. ApplicationController):
|
22
|
+
|
23
|
+
``` ruby
|
24
|
+
class ApplicationController < ActionController::Base
|
25
|
+
|
26
|
+
include ParamsPurifier::Purifiable
|
27
|
+
|
28
|
+
end
|
29
|
+
```
|
30
|
+
|
31
|
+
And that's it! You have an access to "purified" params by purified_params method. Works with Strong Parameters by default (it returns an instance of ActionController::Parameters):
|
32
|
+
|
33
|
+
``` ruby
|
34
|
+
def user_params
|
35
|
+
purified_params.require(:user).permit(:email, :password, :password_confirmation)
|
36
|
+
end
|
37
|
+
```
|
38
|
+
|
39
|
+
|
40
|
+
## Contributing
|
41
|
+
|
42
|
+
1. Fork it ( https://github.com/[my-github-username]/params_purifier/fork )
|
43
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
44
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
45
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
46
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'action_controller'
|
2
|
+
require 'params_purifier/purification'
|
3
|
+
|
4
|
+
module ParamsPurifier
|
5
|
+
module Purifiable
|
6
|
+
|
7
|
+
def purified_params
|
8
|
+
if respond_to? :params, true
|
9
|
+
ActionController::Parameters.new(Purification.execute(params))
|
10
|
+
else
|
11
|
+
raise ParamsMethodNotImplementedError.new("Interface must implement #params method")
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
class ParamsMethodNotImplementedError < StandardError ; end
|
16
|
+
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'active_support/core_ext'
|
2
|
+
|
3
|
+
module ParamsPurifier
|
4
|
+
module Purification
|
5
|
+
|
6
|
+
module_function
|
7
|
+
|
8
|
+
def execute(params)
|
9
|
+
purify_params(params, {})
|
10
|
+
end
|
11
|
+
|
12
|
+
def purify_params(original_params, new_params)
|
13
|
+
original_params.each_with_object(new_params) do |(key, value), purified_params|
|
14
|
+
case value
|
15
|
+
when Array
|
16
|
+
new_value = value.reject { |v| v.blank? }
|
17
|
+
when Hash
|
18
|
+
new_value = purify_params(value, {})
|
19
|
+
else
|
20
|
+
new_value = value
|
21
|
+
end
|
22
|
+
purified_params[key] = new_value
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'params_purifier/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "params_purifier"
|
8
|
+
spec.version = ParamsPurifier::VERSION
|
9
|
+
spec.authors = ["Karol Galanciak"]
|
10
|
+
spec.email = ["karol.galanciak@gmail.com"]
|
11
|
+
spec.summary = %q{Gem for removing empty strings in arrays in params.}
|
12
|
+
spec.description = %q{Gem for removing empty strings in arrays in params.}
|
13
|
+
spec.homepage = "https://github.com/Azdaroth/params_purifier"
|
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_development_dependency "bundler", "~> 1.5"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec", "~> 2.14"
|
24
|
+
spec.add_development_dependency "guard"
|
25
|
+
spec.add_development_dependency "guard-rspec"
|
26
|
+
spec.add_development_dependency "coveralls"
|
27
|
+
spec.add_dependency "activesupport", ">= 3.2"
|
28
|
+
spec.add_dependency "actionpack", ">= 3.2"
|
29
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class DummyInterFaceForPurifiableWithError
|
4
|
+
|
5
|
+
include ParamsPurifier::Purifiable
|
6
|
+
|
7
|
+
end
|
8
|
+
|
9
|
+
class DummyInterFaceForPurifiable
|
10
|
+
|
11
|
+
include ParamsPurifier::Purifiable
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def params
|
16
|
+
{ "ids" => [""] }
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
|
22
|
+
describe ParamsPurifier::Purifiable do
|
23
|
+
|
24
|
+
describe "#purified_params" do
|
25
|
+
|
26
|
+
it "raises error if interface doesn't implement params method" do
|
27
|
+
expect do
|
28
|
+
DummyInterFaceForPurifiableWithError.new.purified_params
|
29
|
+
end.to raise_error ParamsPurifier::Purifiable::ParamsMethodNotImplementedError
|
30
|
+
end
|
31
|
+
|
32
|
+
it "returns purified params" do
|
33
|
+
result = { "ids" => [] }
|
34
|
+
expect(DummyInterFaceForPurifiable.new.purified_params).to eq result
|
35
|
+
end
|
36
|
+
|
37
|
+
it "returns instance of ActionController::Parameters" do
|
38
|
+
expect(DummyInterFaceForPurifiable.new.purified_params).to be_instance_of ActionController::Parameters
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ParamsPurifier::Purification do
|
4
|
+
|
5
|
+
subject { ParamsPurifier::Purification }
|
6
|
+
|
7
|
+
it "does not change params if there are no arrays" do
|
8
|
+
params = {
|
9
|
+
"user_id" => "1",
|
10
|
+
"comment_attributes" => {
|
11
|
+
"id" => "2",
|
12
|
+
"content" => "content"
|
13
|
+
}
|
14
|
+
}
|
15
|
+
expect(subject.execute(params)).to eq params
|
16
|
+
end
|
17
|
+
|
18
|
+
it "removes empty strings from arrays - level 0 of nesting" do
|
19
|
+
params = { "user_ids" => ["", "1"], "name" => "name" }
|
20
|
+
result = { "user_ids" => ["1"], "name" => "name" }
|
21
|
+
expect(subject.execute(params)).to eq result
|
22
|
+
end
|
23
|
+
|
24
|
+
it "removes empty strings from arrays - level 1 of nesting" do
|
25
|
+
params = { "comment_attributes" => {
|
26
|
+
"post_ids" => [""],
|
27
|
+
"content" => "content"
|
28
|
+
}
|
29
|
+
}
|
30
|
+
result = { "comment_attributes" => {
|
31
|
+
"post_ids" => [],
|
32
|
+
"content" => "content"
|
33
|
+
}
|
34
|
+
}
|
35
|
+
expect(subject.execute(params)).to eq result
|
36
|
+
end
|
37
|
+
|
38
|
+
it "removes empty strings from arrays - level 2 of nesting" do
|
39
|
+
params = { "comment_attributes" => {
|
40
|
+
"some_ids" => [""],
|
41
|
+
"other_attributes" => {
|
42
|
+
"values" => ["", "value"]
|
43
|
+
},
|
44
|
+
"content" => "content"
|
45
|
+
}
|
46
|
+
}
|
47
|
+
result = { "comment_attributes" => {
|
48
|
+
"some_ids" => [],
|
49
|
+
"other_attributes" => {
|
50
|
+
"values" => ["value"]
|
51
|
+
},
|
52
|
+
"content" => "content"
|
53
|
+
}
|
54
|
+
}
|
55
|
+
expect(subject.execute(params)).to eq result
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,176 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: params_purifier
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Karol Galanciak
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-05-04 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.5'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.5'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
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: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '2.14'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '2.14'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: guard
|
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
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: guard-rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: coveralls
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: activesupport
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '3.2'
|
104
|
+
type: :runtime
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '3.2'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: actionpack
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '3.2'
|
118
|
+
type: :runtime
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '3.2'
|
125
|
+
description: Gem for removing empty strings in arrays in params.
|
126
|
+
email:
|
127
|
+
- karol.galanciak@gmail.com
|
128
|
+
executables: []
|
129
|
+
extensions: []
|
130
|
+
extra_rdoc_files: []
|
131
|
+
files:
|
132
|
+
- ".coversall"
|
133
|
+
- ".gitignore"
|
134
|
+
- ".rspec"
|
135
|
+
- ".travis.yml"
|
136
|
+
- Gemfile
|
137
|
+
- LICENSE.txt
|
138
|
+
- README.md
|
139
|
+
- Rakefile
|
140
|
+
- lib/params_purifier.rb
|
141
|
+
- lib/params_purifier/purifiable.rb
|
142
|
+
- lib/params_purifier/purification.rb
|
143
|
+
- lib/params_purifier/version.rb
|
144
|
+
- params_purifier.gemspec
|
145
|
+
- spec/params_purifier/purifiable_spec.rb
|
146
|
+
- spec/params_purifier/purification_spec.rb
|
147
|
+
- spec/spec_helper.rb
|
148
|
+
homepage: https://github.com/Azdaroth/params_purifier
|
149
|
+
licenses:
|
150
|
+
- MIT
|
151
|
+
metadata: {}
|
152
|
+
post_install_message:
|
153
|
+
rdoc_options: []
|
154
|
+
require_paths:
|
155
|
+
- lib
|
156
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
157
|
+
requirements:
|
158
|
+
- - ">="
|
159
|
+
- !ruby/object:Gem::Version
|
160
|
+
version: '0'
|
161
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
162
|
+
requirements:
|
163
|
+
- - ">="
|
164
|
+
- !ruby/object:Gem::Version
|
165
|
+
version: '0'
|
166
|
+
requirements: []
|
167
|
+
rubyforge_project:
|
168
|
+
rubygems_version: 2.2.2
|
169
|
+
signing_key:
|
170
|
+
specification_version: 4
|
171
|
+
summary: Gem for removing empty strings in arrays in params.
|
172
|
+
test_files:
|
173
|
+
- spec/params_purifier/purifiable_spec.rb
|
174
|
+
- spec/params_purifier/purification_spec.rb
|
175
|
+
- spec/spec_helper.rb
|
176
|
+
has_rdoc:
|