From a5828b8e4343175b5a10281edeacdc1d94a0a2c6 Mon Sep 17 00:00:00 2001 From: John Hawthorn Date: Thu, 6 Jul 2017 16:58:30 -0700 Subject: [PATCH] Avoid running validations in current_order Previously we updated the IP address on each request by calling update(last_ip_address: ip_address) Though this doesn't run a SQL UPDATE if not necessary, it will still run validations (resulting in two SELECTs). This commit adds a new record_ip_address(ip_address) method to Spree::Order, which will only update the ip_address if it has changed. This also switches `update` to `update_attributes!`, so that any errors will be raised. --- core/app/models/spree/order.rb | 6 ++++++ core/lib/spree/core/controller_helpers/order.rb | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/core/app/models/spree/order.rb b/core/app/models/spree/order.rb index 4fd8fdd062a..b0eefa35966 100644 --- a/core/app/models/spree/order.rb +++ b/core/app/models/spree/order.rb @@ -742,6 +742,12 @@ def add_default_payment_from_wallet alias_method :assign_default_credit_card, :add_default_payment_from_wallet deprecate assign_default_credit_card: :add_default_payment_from_wallet, deprecator: Spree::Deprecation + def record_ip_address(ip_address) + if last_ip_address != ip_address + update_attributes!(last_ip_address: ip_address) + end + end + private def process_payments_before_complete diff --git a/core/lib/spree/core/controller_helpers/order.rb b/core/lib/spree/core/controller_helpers/order.rb index a96051a7d83..3f7fa10639e 100644 --- a/core/lib/spree/core/controller_helpers/order.rb +++ b/core/lib/spree/core/controller_helpers/order.rb @@ -45,7 +45,7 @@ def current_order(options = {}) end if @current_order - @current_order.update(last_ip_address: ip_address) + @current_order.record_ip_address(ip_address) return @current_order end end