From cfbf836a1e19433ff4858f1aeea91467848a4429 Mon Sep 17 00:00:00 2001 From: Nick Plante Date: Tue, 18 Aug 2015 14:44:32 -0400 Subject: [PATCH 1/2] Move hash symbolizer into a helper, prevent namespace conflicts --- lib/rails_admin.rb | 2 +- lib/rails_admin/config/actions/export.rb | 2 +- lib/rails_admin/support/core_extensions.rb | 30 ---------------- lib/rails_admin/support/hash_helper.rb | 28 +++++++++++++++ spec/rails_admin/support/hash_helper_spec.rb | 38 ++++++++++++++++++++ 5 files changed, 68 insertions(+), 32 deletions(-) delete mode 100644 lib/rails_admin/support/core_extensions.rb create mode 100644 lib/rails_admin/support/hash_helper.rb create mode 100644 spec/rails_admin/support/hash_helper_spec.rb diff --git a/lib/rails_admin.rb b/lib/rails_admin.rb index 24f45f535b..77431d59f5 100644 --- a/lib/rails_admin.rb +++ b/lib/rails_admin.rb @@ -7,7 +7,7 @@ require 'rails_admin/extensions/paper_trail' require 'rails_admin/extensions/history' require 'rails_admin/support/csv_converter' -require 'rails_admin/support/core_extensions' +require 'rails_admin/support/hash_helper' module RailsAdmin # Setup RailsAdmin diff --git a/lib/rails_admin/config/actions/export.rb b/lib/rails_admin/config/actions/export.rb index 9930f1ab4d..c5dc25a2fe 100644 --- a/lib/rails_admin/config/actions/export.rb +++ b/lib/rails_admin/config/actions/export.rb @@ -16,7 +16,7 @@ class Export < RailsAdmin::Config::Actions::Base proc do if format = params[:json] && :json || params[:csv] && :csv || params[:xml] && :xml request.format = format - @schema = params[:schema].symbolize if params[:schema] # to_json and to_xml expect symbols for keys AND values. + @schema = HashHelper.symbolize(params[:schema]) if params[:schema] # to_json and to_xml expect symbols for keys AND values. @objects = list_entries(@model_config, :export) index else diff --git a/lib/rails_admin/support/core_extensions.rb b/lib/rails_admin/support/core_extensions.rb deleted file mode 100644 index ff966ef99a..0000000000 --- a/lib/rails_admin/support/core_extensions.rb +++ /dev/null @@ -1,30 +0,0 @@ -class Hash - def symbolize - Hash.symbolize_hash(self) - end - - def self.symbolize_hash(obj) - case obj - when Array - obj.each_with_object([]) do |val, res| - res << case val - when Hash, Array then symbolize_hash(val) - when String then val.to_sym - else val - end - end - when Hash - obj.each_with_object({}) do |(key, val), res| - nkey = key.is_a?(String) ? key.to_sym : key - nval = case val - when Hash, Array then symbolize_hash(val) - when String then val.to_sym - else val - end - res[nkey] = nval - end - else - obj - end - end -end diff --git a/lib/rails_admin/support/hash_helper.rb b/lib/rails_admin/support/hash_helper.rb new file mode 100644 index 0000000000..c33ff426cc --- /dev/null +++ b/lib/rails_admin/support/hash_helper.rb @@ -0,0 +1,28 @@ +module RailsAdmin + class HashHelper + def self.symbolize(obj) + case obj + when Array + obj.each_with_object([]) do |val, res| + res << case val + when Hash, Array then symbolize(val) + when String then val.to_sym + else val + end + end + when Hash + obj.each_with_object({}) do |(key, val), res| + nkey = key.is_a?(String) ? key.to_sym : key + nval = case val + when Hash, Array then symbolize(val) + when String then val.to_sym + else val + end + res[nkey] = nval + end + else + obj + end + end + end +end diff --git a/spec/rails_admin/support/hash_helper_spec.rb b/spec/rails_admin/support/hash_helper_spec.rb new file mode 100644 index 0000000000..c5ed8c85a5 --- /dev/null +++ b/spec/rails_admin/support/hash_helper_spec.rb @@ -0,0 +1,38 @@ +# encoding: utf-8 + +require 'spec_helper' + +describe RailsAdmin::HashHelper do + let(:hash) { + { 'subject' => 'Test', + 'user' => { name: 'Dirk', + 'title' => 'Holistic Detective', + 'clients' => [ + { name: 'Zaphod' }, + { 'name' => 'Arthur' } + ] + }}} + + describe 'symbolize' do + let(:symbolized_hash) { RailsAdmin::HashHelper.symbolize(hash) } + + it 'symbolizes top-level hash keys' do + [:subject, :user].each do |key| + expect(symbolized_hash.keys).to include(key) + end + end + + it 'symbolizes nested hashes' do + [:name, :title, :clients].each do |key| + expect(symbolized_hash[:user].keys).to include(key) + end + end + + it 'symbolizes nested hashes inside of array values' do + clients = symbolized_hash[:user][:clients] + expect(clients.length).to eq(2) + expect(clients[0][:name]).to eq(:Zaphod) + expect(clients[1][:name]).to eq(:Arthur) + end + end +end From 684346cc03d6c63af092a8d87df3c8586869e86c Mon Sep 17 00:00:00 2001 From: Nick Plante Date: Wed, 19 Aug 2015 05:24:31 -0400 Subject: [PATCH 2/2] fix rubocop spec offenses --- spec/rails_admin/support/hash_helper_spec.rb | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/spec/rails_admin/support/hash_helper_spec.rb b/spec/rails_admin/support/hash_helper_spec.rb index c5ed8c85a5..f26fa5357f 100644 --- a/spec/rails_admin/support/hash_helper_spec.rb +++ b/spec/rails_admin/support/hash_helper_spec.rb @@ -3,15 +3,14 @@ require 'spec_helper' describe RailsAdmin::HashHelper do - let(:hash) { - { 'subject' => 'Test', - 'user' => { name: 'Dirk', - 'title' => 'Holistic Detective', - 'clients' => [ - { name: 'Zaphod' }, - { 'name' => 'Arthur' } - ] - }}} + let(:hash) do + {'subject' => 'Test', + 'user' => {name: 'Dirk', + 'title' => 'Holistic Detective', + 'clients' => [ + {name: 'Zaphod'}, + {'name' => 'Arthur'}]}} + end describe 'symbolize' do let(:symbolized_hash) { RailsAdmin::HashHelper.symbolize(hash) }