From 667e62a68be48b36148b9018a6fe82250914231b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Bu=CC=88nemann?= Date: Fri, 4 Mar 2016 18:11:03 +0100 Subject: [PATCH 1/2] Speed up countries endpoint --- app/controllers/spree/api/ams/countries_controller.rb | 5 +++-- app/serializers/spree/state_serializer.rb | 7 +++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/app/controllers/spree/api/ams/countries_controller.rb b/app/controllers/spree/api/ams/countries_controller.rb index 2dfad4a..2f88c55 100644 --- a/app/controllers/spree/api/ams/countries_controller.rb +++ b/app/controllers/spree/api/ams/countries_controller.rb @@ -7,11 +7,12 @@ class CountriesController < Spree::Api::CountriesController # We don't need this paginated. def index - @countries = Country.accessible_by(current_ability, :read).order('name ASC') + @countries = Country.accessible_by(current_ability, :read) + .includes(:states).order(:name) respond_with @countries end end end end -end \ No newline at end of file +end diff --git a/app/serializers/spree/state_serializer.rb b/app/serializers/spree/state_serializer.rb index ec59f71..820c951 100644 --- a/app/serializers/spree/state_serializer.rb +++ b/app/serializers/spree/state_serializer.rb @@ -4,9 +4,8 @@ class StateSerializer < ActiveModel::Serializer attributes :id, :name, - :abbr - - has_one :country, include: false + :abbr, + :country_id end -end \ No newline at end of file +end From 58af6e816abea97635944610eef264a32d5fab6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Bu=CC=88nemann?= Date: Fri, 4 Mar 2016 18:39:51 +0100 Subject: [PATCH 2/2] Allow selective loading of countries by ISO 3166-2 Example: `curl http://localhost:3000/api/ams/countries\?isos\=DE,CH,AT` --- app/controllers/spree/api/ams/countries_controller.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/app/controllers/spree/api/ams/countries_controller.rb b/app/controllers/spree/api/ams/countries_controller.rb index 2f88c55..c4cdd20 100644 --- a/app/controllers/spree/api/ams/countries_controller.rb +++ b/app/controllers/spree/api/ams/countries_controller.rb @@ -9,6 +9,12 @@ class CountriesController < Spree::Api::CountriesController def index @countries = Country.accessible_by(current_ability, :read) .includes(:states).order(:name) + + isos = params[:isos] + isos = isos.split ',' if isos.is_a?(String) + + @countries = @countries.where(iso: isos) if isos.present? + respond_with @countries end