active_params 0.1.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 47ecec5d8833f17831a4d8ca416714fc356cd4b8
4
- data.tar.gz: 8b0641f760e01cf63a9c7d2c35e16f1039451276
3
+ metadata.gz: 89e4ec3b12048b26afb3516aad053d9bd624b77c
4
+ data.tar.gz: b036a0b06d54324fc28335d3ad106a4772af290d
5
5
  SHA512:
6
- metadata.gz: 92a4f7043edb9dae18ac1d9ab9d6f94a902c7e6339364318ee2d0792c9afbeb4744cbfc1f35967d19e8416c25bc81733d125a50444a827010524a3aeb1296940
7
- data.tar.gz: 5de694ee1858bac35b7c2548748d2946cd7f3989d2c0589f914fb3e7b4c1727d3930696f2a3aba4a1752e332e2d20cfa74e5350756f0a17c0dcece141e94e5f3
6
+ metadata.gz: 2dfc110db66b78be2d862e8e41f4e621e680d7028c78256087349f413fc2bf0a5bc9a99e22986ea600fcac47fe5db5c3964caaede6e687f380ebeba7f52cae53
7
+ data.tar.gz: d821671fb2ea4db41c0cd0fd10473e635739e0f1c81b05d0b2fc062291f740b5cdea9597b1b25d815ad0f4661ba59d662677b548aad9e8e408bb1117e2cc47ba
data/README.md CHANGED
@@ -91,6 +91,41 @@ end
91
91
 
92
92
  When you create new features for your app & try them out in development mode, `config/active_params.json` will be automatically updated. When you commit your code, include the changes to `config/active_params.json` too.
93
93
 
94
+ ### Static Customizations
95
+
96
+ You can add a `config/initializers/active_params.rb` file in your Rails app, and perform a global config like this
97
+
98
+ ```ruby
99
+ ActiveParams.config do |config|
100
+ config.writing = Rails.env.development?
101
+ config.path = "config/active_params.json"
102
+ config.scope = proc { |controller|
103
+ "#{controller.request.method} #{controller.controller_name}/#{controller.action_name}"
104
+ }
105
+ end
106
+ ```
107
+
108
+ ### Dynamic Customizations
109
+
110
+ Each controller may need its own config, e.g. some strong params are only permitted for certain current_user roles. For such controllers, use the `include ActiveParams.setup(...)` syntax instead
111
+
112
+ ```ruby
113
+ class ApplicationController < ActionController::Base
114
+ include ActiveParams.setup({
115
+ path: "tmp/strong_params.json",
116
+ writing: !Rails.env.production?,
117
+ scope: proc {|ctrl|
118
+ [ctrl.current_user.role, ActiveParams.scope.(ctrl)].join('@')
119
+ },
120
+ })
121
+ end
122
+ ```
123
+
124
+ You can setup
125
+ - where the config file is stored
126
+ - if you want to write to the config file
127
+ - how should strong params be scoped
128
+
94
129
  ## LICENSE
95
130
 
96
131
  MIT
@@ -1,86 +1,38 @@
1
1
  require "active_params/version"
2
+ require "active_params/parser"
3
+ require "active_params/controller"
2
4
 
3
5
  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)) }
13
- yield
14
- end
6
+ class << self
7
+ attr_accessor :writing, :path, :scope
15
8
 
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
9
+ # ActiveParams.config {|c| .... }
10
+ def config
11
+ yield self
23
12
  end
24
- yield
25
- end
26
13
 
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
14
+ # `include ActiveParams` use default settings
15
+ def included(base)
16
+ base.send(:include, setup)
33
17
  end
34
- end
35
18
 
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)
19
+ # `include ActiveParams.setup({...})` customize
20
+ def setup(options = {})
21
+ Module.new do
22
+ def self.included(base)
23
+ base.send(:include, ActiveParams::Controller)
75
24
  end
25
+ end.tap do |m|
26
+ m.send(:define_method, :active_params_options) { options }
76
27
  end
77
- if arrays.empty?
78
- values
79
- else
80
- [*values.sort, arrays]
81
- end
82
- else
83
- params
84
28
  end
85
29
  end
86
30
  end
31
+
32
+ ActiveParams.config do |config|
33
+ config.writing = defined?(Rails) && Rails.env.development?
34
+ config.path = "config/active_params.json"
35
+ config.scope = proc { |controller|
36
+ "#{controller.request.method} #{controller.controller_name}/#{controller.action_name}"
37
+ }
38
+ end
@@ -0,0 +1,61 @@
1
+ require "json"
2
+
3
+ module ActiveParams
4
+ module Controller
5
+ def self.included(base)
6
+ add_before_method =
7
+ (base.methods.include?(:before_action) && :before_action) ||
8
+ (base.methods.include?(:before_filter) && :before_filter)
9
+
10
+ if add_before_method
11
+ base.class_eval do
12
+ send add_before_method, :active_params_write, if: :active_params_writing?
13
+ send add_before_method, :active_params_apply
14
+ end
15
+ end
16
+ end
17
+
18
+ def active_params_writing?
19
+ active_params_options[:writing] || ActiveParams.writing
20
+ end
21
+
22
+ def active_params_path
23
+ active_params_options[:path] || ActiveParams.path
24
+ end
25
+
26
+ def active_params_json
27
+ @@active_params_json ||= (File.exists?(active_params_path) ? JSON.parse(IO.read active_params_path) : {})
28
+ rescue JSON::ParserError
29
+ return {}
30
+ ensure
31
+ # undo cache in development mode
32
+ @@active_params_json = nil if active_params_writing?
33
+ end
34
+
35
+ def active_params_scope
36
+ scope = active_params_options[:scope] || ActiveParams.scope
37
+ scope.respond_to?(:call) ? scope.(self) : scope
38
+ end
39
+
40
+ def active_params_write(global_json: active_params_json)
41
+ scoped_json = global_json[active_params_scope] ||= {}
42
+ params.each do |k,v|
43
+ case result = ActiveParams::Parser.strong_params_definition_for(v)
44
+ when Array, Hash
45
+ scoped_json[k] = result
46
+ end
47
+ end
48
+ open(active_params_path, "wb") {|f| f.write(JSON.pretty_generate(global_json)) }
49
+ end
50
+
51
+ def active_params_apply(global_json: active_params_json)
52
+ # e.g. POST users#update
53
+ scoped_json = global_json[active_params_scope] ||= {}
54
+ params.each do |k,v|
55
+ if result = scoped_json[k]
56
+ params[k] = params.require(k).permit(result)
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,41 @@
1
+ module ActiveParams
2
+ module Parser
3
+ # to obtain a hash of all possible keys
4
+ def combine_hashes(array_of_hashes)
5
+ array_of_hashes.select {|v| v.kind_of?(Hash) }.
6
+ inject({}) {|sum, hash| hash.inject(sum) {|sum,(k,v)| sum.merge(k => v) } }
7
+ end
8
+
9
+ def strong_params_definition_for(params)
10
+ if params.kind_of?(Array)
11
+ combined_hash = combine_hashes(params)
12
+ if combined_hash.empty?
13
+ []
14
+ else
15
+ strong_params_definition_for(combined_hash)
16
+ end
17
+ elsif params.respond_to?(:keys) # Hash, ActionController::Parameters
18
+ values, arrays = [[], {}]
19
+ params.each do |key, value|
20
+ case value
21
+ when Array
22
+ arrays[key] = strong_params_definition_for(value)
23
+ when Hash
24
+ arrays[key] = strong_params_definition_for(combine_hashes(value.values))
25
+ else
26
+ values.push(key)
27
+ end
28
+ end
29
+ if arrays.empty?
30
+ values
31
+ else
32
+ [*values.sort, arrays]
33
+ end
34
+ else
35
+ params
36
+ end
37
+ end
38
+
39
+ self.extend(self)
40
+ end
41
+ end
@@ -1,3 +1,3 @@
1
1
  module ActiveParams
2
- VERSION = "0.1.1"
2
+ VERSION = "1.0.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_params
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - choonkeat
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-06-27 00:00:00.000000000 Z
11
+ date: 2016-12-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -69,6 +69,8 @@ files:
69
69
  - bin/console
70
70
  - bin/setup
71
71
  - lib/active_params.rb
72
+ - lib/active_params/controller.rb
73
+ - lib/active_params/parser.rb
72
74
  - lib/active_params/version.rb
73
75
  homepage: http://github.com/choonkeat/active_params
74
76
  licenses: []
@@ -89,7 +91,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
89
91
  version: '0'
90
92
  requirements: []
91
93
  rubyforge_project:
92
- rubygems_version: 2.5.1
94
+ rubygems_version: 2.6.7
93
95
  signing_key:
94
96
  specification_version: 4
95
97
  summary: Automatic strong parameters