Files
openfoodnetwork/app/models/spree/preferences/store.rb
Neal Chambers e9f448fad9 Safely autocorrect Lint/AmbiguousOperatorPrecedence
Inspecting 1480 files
...................................................................................................................................................................................................................................W..........................W........................................................W................W..............W................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................

Offenses:

app/models/calculator/flexi_rate.rb:38:7: W: [Corrected] Lint/AmbiguousOperatorPrecedence: Wrap expressions with varying precedence with parentheses to avoid ambiguity.
      count * preferred_additional_item.to_f + preferred_first_item.to_f
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
app/models/enterprise.rb:362:12: W: [Corrected] Lint/AmbiguousOperatorPrecedence: Wrap expressions with varying precedence with parentheses to avoid ambiguity.
    cat << "sells_" + sells
           ^^^^^^^^^^^^^^^^
app/models/enterprise.rb:496:21: W: [Corrected] Lint/AmbiguousOperatorPrecedence: Wrap expressions with varying precedence with parentheses to avoid ambiguity.
    phone_number && "https://wa.me/" + phone_number.tr('+ ', '')
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
app/models/spree/ability.rb:27:33: W: [Corrected] Lint/AmbiguousOperatorPrecedence: Wrap expressions with varying precedence with parentheses to avoid ambiguity.
          order.user == user || order.token && token == order.token
                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
app/models/spree/ability.rb:30:33: W: [Corrected] Lint/AmbiguousOperatorPrecedence: Wrap expressions with varying precedence with parentheses to avoid ambiguity.
          order.user == user || order.token && token == order.token
                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
app/models/spree/line_item.rb:205:16: W: [Corrected] Lint/AmbiguousOperatorPrecedence: Wrap expressions with varying precedence with parentheses to avoid ambiguity.
      (price + fees / quantity).round(2)
               ^^^^^^^^^^^^^^^
app/models/spree/preferences/store.rb:28:11: W: [Corrected] Lint/AmbiguousOperatorPrecedence: Wrap expressions with varying precedence with parentheses to avoid ambiguity.
          should_persist? && Spree::Preference.where(key: key).exists?
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

1480 files inspected, 7 offenses detected, 7 offenses corrected
2023-07-30 21:29:19 +09:00

101 lines
2.4 KiB
Ruby

# frozen_string_literal: true
# Use singleton class Spree::Preferences::Store.instance to access
#
# StoreInstance has a persistence flag that is on by default,
# but we disable database persistence in testing to speed up tests
#
require 'singleton'
module Spree
module Preferences
class StoreInstance
attr_accessor :persistence
def initialize
@cache = Rails.cache
@persistence = true
end
def set(key, value, type)
@cache.write(key, value)
persist(key, value, type)
end
def exist?(key)
@cache.exist?(key) ||
(should_persist? && Spree::Preference.where(key: key).exists?)
end
def get(key, fallback = nil)
# return the retrieved value, if it's in the cache
# use unless nil? incase the value is actually boolean false
#
unless (val = @cache.read(key)).nil?
return val
end
# If it's not in the cache, maybe it's in the database, but has been cleared from the cache
# does it exist in the database?
if should_persist? && Spree::Preference.table_exists?
preference = Spree::Preference.find_by(key: key)
if preference
# it does exist, so let's put it back into the cache
@cache.write(preference.key, preference.value)
# and return the value
return preference.value
end
end
unless fallback.nil?
# cache fallback so we won't hit the db above on
# subsequent queries for the same key
#
@cache.write(key, fallback)
end
fallback
end
def delete(key)
return if key.nil?
@cache.delete(key)
destroy(key)
end
def clear_cache
@cache.clear
end
private
def persist(cache_key, value, type)
return unless should_persist?
preference = Spree::Preference.where(key: cache_key).first_or_initialize
preference.value = value
preference.value_type = type
preference.save
end
def destroy(cache_key)
return unless should_persist?
preference = Spree::Preference.find_by(key: cache_key)
preference&.destroy
end
def should_persist?
@persistence && Spree::Preference.connected? && Spree::Preference.table_exists?
end
end
class Store < StoreInstance
include Singleton
end
end
end