snake-eyes 1.0.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/Rakefile +7 -5
  4. data/lib/snake-eyes.rb +6 -6
  5. data/lib/snake_eyes/compatibility.rb +19 -0
  6. data/lib/snake_eyes/configuration.rb +53 -0
  7. data/lib/{snake-eyes → snake_eyes}/engine.rb +4 -4
  8. data/lib/snake_eyes/interface_changes.rb +49 -0
  9. data/lib/snake_eyes/logging.rb +19 -0
  10. data/lib/snake_eyes/memoization.rb +27 -0
  11. data/lib/snake_eyes/transform.rb +124 -0
  12. data/lib/snake_eyes/version.rb +5 -0
  13. data/spec/dummy/Rakefile +3 -1
  14. data/spec/dummy/app/controllers/application_controller.rb +2 -0
  15. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  16. data/spec/dummy/app/views/layouts/application.html.erb +1 -1
  17. data/spec/dummy/bin/bundle +3 -1
  18. data/spec/dummy/bin/rails +3 -1
  19. data/spec/dummy/bin/rake +2 -0
  20. data/spec/dummy/bin/setup +10 -8
  21. data/spec/dummy/config.ru +2 -0
  22. data/spec/dummy/config/application.rb +4 -3
  23. data/spec/dummy/config/boot.rb +4 -2
  24. data/spec/dummy/config/environment.rb +3 -1
  25. data/spec/dummy/config/environments/development.rb +6 -3
  26. data/spec/dummy/config/environments/production.rb +2 -0
  27. data/spec/dummy/config/environments/test.rb +3 -1
  28. data/spec/dummy/config/initializers/assets.rb +4 -1
  29. data/spec/dummy/config/initializers/backtrace_silencers.rb +6 -2
  30. data/spec/dummy/config/initializers/cookies_serializer.rb +2 -0
  31. data/spec/dummy/config/initializers/filter_parameter_logging.rb +2 -0
  32. data/spec/dummy/config/initializers/inflections.rb +2 -0
  33. data/spec/dummy/config/initializers/mime_types.rb +2 -0
  34. data/spec/dummy/config/initializers/session_store.rb +2 -0
  35. data/spec/dummy/config/initializers/wrap_parameters.rb +2 -0
  36. data/spec/dummy/config/routes.rb +2 -1
  37. data/spec/dummy/log/test.log +5615 -0
  38. data/spec/nested_attributes_spec.rb +116 -118
  39. data/spec/params_spec.rb +107 -106
  40. data/spec/spec_helper.rb +5 -3
  41. data/spec/substitutions_spec.rb +202 -172
  42. metadata +27 -8
  43. data/lib/snake-eyes/interface_changes.rb +0 -187
  44. data/lib/snake-eyes/version.rb +0 -3
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: 1.0.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aleck Greenham
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-01-17 00:00:00.000000000 Z
11
+ date: 2019-03-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: 4.2.5
27
+ - !ruby/object:Gem::Dependency
28
+ name: awesome_print
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: rspec
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -39,7 +53,7 @@ dependencies:
39
53
  - !ruby/object:Gem::Version
40
54
  version: '0'
41
55
  - !ruby/object:Gem::Dependency
42
- name: sqlite3
56
+ name: rspec-rails
43
57
  requirement: !ruby/object:Gem::Requirement
44
58
  requirements:
45
59
  - - ">="
@@ -53,7 +67,7 @@ dependencies:
53
67
  - !ruby/object:Gem::Version
54
68
  version: '0'
55
69
  - !ruby/object:Gem::Dependency
56
- name: awesome_print
70
+ name: rubocop
57
71
  requirement: !ruby/object:Gem::Requirement
58
72
  requirements:
59
73
  - - ">="
@@ -67,7 +81,7 @@ dependencies:
67
81
  - !ruby/object:Gem::Version
68
82
  version: '0'
69
83
  - !ruby/object:Gem::Dependency
70
- name: rspec-rails
84
+ name: sqlite3
71
85
  requirement: !ruby/object:Gem::Requirement
72
86
  requirements:
73
87
  - - ">="
@@ -92,9 +106,14 @@ files:
92
106
  - README.md
93
107
  - Rakefile
94
108
  - lib/snake-eyes.rb
95
- - lib/snake-eyes/engine.rb
96
- - lib/snake-eyes/interface_changes.rb
97
- - lib/snake-eyes/version.rb
109
+ - lib/snake_eyes/compatibility.rb
110
+ - lib/snake_eyes/configuration.rb
111
+ - lib/snake_eyes/engine.rb
112
+ - lib/snake_eyes/interface_changes.rb
113
+ - lib/snake_eyes/logging.rb
114
+ - lib/snake_eyes/memoization.rb
115
+ - lib/snake_eyes/transform.rb
116
+ - lib/snake_eyes/version.rb
98
117
  - spec/dummy/README.rdoc
99
118
  - spec/dummy/Rakefile
100
119
  - spec/dummy/app/assets/javascripts/application.js
@@ -1,187 +0,0 @@
1
- module SnakeEyes
2
- module InterfaceChanges
3
- KEYS_ALWAYS_PRESENT = [
4
- "controller",
5
- "action"
6
- ]
7
-
8
- if Rails::VERSION::MAJOR >= 5
9
- def _prepare(transformed_params)
10
- # We permit all parameter values so that we many convert it to a hash, to work
11
- # with ActionPack 5.2's ActionController::Parameters initializer
12
- _transform_params = transformed_params.dup
13
- _transform_params.permit!
14
- _transform_params.to_h
15
- end
16
- else
17
- def _prepare(transformed_params)
18
- transformed_params
19
- end
20
- end
21
-
22
- def params(options = {})
23
- validate_options(options)
24
-
25
- original_params = super()
26
-
27
- params_present = (original_params.keys | KEYS_ALWAYS_PRESENT).length > KEYS_ALWAYS_PRESENT.length
28
-
29
- unless params_present
30
- return original_params
31
- end
32
-
33
- @previous_params ||= { }
34
-
35
- nested_schema = build_options_schema(options[:nested_attributes] || {}, '') do |target, parent_name|
36
- if parent_name.empty? || parent_name.starts_with?('_')
37
- target
38
- else
39
- target.merge({ _attributes_suffix: true })
40
- end
41
- end
42
-
43
- options[:nested_attributes] = nested_schema
44
-
45
- return @previous_params[options] if @previous_params[options]
46
-
47
- transformed_params = deep_transform(_prepare(original_params), options)
48
-
49
- @previous_params[options] =
50
- @snake_eyes_params = ActionController::Parameters.new(transformed_params)
51
-
52
- log_snakized_params
53
-
54
- @snake_eyes_params
55
- end
56
-
57
- private
58
-
59
- def validate_options(options)
60
- options.keys.each do |key|
61
- raise ArgumentError.new("SnakeEyes: params received unrecognised option '#{key}'") if key != :nested_attributes && key != :substitutions
62
- end
63
- end
64
-
65
- def log_snakized_params
66
- if SnakeEyes.log_snake_eyes_parameters
67
- ignored_params = ActionController::LogSubscriber::INTERNAL_PARAMS
68
- filtered_params = request.send(:parameter_filter).filter(@snake_eyes_params.except(*ignored_params))
69
-
70
- logger.info " SnakeEyes Parameters: #{_prepare(filtered_params).inspect}"
71
- end
72
- end
73
-
74
- def deep_transform(target, options = {})
75
-
76
- nested_attributes = options[:nested_attributes] || {}
77
-
78
- substitutions =
79
- if options[:substitutions].kind_of?(Array)
80
- options[:substitutions].map(&:stringify_keys)
81
- else
82
- (options[:substitutions] || {}).stringify_keys
83
- end
84
-
85
- if target.kind_of?(Array)
86
- target.map do |targetElement|
87
- deep_transform(targetElement, {
88
- nested_attributes: nested_attributes['*'],
89
- substitutions: substitutions.kind_of?(Array) ? {} : substitutions['*']
90
- })
91
- end
92
-
93
- elsif target.kind_of?(Hash)
94
-
95
- target.inject({}) do |memo, key_and_value|
96
- key, value = key_and_value
97
-
98
- # Append the '_attributes' suffix if the original params key has the
99
- # same name and is nested in the same place as one mentioned in the
100
- # nested_attributes option
101
-
102
- transformed_key_base = key.to_s.underscore
103
-
104
- transformed_key =
105
- if nested_attributes[transformed_key_base] && nested_attributes[transformed_key_base][:_attributes_suffix]
106
- transformed_key_base + '_attributes'
107
- else
108
- transformed_key_base
109
- end
110
-
111
- transformed_value = deep_transform(value,
112
- {
113
- nested_attributes: nested_attributes[transformed_key_base] || nested_attributes['_' + transformed_key_base],
114
- substitutions: substitutions.kind_of?(Array) ? {} : substitutions[transformed_key_base]
115
- }
116
- )
117
-
118
- memo[transformed_key] = transformed_value
119
-
120
- memo
121
- end
122
-
123
- else
124
- perform_substitution(target, substitutions)
125
- end
126
-
127
- end
128
-
129
- def perform_substitution(target, substitution)
130
- if substitution.kind_of?(Array)
131
- matching_substitution = substitution.find do |substitution_item|
132
- has_substitution_keys?(substitution_item) && target === substitution_item["replace"]
133
- end
134
-
135
- if matching_substitution
136
- matching_substitution["with"]
137
- else
138
- target
139
- end
140
-
141
- else
142
- if has_substitution_keys?(substitution)
143
- target === substitution["replace"] ? substitution["with"] : target
144
- else
145
- target
146
- end
147
- end
148
- end
149
-
150
- def has_substitution_keys?(substitution)
151
- substitution.has_key?("replace") && substitution.has_key?("with")
152
- end
153
-
154
- def build_options_schema(attributes_list = [], parent_name = '', options = {}, &block)
155
-
156
- if attributes_list.kind_of?(Array)
157
- attributes_array = attributes_list.inject({}) do |memo, nested_attribute|
158
- memo.merge(build_options_schema(nested_attribute, parent_name, options, &block))
159
- end
160
-
161
- yield(attributes_array, parent_name)
162
-
163
- elsif attributes_list.kind_of?(Hash) && (!options[:internal_attributes] || (attributes_list.keys & options[:internal_attributes]).length > options[:internal_attributes].length)
164
-
165
- attributes_hash = attributes_list.inject({}) do |memo, key_and_value|
166
- key, value = key_and_value
167
-
168
- memo[key.to_s] = yield(build_options_schema(value, '', options, &block), key.to_s)
169
-
170
- memo
171
- end
172
-
173
- yield(attributes_hash, parent_name)
174
- else
175
-
176
- {
177
- attributes_list.to_s.underscore => yield({}, attributes_list.to_s.underscore)
178
- }
179
-
180
- end
181
-
182
- end
183
-
184
-
185
- end
186
-
187
- end
@@ -1,3 +0,0 @@
1
- module SnakeEyes
2
- VERSION = "1.0.0"
3
- end