Sha256: 4d04fbb141d152858c2aa4e3a5cbd2de8ffea7e2e8f3bcce93c332b9ffffd218

Contents?: true

Size: 1.6 KB

Versions: 15

Compression:

Stored size: 1.6 KB

Contents

# frozen_string_literal: true

module Spree
  module Core
    module ControllerHelpers
      # Spree::Core::ControllerHelpers::Order decorator to inject Flow.io Experience into the order, if such an
      # Experience is present in the Order's zone
      module FlowIoOrderHelperDecorator
        private

        def adjust_zone_and_ip
          update_meta = @current_order.zone_id ? nil : true
          @current_order.zone = current_zone

          if @current_order.zone&.flow_io_active_experience?
            if @current_order.flow_io_experience_key.nil?
              @current_order.flow_io_experience_from_zone
              update_meta ||= true
            end
          end

          if @current_order.new_record?
            @current_order.last_ip_address = ip_address
            return @current_order.save
          end

          attrs_to_update = {}

          # Update last_ip_address only for a new request_id
          attrs_to_update = { last_ip_address: ip_address } if @request_id != request_id

          # :meta is a jsonb column costly to update every time, especially with all the flow.io data, that's why
          # here it is updated only if no zone_id there was inside :meta
          attrs_to_update[:meta] = @current_order.meta.to_json if update_meta

          @current_order.update_columns(attrs_to_update) if attrs_to_update.present?
          attrs_to_update
        end

        def request_id
          @request_id ||= env['action_dispatch.request_id']
        end

        ApplicationController.prepend(self) if ApplicationController.included_modules.exclude?(self)
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
flowcommerce_spree-0.0.18 app/helpers/spree/core/controller_helpers/flow_io_order_helper_decorator.rb
flowcommerce_spree-0.0.17 app/helpers/spree/core/controller_helpers/flow_io_order_helper_decorator.rb
flowcommerce_spree-0.0.16 app/helpers/spree/core/controller_helpers/flow_io_order_helper_decorator.rb
flowcommerce_spree-0.0.15 app/helpers/spree/core/controller_helpers/flow_io_order_helper_decorator.rb
flowcommerce_spree-0.0.14 app/helpers/spree/core/controller_helpers/flow_io_order_helper_decorator.rb
flowcommerce_spree-0.0.13 app/helpers/spree/core/controller_helpers/flow_io_order_helper_decorator.rb
flowcommerce_spree-0.0.12 app/helpers/spree/core/controller_helpers/flow_io_order_helper_decorator.rb
flowcommerce_spree-0.0.11 app/helpers/spree/core/controller_helpers/flow_io_order_helper_decorator.rb
flowcommerce_spree-0.0.10 app/helpers/spree/core/controller_helpers/flow_io_order_helper_decorator.rb
flowcommerce_spree-0.0.9 app/helpers/spree/core/controller_helpers/flow_io_order_helper_decorator.rb
flowcommerce_spree-0.0.8 app/helpers/spree/core/controller_helpers/flow_io_order_helper_decorator.rb
flowcommerce_spree-0.0.7 app/helpers/spree/core/controller_helpers/flow_io_order_helper_decorator.rb
flowcommerce_spree-0.0.6 app/helpers/spree/core/controller_helpers/flow_io_order_helper_decorator.rb
flowcommerce_spree-0.0.5 app/helpers/spree/core/controller_helpers/flow_io_order_helper_decorator.rb
flowcommerce_spree-0.0.4 app/helpers/spree/core/controller_helpers/flow_io_order_helper_decorator.rb