Project

General

Profile

« Previous | Next » 

Revision cc739ab4

Added by Joseph Magen over 10 years ago

fixes #3011 - default root node name for api collections json response to :results but make it configurable

View differences:

test/functional/api/v2/locations_controller_test.rb
# TODO was 500 since there is not include Authorization in taxonomy.rb
@location = taxonomies(:location1)
@location.organization_ids = [taxonomies(:organization1).id]
Rabl.configuration.use_controller_name_as_json_root = false
end
......
assert_equal assigns(:locations), [taxonomies(:location1)]
end
#####################
# test config/initializers/rabl_init.rb
# using Location as class to test rabl extension
test "root name on index should be results by default" do
get :index, {}
response = ActiveSupport::JSON.decode(@response.body)
assert response.kind_of?(Hash)
assert response['results'].kind_of?(Array)
refute response['locations']
end
test "root name on index is configured to be controller name" do
Rabl.configuration.use_controller_name_as_json_root = true
get :index, {}
response = ActiveSupport::JSON.decode(@response.body)
assert response.kind_of?(Hash)
refute response['results']
assert response['locations'].kind_of?(Array)
end
test "root name on index can be overwritten by param root_name" do
get :index, {:root_name => "data"}
response = ActiveSupport::JSON.decode(@response.body)
assert response.kind_of?(Hash)
assert response['data'].kind_of?(Array)
refute response['results']
refute response['locations']
end
test "on index no object_root name for each element in array" do
get :index, {}
response = ActiveSupport::JSON.decode(@response.body)
assert response.kind_of?(Hash)
assert response['results'].kind_of?(Array)
assert_equal ['id', 'name'], response['results'][0].keys.sort
end
test "object name on show defaults to object class name" do
obj = taxonomies(:location1)
get :show, {:id => obj.id}
response = ActiveSupport::JSON.decode(@response.body)
assert response.kind_of?(Hash)
klass_name = obj.class.name.downcase
assert "location", klass_name
assert response[klass_name].kind_of?(Hash)
assert_equal obj.id, response[klass_name]["id"]
end
test "object name on show can be specified" do
obj = taxonomies(:location1)
get :show, {:id => obj.id, :object_name => 'row'}
response = ActiveSupport::JSON.decode(@response.body)
assert response.kind_of?(Hash)
assert response['row'].kind_of?(Hash)
assert_equal obj.id, response['row']["id"]
end
test "no object name on show" do
obj = taxonomies(:location1)
get :show, {:id => obj.id, :object_name => 'false'}
response = ActiveSupport::JSON.decode(@response.body)
assert response.kind_of?(Hash)
assert_equal obj.id, response["id"]
end
end

Also available in: Unified diff