From df84bd9e1d115c64a20d934e03a76d351aa806b4 Mon Sep 17 00:00:00 2001 From: Devin Howard Date: Fri, 23 Sep 2016 14:39:15 +0800 Subject: [PATCH 1/2] fix @maps serialization bug if @maps is empty, it returns {"maps":[]}, instead of [] like we expect on the frontend. This commit fixes this issue --- app/controllers/maps_controller.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/app/controllers/maps_controller.rb b/app/controllers/maps_controller.rb index 7c4a74a7..22e75819 100644 --- a/app/controllers/maps_controller.rb +++ b/app/controllers/maps_controller.rb @@ -19,7 +19,7 @@ class MapsController < ApplicationController redirect_to(root_url) && return if authenticated? respond_with(@maps, @user) end - format.json { render json: @maps } + format.json { render json: @maps.to_json } end end @@ -33,7 +33,7 @@ class MapsController < ApplicationController respond_to do |format| format.html { respond_with(@maps, @user) } - format.json { render json: @maps } + format.json { render json: @maps.to_json } end end @@ -51,7 +51,7 @@ class MapsController < ApplicationController respond_to do |format| format.html { respond_with(@maps, @user) } - format.json { render json: @maps } + format.json { render json: @maps.to_json } end end @@ -69,7 +69,7 @@ class MapsController < ApplicationController respond_to do |format| format.html { respond_with(@maps, @user) } - format.json { render json: @maps } + format.json { render json: @maps.to_json } end end @@ -88,7 +88,7 @@ class MapsController < ApplicationController respond_to do |format| format.html { respond_with(@maps, @user) } - format.json { render json: @maps } + format.json { render json: @maps.to_json } end end @@ -101,7 +101,7 @@ class MapsController < ApplicationController respond_to do |format| format.html { respond_with(@maps, @user) } - format.json { render json: @maps } + format.json { render json: @maps.to_json } end end From 117b7910bf8465feb444a1399cbe15e51fa5cb74 Mon Sep 17 00:00:00 2001 From: Devin Howard Date: Fri, 23 Sep 2016 17:40:30 +0800 Subject: [PATCH 2/2] test --- spec/controllers/maps_controller_spec.rb | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/spec/controllers/maps_controller_spec.rb b/spec/controllers/maps_controller_spec.rb index 278ec559..a10c20d1 100644 --- a/spec/controllers/maps_controller_spec.rb +++ b/spec/controllers/maps_controller_spec.rb @@ -8,6 +8,21 @@ RSpec.describe MapsController, type: :controller do sign_in create(:user) end + describe 'GET #activemaps' do + context 'always returns an array' do + it 'with 0 records' do + Map.delete_all + get :activemaps, format: :json + expect(JSON.parse(response.body)).to eq [] + end + it 'with 1 record' do + map = create(:map) + get :activemaps, format: :json + expect(JSON.parse(response.body).class).to be Array + end + end + end + describe 'POST #create' do context 'with valid params' do it 'creates a new Map' do