params_cleaner 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/params_cleaner.rb +36 -9
  2. metadata +6 -6
@@ -5,15 +5,14 @@ require "active_support/hash_with_indifferent_access"
5
5
  module ParamsCleaner
6
6
  extend ActiveSupport::Concern
7
7
 
8
- VERSION = "0.1.2"
8
+ VERSION = "0.2.0"
9
9
 
10
- def clean_params(root_params = params)
10
+ def clean_params(root_params = params, top_level = true)
11
11
  cleaned_params = root_params.map do |key, val|
12
12
  if val.kind_of?(Hash)
13
- clean_values = clean_params(val.slice(*self.class._allowed_params[key.to_sym]))
14
- [key, clean_values]
13
+ _clean_hash(key, val)
15
14
  else
16
- [key, val]
15
+ _clean_value(key, val, top_level)
17
16
  end
18
17
  end
19
18
 
@@ -21,13 +20,41 @@ module ParamsCleaner
21
20
  HashWithIndifferentAccess.new(cleaned_params_hash)
22
21
  end
23
22
 
23
+ def _clean_hash(key, value)
24
+ allowed_keys = value.slice(*self.class._allowed_nested[key.to_sym])
25
+ clean_values = clean_params(allowed_keys, false)
26
+ [key, clean_values]
27
+ end
28
+
29
+ def _clean_value(key, value, top_level)
30
+ return [key, value] unless top_level
31
+
32
+ if self.class._allowed_top_level.include?(key)
33
+ [key, value]
34
+ else
35
+ []
36
+ end
37
+ end
38
+
24
39
  module ClassMethods
25
- def allowed_params(params_hash)
26
- @allowed_params = params_hash
40
+ def allowed_params(*params_groups)
41
+ @allowed_top_level = []
42
+
43
+ params_groups.each do |params_group|
44
+ if params_group.is_a?(Hash)
45
+ @allowed_nested = params_group
46
+ else
47
+ @allowed_top_level << params_group
48
+ end
49
+ end
50
+ end
51
+
52
+ def _allowed_nested
53
+ @allowed_nested
27
54
  end
28
55
 
29
- def _allowed_params
30
- @allowed_params
56
+ def _allowed_top_level
57
+ @allowed_top_level
31
58
  end
32
59
  end
33
60
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: params_cleaner
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-04-10 00:00:00.000000000 Z
12
+ date: 2012-05-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
16
- requirement: &70285576499760 !ruby/object:Gem::Requirement
16
+ requirement: &70105385011580 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: 3.0.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70285576499760
24
+ version_requirements: *70105385011580
25
25
  description: Rails mass assignment protection in the controller
26
26
  email:
27
27
  - drew@drewolson.org
@@ -44,7 +44,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
44
44
  version: '0'
45
45
  segments:
46
46
  - 0
47
- hash: 4310871954395950718
47
+ hash: 1896846872677650813
48
48
  required_rubygems_version: !ruby/object:Gem::Requirement
49
49
  none: false
50
50
  requirements:
@@ -53,7 +53,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
53
53
  version: '0'
54
54
  segments:
55
55
  - 0
56
- hash: 4310871954395950718
56
+ hash: 1896846872677650813
57
57
  requirements: []
58
58
  rubyforge_project:
59
59
  rubygems_version: 1.8.15