active_params 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE +21 -0
- data/README.md +37 -0
- data/Rakefile +10 -0
- data/active_params.gemspec +24 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/active_params/version.rb +3 -0
- data/lib/active_params.rb +86 -0
- metadata +96 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: fe7115f8e738b09dca4fa6b8db9579d78ad047c0
|
4
|
+
data.tar.gz: 531896b41459dd0ec27bfb8643c7a2e183fb508d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 218017f0012a0a7787e05f63d5fe8bf07d61f70029de377142b2607e11aa8eaab239957a2899985cc59ff20ff4c466c1ec09496ed73606dc81dcad7a26ba987d
|
7
|
+
data.tar.gz: f05f615aa69f9be8c7747ac355a759cf776ba665f93a8c359bc20659b7c4c44754bffaf0f5d296e19d7e4a5204a4562889e46b24e2845f6bbb810d150c60e85c
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Chew Choon Keat
|
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 all
|
13
|
+
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 THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# ActiveParams
|
2
|
+
|
3
|
+
Stop manually defining `strong_parameters` in each and every controller.
|
4
|
+
|
5
|
+
Automatically record the necessary `strong_parameters` settings when you use your app during development mode.
|
6
|
+
|
7
|
+
The `strong_parameters` will automatically apply.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'active_params'
|
15
|
+
```
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install active_params
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
Include the `ActiveParams` module into your controller, e.g.
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
class ApplicationController < ActionController::Base
|
31
|
+
include ActiveParams
|
32
|
+
end
|
33
|
+
```
|
34
|
+
|
35
|
+
## LICENSE
|
36
|
+
|
37
|
+
MIT
|
data/Rakefile
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'active_params/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "active_params"
|
8
|
+
spec.version = ActiveParams::VERSION
|
9
|
+
spec.authors = ["choonkeat"]
|
10
|
+
spec.email = ["choonkeat@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Automatic strong parameters}
|
13
|
+
spec.description = %q{Bye bye strong parameters falls}
|
14
|
+
spec.homepage = "http://github.com/choonkeat/active_params"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = "exe"
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.12"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
spec.add_development_dependency "minitest"
|
24
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "active_params"
|
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
|
data/bin/setup
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
require "active_params/version"
|
2
|
+
|
3
|
+
module ActiveParams
|
4
|
+
def write_strong_params(global_config: ActiveParams.global_config)
|
5
|
+
current_config = global_config["#{request.method} #{controller_name}/#{action_name}"] ||= {}
|
6
|
+
params.each do |k,v|
|
7
|
+
case result = ActiveParams.strong_params_definition_for(v)
|
8
|
+
when Array, Hash
|
9
|
+
current_config[k] = result
|
10
|
+
end
|
11
|
+
end
|
12
|
+
open(ActiveParams.path, "wb") {|f| f.write(JSON.pretty_generate(global_config).tap {|s| logger.info s }) }
|
13
|
+
yield
|
14
|
+
end
|
15
|
+
|
16
|
+
def apply_strong_params(global_config: ActiveParams.global_config)
|
17
|
+
# e.g. POST users#update
|
18
|
+
current_config = global_config["#{request.method} #{controller_name}/#{action_name}"] ||= {}
|
19
|
+
params.each do |k,v|
|
20
|
+
if result = current_config[k]
|
21
|
+
params[k] = params.require(k).permit(result)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
yield
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.included(base)
|
28
|
+
if base.methods.include? :around_action
|
29
|
+
base.class_eval do
|
30
|
+
around_action :write_strong_params if ActiveParams.development_mode?
|
31
|
+
around_action :apply_strong_params
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.development_mode?
|
37
|
+
defined?(Rails) && Rails.env.development?
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.path
|
41
|
+
ENV.fetch("ACTIVE_PARAMS_PATH", "config/active_params.json")
|
42
|
+
end
|
43
|
+
|
44
|
+
def self.global_config
|
45
|
+
@@global_config ||= (File.exists?(ActiveParams.path) ? JSON.parse(IO.read ActiveParams.path) : {})
|
46
|
+
ensure
|
47
|
+
# undo cache in development mode
|
48
|
+
@@global_config = nil if development_mode?
|
49
|
+
end
|
50
|
+
|
51
|
+
# to obtain a hash of all possible keys
|
52
|
+
def self.combine_hashes(array_of_hashes)
|
53
|
+
array_of_hashes.select {|v| v.kind_of?(Hash) }.
|
54
|
+
inject({}) {|sum, hash| hash.inject(sum) {|sum,(k,v)| sum.merge(k => v) } }
|
55
|
+
end
|
56
|
+
|
57
|
+
def self.strong_params_definition_for(params)
|
58
|
+
if params.kind_of?(Array)
|
59
|
+
combined_hash = combine_hashes(params)
|
60
|
+
if combined_hash.empty?
|
61
|
+
[]
|
62
|
+
else
|
63
|
+
strong_params_definition_for(combined_hash)
|
64
|
+
end
|
65
|
+
elsif params.respond_to?(:keys) # Hash, ActionController::Parameters
|
66
|
+
values, arrays = [[], {}]
|
67
|
+
params.each do |key, value|
|
68
|
+
case value
|
69
|
+
when Array
|
70
|
+
arrays[key] = strong_params_definition_for(value)
|
71
|
+
when Hash
|
72
|
+
arrays[key] = strong_params_definition_for(combine_hashes(value.values))
|
73
|
+
else
|
74
|
+
values.push(key)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
if arrays.empty?
|
78
|
+
values
|
79
|
+
else
|
80
|
+
[*values.sort, arrays]
|
81
|
+
end
|
82
|
+
else
|
83
|
+
params
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
metadata
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: active_params
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- choonkeat
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-06-25 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.12'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.12'
|
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: minitest
|
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
|
+
description: Bye bye strong parameters falls
|
56
|
+
email:
|
57
|
+
- choonkeat@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".travis.yml"
|
64
|
+
- Gemfile
|
65
|
+
- LICENSE
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- active_params.gemspec
|
69
|
+
- bin/console
|
70
|
+
- bin/setup
|
71
|
+
- lib/active_params.rb
|
72
|
+
- lib/active_params/version.rb
|
73
|
+
homepage: http://github.com/choonkeat/active_params
|
74
|
+
licenses: []
|
75
|
+
metadata: {}
|
76
|
+
post_install_message:
|
77
|
+
rdoc_options: []
|
78
|
+
require_paths:
|
79
|
+
- lib
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
requirements: []
|
91
|
+
rubyforge_project:
|
92
|
+
rubygems_version: 2.5.1
|
93
|
+
signing_key:
|
94
|
+
specification_version: 4
|
95
|
+
summary: Automatic strong parameters
|
96
|
+
test_files: []
|