Skip to content

Commit

Permalink
Merge pull request #2388 from zapnap/hash-helper-symbolize
Browse files Browse the repository at this point in the history
Move hash symbolizer into a helper, prevent namespace conflicts
  • Loading branch information
sferik committed Aug 19, 2015
2 parents 447e33b + 684346c commit dbff2cf
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 32 deletions.
2 changes: 1 addition & 1 deletion lib/rails_admin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/rails_admin/config/actions/export.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 0 additions & 30 deletions lib/rails_admin/support/core_extensions.rb

This file was deleted.

28 changes: 28 additions & 0 deletions lib/rails_admin/support/hash_helper.rb
Original file line number Diff line number Diff line change
@@ -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
37 changes: 37 additions & 0 deletions spec/rails_admin/support/hash_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# encoding: utf-8

require 'spec_helper'

describe RailsAdmin::HashHelper do
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) }

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

0 comments on commit dbff2cf

Please sign in to comment.