underscore_params 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b3becd47ba00ca0a65525b288f3f21389cefaff0
4
+ data.tar.gz: ef05bc94877a0c4d2d49cbd3721b0d996e1c0fa0
5
+ SHA512:
6
+ metadata.gz: 3604b1c20038bb6edf1eda8e9a1d0b9ba8d93cfa88dcc9331d7536977afe6d68a6d80909b9163d62443b987bac0e7991212bbb48adcbf04dd79385253199c06f
7
+ data.tar.gz: 006bbcd6135419e32b5cef91bee0a481db49ac3aac71573be930f616d045e764d20b330f6502938f397c50aa9a30f4df64212c0fd34c57bb81552aa3ae0c4460
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem "bundler", "~> 1.5"
4
+ gem "rake"
5
+ # Specify your gem's dependencies in underscore_params.gemspec
6
+ gemspec
data/README.md ADDED
@@ -0,0 +1,40 @@
1
+ # Underscore Params
2
+
3
+ Forces all param keys in Rails to be underscored. eg
4
+
5
+ {'anObject' => 'value'} turns into {'an_object' => 'value'}
6
+
7
+ Will underscore all param keys including deeply nested ones.
8
+
9
+ Helpful if you do not have control over the view layer / what params are being sent to the application and don't want to complicate controllers / models with conversion logic.
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ gem 'underscore_params'
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install underscore_params
24
+
25
+ ## Usage
26
+
27
+ * Just reference in Gemfile
28
+ * Works for Rails 4+ (not tested on earlier versions)
29
+
30
+ ## Contributing
31
+
32
+ 1. Fork it ( http://github.com/ansonK/underscore_params/fork )
33
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
34
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
35
+ 4. Push to the branch (`git push origin my-new-feature`)
36
+ 5. Create new Pull Request
37
+
38
+ ## License
39
+
40
+ Underscore Params is released under the [MIT License](http://www.opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rake/testtask'
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.libs.push "lib"
7
+ t.test_files = FileList['test/*_test.rb']
8
+ t.verbose = true
9
+ end
10
+
11
+ task :default => :test
@@ -0,0 +1,43 @@
1
+ module ActionDispatch
2
+ module Http
3
+
4
+ #
5
+ # Overrides the default params method on ActionDispatch::Request to underscore all keys
6
+ # (request.params is aliased to parameters)
7
+ #
8
+ # Should be included into ActionDispatch::Request after the standard Rails modules
9
+ #
10
+ module UnderscoreParams
11
+
12
+ #
13
+ # Processes parameters by underscoring all hash keys
14
+ #
15
+ def parameters
16
+ underscore_keys super
17
+ end
18
+
19
+ private
20
+
21
+ #
22
+ # Underscores all key values in both Hashes and Hashes in arrays.
23
+ # Other values are not modified
24
+ #
25
+ # @param value [Object] The value to be processed
26
+ #
27
+ def underscore_keys(value)
28
+ case value
29
+ when Array
30
+ value.map { |v| underscore_keys(v) }
31
+ when Hash
32
+ Hash[value.map { |k, v| [underscore_key(k), underscore_keys(v)] }]
33
+ else
34
+ value
35
+ end
36
+ end
37
+
38
+ def underscore_key(val)
39
+ val.to_s.underscore
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,9 @@
1
+ module UnderscoreParams
2
+ class Railtie < Rails::Railtie
3
+ initializer "underscore_params.configure_controller" do |app|
4
+ ActiveSupport.on_load :action_controller do
5
+ ActionDispatch::Request.send :include, ActionDispatch::Http::UnderscoreParams
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module UnderscoreParams
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,8 @@
1
+ require 'active_support/core_ext/string/inflections'
2
+ require "underscore_params/version"
3
+ require "underscore_params/action_dispatch/http/underscore_params"
4
+ require 'underscore_params/railtie' if defined?(Rails)
5
+
6
+ module UnderscoreParams
7
+ # Your code goes here...
8
+ end
@@ -0,0 +1,58 @@
1
+ require 'minitest'
2
+ require 'minitest/spec'
3
+ require 'minitest/autorun'
4
+ require 'underscore_params'
5
+
6
+ #
7
+ # Mymics the setup of how ActionDispatch::Request has a Parameters module included
8
+ #
9
+ module UnderscoreTest
10
+ module Parameters
11
+ def parameters
12
+ parameter_values
13
+ end
14
+ end
15
+
16
+ class TestController
17
+ attr_accessor :parameter_values
18
+ include Parameters
19
+ include ActionDispatch::Http::UnderscoreParams
20
+ end
21
+ end
22
+
23
+ describe ActionDispatch::Http::UnderscoreParams do
24
+ before do
25
+ @controller = UnderscoreTest::TestController.new
26
+ end
27
+
28
+ it 'underscores camelCase keys' do
29
+ @controller.parameter_values = {'firstName' => 'Bob'}
30
+ @controller.parameters.must_equal({'first_name' => 'Bob'})
31
+ end
32
+
33
+ it 'underscores camelCase keys in nested objects' do
34
+ @controller.parameter_values = {'objName' => {'firstName' => 'Bob'} }
35
+ @controller.parameters.must_equal({'obj_name' => {'first_name' => 'Bob'}})
36
+ end
37
+
38
+ it 'underscores camelCase keys in deeply nested objects' do
39
+ @controller.parameter_values = {"type"=>"Birth",
40
+ "text"=>"This is when I was born",
41
+ "date"=>"1980-01-01",
42
+ "locations"=>{},
43
+ "persons"=>
44
+ {"person"=>
45
+ {"firstName"=>"Bob",
46
+ "middleNames"=>"Billy Joe"
47
+ }
48
+ }
49
+ }
50
+
51
+ @controller.parameters['persons']['person'].must_equal({'first_name' => 'Bob', 'middle_names' => 'Billy Joe'})
52
+ end
53
+
54
+ it 'underscores keys nested in arrays' do
55
+ @controller.parameter_values = {'objName' => [{'firstName' => 'Bob'}] }
56
+ @controller.parameters.must_equal({'obj_name' => [{'first_name' => 'Bob'}]})
57
+ end
58
+ end
@@ -0,0 +1,22 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'underscore_params/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "underscore_params"
8
+ spec.version = UnderscoreParams::VERSION
9
+ spec.authors = ["Anson Kelly"]
10
+ spec.email = ["ansonkelly@gmail.com"]
11
+ spec.summary = %q{Forces all rails request params to be underscored.}
12
+ spec.description = %q{Forces all rails request params (both GET and POST) to be underscored.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "activesupport", '>= 3'
22
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: underscore_params
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Anson Kelly
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-06-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '3'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '3'
27
+ description: Forces all rails request params (both GET and POST) to be underscored.
28
+ email:
29
+ - ansonkelly@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".gitignore"
35
+ - Gemfile
36
+ - README.md
37
+ - Rakefile
38
+ - lib/underscore_params.rb
39
+ - lib/underscore_params/action_dispatch/http/underscore_params.rb
40
+ - lib/underscore_params/railtie.rb
41
+ - lib/underscore_params/version.rb
42
+ - test/underscore_params_test.rb
43
+ - underscore_params.gemspec
44
+ homepage: ''
45
+ licenses:
46
+ - MIT
47
+ metadata: {}
48
+ post_install_message:
49
+ rdoc_options: []
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ requirements: []
63
+ rubyforge_project:
64
+ rubygems_version: 2.2.2
65
+ signing_key:
66
+ specification_version: 4
67
+ summary: Forces all rails request params to be underscored.
68
+ test_files:
69
+ - test/underscore_params_test.rb