snake-eyes 0.0.4 → 0.0.5

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: 762d71b565366e41521d8f983bcd79c8b59ac88f
4
- data.tar.gz: 791a4488473c8d7f6c420f62535d66850d47062f
3
+ metadata.gz: 5fae63d42cba9d54bdc028a5faf8d63d7b48b238
4
+ data.tar.gz: b0abda0615edcd1d1b266755015e2e249c41950d
5
5
  SHA512:
6
- metadata.gz: c92ef14af8e407beb2a802a35740d89f6e2f5455641d9c1e98680a82ee1af4f8119912c22fedf98845c018319ebc19fb8d0094ecb68e64f8517b85ec3bb8e693
7
- data.tar.gz: 8298771aa45a3ed17ac71d5bc6a81a4ee54bc683d7a08977c36260e3b65efed8fb511417646da44f32c75df9b454e8154d91a78df7c21ca4bce68b5e33efe014
6
+ metadata.gz: 377e94c1eb0a2f12c477a89a661eeda7529f38d86147814c5ca412847b64bedca2d36b78e857d73104a9e7f6980f0ea050d1b689ff828ed2d10b8281152d4a44
7
+ data.tar.gz: 1f23f3f3a5661bb09b63c24f1b4ebaeb056884087fc666e93f805312450ba23c04c0f4dbca2976522ade9c17919b07dbe08f61c137d040284659d6d90bb6f6d9
data/README.md CHANGED
@@ -20,18 +20,76 @@ And then execute:
20
20
 
21
21
  ## Usage
22
22
 
23
- To use SnakeEyes, simply add the following to the top of any controller in which you wish to have snake case parameters. All controllers that inherit from it shall also have the behaviour
23
+ To use SnakeEyes, simply add the following to the top of any controller in which you wish to have snake case parameters. All controllers that inherit from it shall also have the behaviour
24
24
 
25
25
  ```ruby
26
26
  class JsonController < ApplicationController
27
27
  snake_eyes_params
28
-
28
+
29
29
  def show
30
- #reference the params hash as normal
30
+ #reference the params hash as normal
31
31
  end
32
32
  end
33
33
  ```
34
34
 
35
+ ### Dealing with nested params
36
+
37
+ Once `snake_eyes_params` has been enabled for a controllor, `params` accepts an options hash, which can be used to specify which attributes should have the `_attributes` suffix appended.
38
+
39
+ ```ruby
40
+ class WithoutSnakeEyesController < ApplicationController
41
+
42
+ def show
43
+ params # results in:
44
+ # {
45
+ # 'user' => {
46
+ # 'name' => 'John Smith',
47
+ # 'favouriteColor' => 'blue',
48
+ # 'address' => { '123 street' },
49
+ # 'billingAddress' => { '456 road' }
50
+ # }
51
+ #}
52
+ end
53
+ end
54
+
55
+ class WithSnakeEyesController < ApplicationController
56
+ snake_eyes_params
57
+
58
+ def show
59
+ params(nested_attributes: { user: [ :address, :billing_address ] }) # results in:
60
+ # {
61
+ # 'user_attributes' => {
62
+ # 'name' => 'John Smith',
63
+ # 'favourite_color' => 'blue',
64
+ # 'address_attributes' => { '123 street' },
65
+ # 'billing_address_attributes' => { '456 road' }
66
+ # }
67
+ #}
68
+ end
69
+ end
70
+ ```
71
+
72
+ To specify nested objects that should not have the `_attributes` suffix (but contain attributes that should), you can prefix them with an underscore:
73
+
74
+
75
+ ```ruby
76
+ class WithSnakeEyesController < ApplicationController
77
+ snake_eyes_params
78
+
79
+ def show
80
+ params(nested_attributes: { _user: [ :address, :billing_address ] }) # results in:
81
+ # {
82
+ # 'user' => {
83
+ # 'name' => 'John Smith',
84
+ # 'favourite_color' => 'blue',
85
+ # 'address_attributes' => { '123 street' },
86
+ # 'billing_address_attributes' => { '456 road' }
87
+ # }
88
+ #}
89
+ end
90
+ end
91
+ ```
92
+
35
93
  ## Configuration
36
94
 
37
95
  By default SnakeEyes logs the snake case parameters to the Rails console. You can prevent this behaviour by configuring the gem:
@@ -1,17 +1,89 @@
1
1
  module SnakeEyes
2
2
  module InterfaceChanges
3
- def params
4
- unless defined? @snake_eyes_params
5
- @snake_eyes_params = ActionController::Parameters.new(super.deep_transform_keys(&:underscore))
6
-
7
- if SnakeEyes.log_snake_eyes_parameters
8
- ignored_params = ActionController::LogSubscriber::INTERNAL_PARAMS
9
- filtered_params = request.send(:parameter_filter).filter(@snake_eyes_params.except(*ignored_params))
10
- logger.info " SnakeEyes Parameters: #{filtered_params.inspect}"
3
+ def params(options = {})
4
+ validate_options(options)
5
+
6
+ untransformed_params = super()
7
+
8
+ traverse_positions = [
9
+ untransformed_params
10
+ ]
11
+
12
+ nested_attributes = nested_attributes_hash(options[:nested_attributes])
13
+
14
+ nested_attributes_positions = [
15
+ nested_attributes
16
+ ]
17
+
18
+ transformed_params = untransformed_params.deep_transform_keys do |key|
19
+ underscored_key = key.underscore
20
+
21
+ nested_attributes_position = nested_attributes_positions.last
22
+
23
+ transformed_key =
24
+ if nested_attributes_position[underscored_key]
25
+ underscored_key + '_attributes'
26
+ else
27
+ underscored_key
28
+ end
29
+
30
+ while traverse_positions.length > 1 && traverse_positions.last[key].nil?
31
+ traverse_positions.pop
32
+ nested_attributes_positions.pop
11
33
  end
34
+
35
+ current_position = traverse_positions.last[underscored_key]
36
+
37
+ if current_position.kind_of?(Hash)
38
+ traverse_positions.push(current_position)
39
+
40
+ nested_attributes_positions.push(
41
+ nested_attributes_position[underscored_key] || nested_attributes_position['_' + underscored_key] || {}
42
+ )
43
+ end
44
+
45
+ transformed_key
12
46
  end
13
47
 
48
+ @snake_eyes_params = ActionController::Parameters.new(transformed_params)
49
+
50
+ log_snakized_params
51
+
14
52
  @snake_eyes_params
15
53
  end
54
+
55
+ private
56
+
57
+ def validate_options(options)
58
+ options.keys.each do |key|
59
+ raise ArgumentError.new("SnakeEyes: params received unrecognised option '#{key}'") if key != :nested_attributes
60
+ end
61
+ end
62
+
63
+ def log_snakized_params
64
+ if SnakeEyes.log_snake_eyes_parameters
65
+ ignored_params = ActionController::LogSubscriber::INTERNAL_PARAMS
66
+ filtered_params = request.send(:parameter_filter).filter(@snake_eyes_params.except(*ignored_params))
67
+ logger.info " SnakeEyes Parameters: #{filtered_params.inspect}"
68
+ end
69
+ end
70
+
71
+ def nested_attributes_hash(attributes_list = [])
72
+
73
+ if attributes_list.kind_of?(Array)
74
+ attributes_list.inject({}) do |memo, nested_attribute|
75
+ memo.merge(nested_attributes_hash(nested_attribute))
76
+ end
77
+ elsif attributes_list.kind_of?(Hash)
78
+ attributes_list.inject({}) do |memo, key_and_value|
79
+ key, value = key_and_value
80
+ memo[key.to_s] = nested_attributes_hash(value)
81
+ memo
82
+ end
83
+ else
84
+ { attributes_list.to_s.underscore => {} }
85
+ end
86
+
87
+ end
16
88
  end
17
89
  end
@@ -1,3 +1,3 @@
1
1
  module SnakeEyes
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: snake-eyes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aleck Greenham
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-05-29 00:00:00.000000000 Z
11
+ date: 2017-06-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler