Skip to content

Commit

Permalink
Wrap commonly-repeated calls to Spree::Config to reduce unnecessary c…
Browse files Browse the repository at this point in the history
…ache reads

These config values are relatively static but in some cases they can be called many times in the same request (like rendering a report or a large list of line_items in BOM). These values will now only get fetched from Redis/Postgres once at most per request/job.
  • Loading branch information
Matt-Yorkley committed Mar 23, 2024
1 parent 26f3b56 commit 6d7f02c
Show file tree
Hide file tree
Showing 16 changed files with 47 additions and 30 deletions.
2 changes: 1 addition & 1 deletion app/helpers/admin/injection_helper.rb
Expand Up @@ -162,7 +162,7 @@ def admin_inject_spree_api_key(spree_api_key)
def admin_inject_available_units
admin_inject_json "admin.products",
"availableUnits",
Spree::Config.available_units
CurrentConfig.get(:available_units)
end

def admin_inject_json(ng_module, name, data)
Expand Down
17 changes: 17 additions & 0 deletions app/models/current_config.rb
@@ -0,0 +1,17 @@
# frozen_string_literal: true

# Wraps repeatedly-called configs in a CurrentAttributes object so they only get fetched once
# per request at most, eg: CurrentConfig.get(:available_units) for Spree::Config[:available_units]

class CurrentConfig < ActiveSupport::CurrentAttributes
attribute :display_currency, :hide_cents, :currency_decimal_mark,
:currency_thousands_separator, :currency_symbol_position, :available_units

def get(config_key)
self.public_send(config_key) || self.public_send("#{config_key}=", Spree::Config[config_key])
end

def currency
ENV.fetch("CURRENCY")
end
end
2 changes: 1 addition & 1 deletion app/models/spree/adjustment.rb
Expand Up @@ -120,7 +120,7 @@ def update_adjustment!(calculable = nil, force: false)
end

def currency
adjustable ? adjustable.currency : Spree::Config[:currency]
adjustable ? adjustable.currency : CurrentConfig.get(:currency)
end

def display_amount
Expand Down
4 changes: 2 additions & 2 deletions app/models/spree/order.rb
Expand Up @@ -186,7 +186,7 @@ def pre_discount_total
end

def currency
self[:currency] || Spree::Config[:currency]
self[:currency] || CurrentConfig.get(:currency)
end

def display_item_total
Expand Down Expand Up @@ -689,7 +689,7 @@ def use_billing?
end

def set_currency
self.currency = Spree::Config[:currency] if self[:currency].nil?
self.currency = CurrentConfig.get(:currency) if self[:currency].nil?
end

def using_guest_checkout?
Expand Down
2 changes: 1 addition & 1 deletion app/models/spree/price.rb
Expand Up @@ -37,7 +37,7 @@ def price=(price)
def check_price
return unless currency.nil?

self.currency = Spree::Config[:currency]
self.currency = CurrentConfig.get(:currency)
end

# strips all non-price-like characters from the price, taking into account locale settings
Expand Down
2 changes: 1 addition & 1 deletion app/models/spree/return_authorization.rb
Expand Up @@ -29,7 +29,7 @@ class ReturnAuthorization < ApplicationRecord
end

def currency
order.nil? ? Spree::Config[:currency] : order.currency
order.nil? ? CurrentConfig.get(:currency) : order.currency
end

def display_amount
Expand Down
2 changes: 1 addition & 1 deletion app/models/spree/shipment.rb
Expand Up @@ -163,7 +163,7 @@ def find_shipping_rate_for(shipping_method_id)
end

def currency
order ? order.currency : Spree::Config[:currency]
order ? order.currency : CurrentConfig.get(:currency)
end

def display_cost
Expand Down
8 changes: 4 additions & 4 deletions app/models/spree/variant.rb
Expand Up @@ -42,7 +42,7 @@ class Variant < ApplicationRecord
accepts_nested_attributes_for :images

has_one :default_price,
-> { with_deleted.where(currency: Spree::Config[:currency]) },
-> { with_deleted.where(currency: CurrentConfig.get(:currency)) },
class_name: 'Spree::Price',
dependent: :destroy
has_many :prices,
Expand Down Expand Up @@ -161,7 +161,7 @@ def self.active(currency = nil)
where("spree_variants.id in (?)", joins(:prices).
where(deleted_at: nil).
where('spree_prices.currency' =>
currency || Spree::Config[:currency]).
currency || CurrentConfig.get(:currency)).
where.not(spree_prices: { amount: nil }).
select("spree_variants.id"))
end
Expand Down Expand Up @@ -210,7 +210,7 @@ def total_on_hand
def check_currency
return unless currency.nil?

self.currency = Spree::Config[:currency]
self.currency = CurrentConfig.get(:currency)
end

def save_default_price
Expand All @@ -222,7 +222,7 @@ def find_or_build_default_price
end

def set_cost_currency
self.cost_currency = Spree::Config[:currency] if cost_currency.blank?
self.cost_currency = CurrentConfig.get(:currency) if cost_currency.blank?
end

def create_stock_items
Expand Down
Expand Up @@ -12,7 +12,7 @@ class CustomerWithBalanceSerializer < CustomerSerializer
delegate :balance_value, to: :object

def balance
Spree::Money.new(balance_value, currency: Spree::Config[:currency]).to_s
Spree::Money.new(balance_value).to_s
end

def balance_status
Expand Down
10 changes: 5 additions & 5 deletions app/serializers/api/currency_config_serializer.rb
Expand Up @@ -4,22 +4,22 @@ class Api::CurrencyConfigSerializer < ActiveModel::Serializer
attributes :currency, :display_currency, :symbol, :symbol_position, :hide_cents

def currency
Spree::Config[:currency]
CurrentConfig.get(:currency)
end

def display_currency
Spree::Config[:display_currency]
CurrentConfig.get(:display_currency)
end

def symbol
::Money.new(1, Spree::Config[:currency]).symbol
::Money.new(1, CurrentConfig.get(:currency)).symbol
end

def symbol_position
Spree::Config[:currency_symbol_position]
CurrentConfig.get(:currency_symbol_position)
end

def hide_cents
Spree::Config[:hide_cents]
CurrentConfig.get(:hide_cents)
end
end
2 changes: 1 addition & 1 deletion app/services/weights_and_measures.rb
Expand Up @@ -40,7 +40,7 @@ def self.variant_unit_options
end

def self.available_units
Spree::Config.available_units.split(",")
CurrentConfig.get(:available_units).split(",")
end

def self.available_units_sorted
Expand Down
2 changes: 1 addition & 1 deletion app/views/spree/admin/general_settings/edit.html.haml
Expand Up @@ -79,7 +79,7 @@
%fieldset.available_units.no-border-bottom
%legend{:align => "center"}= t('admin.available_units')
.field
- available_units = Spree::Config[:available_units].split(",")
- available_units = CurrentConfig.get(:available_units).split(",")
- all_units.each do |unit|
- selected = available_units.include?(unit)
= preference_field_tag("available_units[#{unit}]", selected, { type: :boolean, selected: selected })
Expand Down
4 changes: 2 additions & 2 deletions app/views/spree/admin/payments/paypal_refund.html.haml
Expand Up @@ -18,8 +18,8 @@
%small
%em= Spree.t(:original_amount, scope: 'paypal', amount: @payment.display_amount)
%br/
- symbol = ::Money.new(1, Spree::Config[:currency]).symbol
- if Spree::Config[:currency_symbol_position] == "before"
- symbol = ::Money.new(1, CurrentConfig.get(:currency)).symbol
- if CurrentConfig.get(:currency_symbol_position) == "before"
= symbol
= text_field_tag 'refund_amount', @payment.amount
- else
Expand Down
2 changes: 1 addition & 1 deletion lib/reporting/reports/xero_invoices/base.rb
Expand Up @@ -189,7 +189,7 @@ def row(order, sku, description, quantity, amount, invoice_number, tax_type, opt
'',
'',
'',
Spree::Config.currency,
CurrentConfig.get(:currency),
'',
order.paid? ? I18n.t(:y) : I18n.t(:n)]
end
Expand Down
2 changes: 1 addition & 1 deletion lib/spree/core/controller_helpers/order.rb
Expand Up @@ -79,7 +79,7 @@ def set_current_order
end

def current_currency
Spree::Config[:currency]
CurrentConfig.get(:currency)
end

def ip_address
Expand Down
14 changes: 7 additions & 7 deletions lib/spree/money.rb
Expand Up @@ -9,7 +9,7 @@ class Money
delegate :cents, to: :money

def initialize(amount, options = {})
@money = ::Monetize.parse([amount, options[:currency] || Spree::Config[:currency]].join)
@money = ::Monetize.parse([amount, options[:currency] || CurrentConfig.get(:currency)].join)

if options.key?(:symbol_position)
options[:format] = position_to_format(options.delete(:symbol_position))
Expand All @@ -20,7 +20,7 @@ def initialize(amount, options = {})

# Return the currency symbol (on its own) for the current default currency
def self.currency_symbol
::Money.new(0, Spree::Config[:currency]).symbol
::Money.new(0, CurrentConfig.get(:currency)).symbol
end

def to_s
Expand All @@ -44,11 +44,11 @@ def ==(other)

def defaults
{
with_currency: Spree::Config[:display_currency],
no_cents: Spree::Config[:hide_cents],
decimal_mark: Spree::Config[:currency_decimal_mark],
thousands_separator: Spree::Config[:currency_thousands_separator],
format: position_to_format(Spree::Config[:currency_symbol_position])
with_currency: CurrentConfig.get(:display_currency),
no_cents: CurrentConfig.get(:hide_cents),
decimal_mark: CurrentConfig.get(:currency_decimal_mark),
thousands_separator: CurrentConfig.get(:currency_thousands_separator),
format: position_to_format(CurrentConfig.get(:currency_symbol_position))
}
end

Expand Down

0 comments on commit 6d7f02c

Please sign in to comment.