From 19018c2564ae9eef32827c7dc60799c65d8de92c Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Mon, 18 Jul 2022 16:53:03 +1000 Subject: [PATCH] Split address transformation for easier reading --- .../api/v1/customers_controller.rb | 43 +---------------- .../concerns/address_transformation.rb | 47 +++++++++++++++++++ 2 files changed, 49 insertions(+), 41 deletions(-) create mode 100644 app/controllers/concerns/address_transformation.rb diff --git a/app/controllers/api/v1/customers_controller.rb b/app/controllers/api/v1/customers_controller.rb index b1bfd6c51d..f1fcdddbf3 100644 --- a/app/controllers/api/v1/customers_controller.rb +++ b/app/controllers/api/v1/customers_controller.rb @@ -5,6 +5,8 @@ require 'open_food_network/permissions' module Api module V1 class CustomersController < Api::V1::BaseController + include AddressTransformation + skip_authorization_check only: :index before_action :authorize_action, only: [:show, :update, :destroy] @@ -89,47 +91,6 @@ module Api attributes end - def transform_address!(attributes, from, to) - return unless attributes.key?(from) - - address = attributes.delete(from) - - if address.nil? - attributes[to] = nil - return - end - - address.transform_keys! do |key| - { - phone: :phone, latitude: :latitude, longitude: :longitude, - first_name: :firstname, last_name: :lastname, - street_address_1: :address1, street_address_2: :address2, - postal_code: :zipcode, - locality: :city, - region: :state, - country: :country, - }.with_indifferent_access[key] - end - - if address[:state].present? - address[:state] = Spree::State.find_by( - "LOWER(abbr) = ? OR LOWER(name) = ?", - address.dig(:state, :code)&.downcase, - address.dig(:state, :name)&.downcase, - ) - end - - if address[:country].present? - address[:country] = Spree::Country.find_by( - "LOWER(iso) = ? OR LOWER(name) = ?", - address.dig(:country, :code)&.downcase, - address.dig(:country, :name)&.downcase, - ) - end - - attributes["#{to}_attributes"] = address - end - def editable_enterprises OpenFoodNetwork::Permissions.new(current_api_user).editable_enterprises.select(:id) end diff --git a/app/controllers/concerns/address_transformation.rb b/app/controllers/concerns/address_transformation.rb new file mode 100644 index 0000000000..dd55f5b529 --- /dev/null +++ b/app/controllers/concerns/address_transformation.rb @@ -0,0 +1,47 @@ +# frozen_string_literal: true + +# Our internal data structures are different to the API data strurctures. +module AddressTransformation + extend ActiveSupport::Concern + + def transform_address!(attributes, from, to) + return unless attributes.key?(from) + + address = attributes.delete(from) + + if address.nil? + attributes[to] = nil + return + end + + address.transform_keys! do |key| + { + phone: :phone, latitude: :latitude, longitude: :longitude, + first_name: :firstname, last_name: :lastname, + street_address_1: :address1, street_address_2: :address2, + postal_code: :zipcode, + locality: :city, + region: :state, + country: :country, + }.with_indifferent_access[key] + end + + if address[:state].present? + address[:state] = Spree::State.find_by( + "LOWER(abbr) = ? OR LOWER(name) = ?", + address.dig(:state, :code)&.downcase, + address.dig(:state, :name)&.downcase, + ) + end + + if address[:country].present? + address[:country] = Spree::Country.find_by( + "LOWER(iso) = ? OR LOWER(name) = ?", + address.dig(:country, :code)&.downcase, + address.dig(:country, :name)&.downcase, + ) + end + + attributes["#{to}_attributes"] = address + end +end