stark_parameters 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.bundle/install.log +29 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +48 -0
- data/Rakefile +1 -0
- data/lib/stark_parameters/version.rb +3 -0
- data/lib/stark_parameters.rb +73 -0
- data/spec/lib/stark_parameters_spec.rb +52 -0
- data/spec/spec_helper.rb +2 -0
- data/stark_parameters.gemspec +24 -0
- metadata +106 -0
data/.bundle/install.log
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# Logfile created on 2014-03-11 14:48:55 +0100 by logger.rb/31641
|
2
|
+
I, [2014-03-11T14:48:55.511655 #30778] INFO -- : 0: rake (10.1.1) from /Users/ryanlevick/.rbenv/versions/1.9.3-p385/lib/ruby/gems/1.9.1/specifications/rake-10.1.1.gemspec
|
3
|
+
I, [2014-03-11T14:48:55.512455 #30778] INFO -- : 0: i18n (0.6.9) from /Users/ryanlevick/.rbenv/versions/1.9.3-p385/lib/ruby/gems/1.9.1/specifications/i18n-0.6.9.gemspec
|
4
|
+
I, [2014-03-11T14:48:55.513137 #30778] INFO -- : 0: multi_json (1.9.0) from /Users/ryanlevick/.rbenv/versions/1.9.3-p385/lib/ruby/gems/1.9.1/specifications/multi_json-1.9.0.gemspec
|
5
|
+
I, [2014-03-11T14:48:57.734092 #30778] INFO -- : 0: activesupport (3.2.17) from /Users/ryanlevick/.rbenv/versions/1.9.3-p385/lib/ruby/gems/1.9.1/specifications/activesupport-3.2.17.gemspec
|
6
|
+
I, [2014-03-11T14:48:57.734649 #30778] INFO -- : 0: builder (3.0.4) from /Users/ryanlevick/.rbenv/versions/1.9.3-p385/lib/ruby/gems/1.9.1/specifications/builder-3.0.4.gemspec
|
7
|
+
I, [2014-03-11T14:48:59.627458 #30778] INFO -- : 0: activemodel (3.2.17) from /Users/ryanlevick/.rbenv/versions/1.9.3-p385/lib/ruby/gems/1.9.1/specifications/activemodel-3.2.17.gemspec
|
8
|
+
I, [2014-03-11T14:48:59.628237 #30778] INFO -- : 0: erubis (2.7.0) from /Users/ryanlevick/.rbenv/versions/1.9.3-p385/lib/ruby/gems/1.9.1/specifications/erubis-2.7.0.gemspec
|
9
|
+
I, [2014-03-11T14:48:59.628729 #30778] INFO -- : 0: journey (1.0.4) from /Users/ryanlevick/.rbenv/versions/1.9.3-p385/lib/ruby/gems/1.9.1/specifications/journey-1.0.4.gemspec
|
10
|
+
I, [2014-03-11T14:48:59.629323 #30778] INFO -- : 0: rack (1.4.5) from /Users/ryanlevick/.rbenv/versions/1.9.3-p385/lib/ruby/gems/1.9.1/specifications/rack-1.4.5.gemspec
|
11
|
+
I, [2014-03-11T14:48:59.630046 #30778] INFO -- : 0: rack-cache (1.2) from /Users/ryanlevick/.rbenv/versions/1.9.3-p385/lib/ruby/gems/1.9.1/specifications/rack-cache-1.2.gemspec
|
12
|
+
I, [2014-03-11T14:48:59.630637 #30778] INFO -- : 0: rack-test (0.6.2) from /Users/ryanlevick/.rbenv/versions/1.9.3-p385/lib/ruby/gems/1.9.1/specifications/rack-test-0.6.2.gemspec
|
13
|
+
I, [2014-03-11T14:48:59.631292 #30778] INFO -- : 0: hike (1.2.3) from /Users/ryanlevick/.rbenv/versions/1.9.3-p385/lib/ruby/gems/1.9.1/specifications/hike-1.2.3.gemspec
|
14
|
+
I, [2014-03-11T14:48:59.631972 #30778] INFO -- : 0: tilt (1.4.1) from /Users/ryanlevick/.rbenv/versions/1.9.3-p385/lib/ruby/gems/1.9.1/specifications/tilt-1.4.1.gemspec
|
15
|
+
I, [2014-03-11T14:48:59.632640 #30778] INFO -- : 0: sprockets (2.2.2) from /Users/ryanlevick/.rbenv/versions/1.9.3-p385/lib/ruby/gems/1.9.1/specifications/sprockets-2.2.2.gemspec
|
16
|
+
I, [2014-03-11T14:49:01.473503 #30778] INFO -- : 0: actionpack (3.2.17) from /Users/ryanlevick/.rbenv/versions/1.9.3-p385/lib/ruby/gems/1.9.1/specifications/actionpack-3.2.17.gemspec
|
17
|
+
I, [2014-03-11T14:49:01.474060 #30778] INFO -- : 0: diff-lcs (1.2.5) from /Users/ryanlevick/.rbenv/versions/1.9.3-p385/lib/ruby/gems/1.9.1/specifications/diff-lcs-1.2.5.gemspec
|
18
|
+
I, [2014-03-11T14:49:01.474420 #30778] INFO -- : 0: json (1.8.1) from /Users/ryanlevick/.rbenv/versions/1.9.3-p385/lib/ruby/gems/1.9.1/specifications/json-1.8.1.gemspec
|
19
|
+
I, [2014-03-11T14:49:01.474753 #30778] INFO -- : 0: rack-ssl (1.3.3) from /Users/ryanlevick/.rbenv/versions/1.9.3-p385/lib/ruby/gems/1.9.1/specifications/rack-ssl-1.3.3.gemspec
|
20
|
+
I, [2014-03-11T14:49:01.475088 #30778] INFO -- : 0: rdoc (3.12.2) from /Users/ryanlevick/.rbenv/versions/1.9.3-p385/lib/ruby/gems/1.9.1/specifications/rdoc-3.12.2.gemspec
|
21
|
+
I, [2014-03-11T14:49:01.475418 #30778] INFO -- : 0: thor (0.18.1) from /Users/ryanlevick/.rbenv/versions/1.9.3-p385/lib/ruby/gems/1.9.1/specifications/thor-0.18.1.gemspec
|
22
|
+
I, [2014-03-11T14:49:03.970720 #30778] INFO -- : 0: railties (3.2.17) from /Users/ryanlevick/.rbenv/versions/1.9.3-p385/lib/ruby/gems/1.9.1/specifications/railties-3.2.17.gemspec
|
23
|
+
I, [2014-03-11T14:49:05.795065 #30778] INFO -- : 0: rspec-core (2.14.8) from /Users/ryanlevick/.rbenv/versions/1.9.3-p385/lib/ruby/gems/1.9.1/specifications/rspec-core-2.14.8.gemspec
|
24
|
+
I, [2014-03-11T14:49:05.795698 #30778] INFO -- : 0: rspec-expectations (2.14.5) from /Users/ryanlevick/.rbenv/versions/1.9.3-p385/lib/ruby/gems/1.9.1/specifications/rspec-expectations-2.14.5.gemspec
|
25
|
+
I, [2014-03-11T14:49:05.796160 #30778] INFO -- : 0: rspec-mocks (2.14.6) from /Users/ryanlevick/.rbenv/versions/1.9.3-p385/lib/ruby/gems/1.9.1/specifications/rspec-mocks-2.14.6.gemspec
|
26
|
+
I, [2014-03-11T14:49:05.796544 #30778] INFO -- : 0: rspec (2.14.1) from /Users/ryanlevick/.rbenv/versions/1.9.3-p385/lib/ruby/gems/1.9.1/specifications/rspec-2.14.1.gemspec
|
27
|
+
I, [2014-03-11T14:49:05.796917 #30778] INFO -- : 0: strong_parameters (0.2.3) from /Users/ryanlevick/.rbenv/versions/1.9.3-p385/lib/ruby/gems/1.9.1/specifications/strong_parameters-0.2.3.gemspec
|
28
|
+
I, [2014-03-11T14:49:05.813289 #30778] INFO -- : 0: stark parameters (0.0.1) from /Users/ryanlevick/Sites/personal/stark-parameters/stark_parameters.gemspec
|
29
|
+
I, [2014-03-11T14:49:05.813731 #30778] INFO -- : 0: bundler (1.5.1) from /Users/ryanlevick/.rbenv/versions/1.9.3-p385/lib/ruby/gems/1.9.1/specifications/bundler-1.5.1.gemspec
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Ryan Levick
|
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,48 @@
|
|
1
|
+
# StarkParameters
|
2
|
+
|
3
|
+
## Usage
|
4
|
+
|
5
|
+
Include `StarkParameters` in any class with which you want to be able to validate params.
|
6
|
+
|
7
|
+
You can require, permit and alias params.
|
8
|
+
|
9
|
+
StarkParameters uses [Strong Parameters](https://github.com/rails/strong_parameters)
|
10
|
+
under the hood so missing require params also raise an `ActionController::ParameterMissing` error.
|
11
|
+
Also nested permits work just as they do in Strong Parameters
|
12
|
+
|
13
|
+
## Example
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
# Rails Controller
|
17
|
+
|
18
|
+
class UsersContoller < ApplicationController
|
19
|
+
def index
|
20
|
+
@users = User.find_by(UserIndexParams.new(params).params)
|
21
|
+
end
|
22
|
+
|
23
|
+
def create
|
24
|
+
@user = User.create!(UserCreateParams.new(params).params)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
class UserIndexParams
|
29
|
+
include StarkParameters
|
30
|
+
|
31
|
+
require :email, as: :email_id
|
32
|
+
end
|
33
|
+
|
34
|
+
class UserCreateParams
|
35
|
+
include StarkParameters
|
36
|
+
|
37
|
+
require :email, as: :email_id
|
38
|
+
permit :name, as: name_id
|
39
|
+
require [:facebook_id, :google_id]
|
40
|
+
end
|
41
|
+
```
|
42
|
+
|
43
|
+
In the index action an email would be required but would be passed to `find_by`
|
44
|
+
as `email_id`.
|
45
|
+
|
46
|
+
In the create action an `email` would be required but passed in as `email_id`,
|
47
|
+
name would be permitted and passed as `name_id` if present, and either a
|
48
|
+
`facebook_id` or a `google_id` would be required.
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,73 @@
|
|
1
|
+
module StarkParameters
|
2
|
+
|
3
|
+
def self.included(klass)
|
4
|
+
klass.send :extend, ClassMethods
|
5
|
+
klass.required_params = []
|
6
|
+
klass.permitted_params = []
|
7
|
+
klass.aliases = {}
|
8
|
+
end
|
9
|
+
|
10
|
+
def initialize(params)
|
11
|
+
@params = make_strong_parameter(params)
|
12
|
+
end
|
13
|
+
|
14
|
+
def params
|
15
|
+
make_strong_parameter(permitted_params.merge(required_params))
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def permitted_params
|
21
|
+
self.class.permitted_params.each_with_object({}) do |permitted_param, hash|
|
22
|
+
param_key = permitted_param.is_a?(Hash) ? permitted_param.keys.first : permitted_param
|
23
|
+
permitted_value = @params.permit(permitted_param).values.first
|
24
|
+
hash[(self.class.aliases[param_key] || param_key).to_s] = permitted_value if permitted_value
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def required_params
|
29
|
+
self.class.required_params.each_with_object({}) do |required_params, hash|
|
30
|
+
param_key = require_one(required_params)
|
31
|
+
hash[(self.class.aliases[param_key] || param_key).to_s] = @params[param_key]
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
|
36
|
+
def make_strong_parameter(hash)
|
37
|
+
return hash if hash.is_a? ActionController::Parameters
|
38
|
+
ActionController::Parameters.new(hash)
|
39
|
+
end
|
40
|
+
|
41
|
+
def require_one(to_require)
|
42
|
+
present_param = to_require.detect {|p| @params.include?(p) }
|
43
|
+
unless present_param
|
44
|
+
raise ActionController::ParameterMissing.new(to_require.join(" or "))
|
45
|
+
end
|
46
|
+
present_param
|
47
|
+
end
|
48
|
+
|
49
|
+
module ClassMethods
|
50
|
+
attr_accessor :required_params, :permitted_params, :aliases
|
51
|
+
|
52
|
+
def require(required_params, options = {})
|
53
|
+
required_params = Array(required_params)
|
54
|
+
if new_name = options[:as]
|
55
|
+
required_params.each { |rp| @aliases[rp] = new_name }
|
56
|
+
end
|
57
|
+
@required_params.push required_params
|
58
|
+
end
|
59
|
+
|
60
|
+
def permit(permitted_param, options = {})
|
61
|
+
if new_name = options[:as]
|
62
|
+
param_key = if permitted_param.is_a? Hash
|
63
|
+
permitted_param.keys.first
|
64
|
+
else
|
65
|
+
permitted_param
|
66
|
+
end
|
67
|
+
|
68
|
+
@aliases[param_key] = new_name
|
69
|
+
end
|
70
|
+
@permitted_params.push permitted_param
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe StarkParameters do
|
4
|
+
let(:test_klass) do
|
5
|
+
Class.new do
|
6
|
+
include StarkParameters
|
7
|
+
|
8
|
+
require :name
|
9
|
+
require :id
|
10
|
+
require :email, as: :login
|
11
|
+
permit author: [:id, :name]
|
12
|
+
permit :password, as: :pword
|
13
|
+
require [:last_name, :surname]
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
let(:full_params) do
|
18
|
+
{
|
19
|
+
"id" => 1,
|
20
|
+
"name" => "Ryan",
|
21
|
+
"email" => "ryan@6wunderkinder.com",
|
22
|
+
"password" => "fdsafdsa",
|
23
|
+
"surname" => "Levick",
|
24
|
+
"author" => { "id" => 1, "name" => "Steve" }
|
25
|
+
}
|
26
|
+
end
|
27
|
+
|
28
|
+
let(:validator) { test_klass.new(params) }
|
29
|
+
|
30
|
+
context "when all params are there" do
|
31
|
+
let(:params) { full_params }
|
32
|
+
|
33
|
+
it { expect(validator.params.to_hash).to include("name" => "Ryan") }
|
34
|
+
it { expect(validator.params.to_hash).to include("surname" => "Levick") }
|
35
|
+
it { expect(validator.params.to_hash).to include("author" => { "id" => 1, "name" => "Steve" }) }
|
36
|
+
it { expect(validator.params.to_hash).to include("login" => "ryan@6wunderkinder.com") }
|
37
|
+
it { expect(validator.params.to_hash.keys).to_not include("email") }
|
38
|
+
end
|
39
|
+
|
40
|
+
context "when required param is missing" do
|
41
|
+
let(:params) { full_params.except("name") }
|
42
|
+
|
43
|
+
it { expect{ validator.params }.to raise_error(ActionController::ParameterMissing) }
|
44
|
+
end
|
45
|
+
|
46
|
+
context "when permitted param is missing" do
|
47
|
+
let(:params) { full_params.except("author") }
|
48
|
+
|
49
|
+
it { expect{ validator.params }.to_not raise_error }
|
50
|
+
it { expect(validator.params.to_hash.keys).to_not include("author") }
|
51
|
+
end
|
52
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'stark_parameters/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "stark_parameters"
|
8
|
+
gem.version = StarkParameters::VERSION
|
9
|
+
gem.authors = ["rylev"]
|
10
|
+
gem.email = ["ryan.levick@gmail.com"]
|
11
|
+
gem.description = %q{Making Strong Parameters Stark}
|
12
|
+
gem.summary = %q{Wraps Strong Parameters with additonal functionality}
|
13
|
+
gem.homepage = ""
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.add_dependency 'strong_parameters'
|
21
|
+
|
22
|
+
gem.add_development_dependency 'rake'
|
23
|
+
gem.add_development_dependency 'rspec'
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: stark_parameters
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- rylev
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2014-03-12 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: strong_parameters
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rspec
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
description: Making Strong Parameters Stark
|
63
|
+
email:
|
64
|
+
- ryan.levick@gmail.com
|
65
|
+
executables: []
|
66
|
+
extensions: []
|
67
|
+
extra_rdoc_files: []
|
68
|
+
files:
|
69
|
+
- .bundle/install.log
|
70
|
+
- .gitignore
|
71
|
+
- Gemfile
|
72
|
+
- LICENSE.txt
|
73
|
+
- README.md
|
74
|
+
- Rakefile
|
75
|
+
- lib/stark_parameters.rb
|
76
|
+
- lib/stark_parameters/version.rb
|
77
|
+
- spec/lib/stark_parameters_spec.rb
|
78
|
+
- spec/spec_helper.rb
|
79
|
+
- stark_parameters.gemspec
|
80
|
+
homepage: ''
|
81
|
+
licenses: []
|
82
|
+
post_install_message:
|
83
|
+
rdoc_options: []
|
84
|
+
require_paths:
|
85
|
+
- lib
|
86
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
87
|
+
none: false
|
88
|
+
requirements:
|
89
|
+
- - ! '>='
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
93
|
+
none: false
|
94
|
+
requirements:
|
95
|
+
- - ! '>='
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
requirements: []
|
99
|
+
rubyforge_project:
|
100
|
+
rubygems_version: 1.8.23
|
101
|
+
signing_key:
|
102
|
+
specification_version: 3
|
103
|
+
summary: Wraps Strong Parameters with additonal functionality
|
104
|
+
test_files:
|
105
|
+
- spec/lib/stark_parameters_spec.rb
|
106
|
+
- spec/spec_helper.rb
|