Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move hash symbolizer into a helper, prevent namespace conflicts #2388

Merged
merged 2 commits into from
Aug 19, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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