Files
openfoodnetwork/app/models/spree/preferences/preferable.rb
Neal Chambers bc559b966c Safely autocorrect Style/FetchEnvVar
Inspecting 1483 files
..............................................................................................................................................................C.............................................................................................................................................................................CC.............................................................................................................................................................................C.................................................................................................................................................................................................................................................................................................................................................................C.................................................................................C..............................................................................................................................................................................................C.....................................................................................................................................................................................................................................C......................................................................................................................

Offenses:

app/helpers/discourse_helper.rb:9:5: C: [Corrected] Style/FetchEnvVar: Use ENV.fetch('DISCOURSE_URL') or ENV.fetch('DISCOURSE_URL', nil) instead of ENV['DISCOURSE_URL'].
    ENV['DISCOURSE_URL']
    ^^^^^^^^^^^^^^^^^^^^
app/models/spree/preferences/configuration.rb:35:10: C: [Corrected] Style/FetchEnvVar: Use ENV.fetch('RAILS_CACHE_ID') or ENV.fetch('RAILS_CACHE_ID', nil) instead of ENV['RAILS_CACHE_ID'].
        [ENV['RAILS_CACHE_ID'], self.class.name, name].flatten.join('::').underscore
         ^^^^^^^^^^^^^^^^^^^^^
app/models/spree/preferences/preferable.rb:84:10: C: [Corrected] Style/FetchEnvVar: Use ENV.fetch("RAILS_CACHE_ID") or ENV.fetch("RAILS_CACHE_ID", nil) instead of ENV["RAILS_CACHE_ID"].
        [ENV["RAILS_CACHE_ID"], self.class.name, name, id].join('::').underscore
         ^^^^^^^^^^^^^^^^^^^^^
app/services/default_country.rb:13:40: C: [Corrected] Style/FetchEnvVar: Use ENV.fetch("DEFAULT_COUNTRY_CODE") or ENV.fetch("DEFAULT_COUNTRY_CODE", nil) instead of ENV["DEFAULT_COUNTRY_CODE"].
    Spree::Country.cached_find_by(iso: ENV["DEFAULT_COUNTRY_CODE"]) || Spree::Country.first
                                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^
app/services/default_country.rb:13:73: C: [Corrected] Layout/TrailingWhitespace: Trailing whitespace detected.
    Spree::Country.cached_find_by(iso: ENV.fetch("DEFAULT_COUNTRY_CODE",
                                                                        ^
app/services/default_country.rb:13:101: C: [Corrected] Layout/LineLength: Line is too long. [102/100]
    Spree::Country.cached_find_by(iso: ENV.fetch("DEFAULT_COUNTRY_CODE", nil)) || Spree::Country.first
                                                                                                    ^^
app/services/default_country.rb:14:1: C: [Corrected] Layout/ArgumentAlignment: Align the arguments of a method call if they span more than one line.
nil)) || Spree::Country.first
^^^
spec/base_spec_helper.rb:51:49: C: [Corrected] Style/FetchEnvVar: Use ENV.fetch("SITE_URL") or ENV.fetch("SITE_URL", nil) instead of ENV["SITE_URL"].
ActionMailer::Base.default_url_options[:host] = ENV["SITE_URL"]
                                                ^^^^^^^^^^^^^^^
spec/controllers/spree/credit_cards_controller_spec.rb:8:20: C: [Corrected] Style/FetchEnvVar: Use ENV.fetch('STRIPE_SECRET_TEST_API_KEY') or ENV.fetch('STRIPE_SECRET_TEST_API_KEY', nil) instead of ENV['STRIPE_SECRET_TEST_API_KEY'].
    let(:secret) { ENV['STRIPE_SECRET_TEST_API_KEY'] }
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
spec/models/order_balance_spec.rb:48:70: C: [Corrected] Layout/TrailingWhitespace: Trailing whitespace detected.
      expect(order_balance.display_amount).to eq(Spree::Money.new(20,
                                                                     ^
spec/models/order_balance_spec.rb:48:81: C: [Corrected] Style/FetchEnvVar: Use ENV.fetch('currency') or ENV.fetch('currency', nil) instead of ENV['currency'].
      expect(order_balance.display_amount).to eq(Spree::Money.new(20, currency: ENV['currency']))
                                                                                ^^^^^^^^^^^^^^^
spec/models/order_balance_spec.rb:48:101: C: [Corrected] Layout/LineLength: Line is too long. [108/100]
      expect(order_balance.display_amount).to eq(Spree::Money.new(20, currency: ENV.fetch('currency', nil)))
                                                                                                    ^^^^^^^^
spec/models/order_balance_spec.rb:49:1: C: [Corrected] Layout/ArgumentAlignment: Align the arguments of a method call if they span more than one line.
currency: ENV.fetch('currency', nil)))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
spec/models/order_balance_spec.rb:49:98: C: [Corrected] Layout/TrailingWhitespace: Trailing whitespace detected.
                                                                  currency: ENV.fetch('currency',
                                                                                                 ^
spec/models/order_balance_spec.rb:49:101: C: [Corrected] Layout/LineLength: Line is too long. [104/100]
                                                                  currency: ENV.fetch('currency', nil)))
                                                                                                    ^^^^
spec/models/order_balance_spec.rb:50:1: C: [Corrected] Layout/ArgumentAlignment: Align the arguments of a method call if they span more than one line.
nil)))
^^^
spec/support/vcr_setup.rb:10:50: C: [Corrected] Style/FetchEnvVar: Use ENV.fetch('STRIPE_SECRET_TEST_API_KEY') or ENV.fetch('STRIPE_SECRET_TEST_API_KEY', nil) instead of ENV['STRIPE_SECRET_TEST_API_KEY'].
  config.filter_sensitive_data('<HIDDEN_KEY>') { ENV['STRIPE_SECRET_TEST_API_KEY'] }
                                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
spec/support/vcr_setup.rb:11:55: C: [Corrected] Style/FetchEnvVar: Use ENV.fetch('STRIPE_CUSTOMER') or ENV.fetch('STRIPE_CUSTOMER', nil) instead of ENV['STRIPE_CUSTOMER'].
  config.filter_sensitive_data('<HIDDEN_CUSTOMER>') { ENV['STRIPE_CUSTOMER'] }
                                                      ^^^^^^^^^^^^^^^^^^^^^^

1483 files inspected, 18 offenses detected, 18 offenses corrected
2023-08-20 12:33:43 +09:00

144 lines
4.0 KiB
Ruby

# frozen_string_literal: true
# The preference_cache_key is used to determine if the preference
# can be set. The default behavior is to return nil if there is no
# id value. On ActiveRecords, new objects will have their preferences
# saved to a pending hash until it is persisted.
#
# class_attributes are inheritied unless you reassign them in
# the subclass, so when you inherit a Preferable class, the
# inherited hook will assign a new hash for the subclass definitions
# and copy all the definitions allowing the subclass to add
# additional defintions without affecting the base
module Spree
module Preferences
module Preferable
def self.included(base)
base.class_eval do
extend Spree::Preferences::PreferableClassMethods
# Disabling rubocop rule because the fix to this rubocop warning breaks specs
# rubocop:disable Style/SymbolProc
if respond_to?(:after_create)
after_create do |obj|
obj.save_pending_preferences
end
end
if respond_to?(:after_destroy)
after_destroy do |obj|
obj.clear_preferences
end
end
# rubocop:enable Style/SymbolProc
end
end
def get_preference(name)
has_preference! name
__send__ self.class.preference_getter_method(name)
end
alias :preferred :get_preference
alias :prefers? :get_preference
def set_preference(name, value)
has_preference! name
__send__ self.class.preference_setter_method(name), value
end
def preference_type(name)
has_preference! name
__send__ self.class.preference_type_getter_method(name)
end
def preference_default(name)
has_preference! name
__send__ self.class.preference_default_getter_method(name)
end
def preference_description(name)
has_preference! name
__send__ self.class.preference_description_getter_method(name)
end
def has_preference!(name)
raise NoMethodError, "#{name} preference not defined" unless has_preference? name
end
def has_preference?(name)
respond_to? self.class.preference_getter_method(name)
end
def preferences
prefs = {}
methods.grep(/^prefers_.*\?$/).each do |pref_method|
prefs[pref_method.to_s.gsub(/prefers_|\?/, '').to_sym] = __send__(pref_method)
end
prefs
end
def preference_cache_key(name)
return unless id
[ENV.fetch("RAILS_CACHE_ID", nil), self.class.name, name, id].join('::').underscore
end
def save_pending_preferences
return unless @pending_preferences
@pending_preferences.each do |name, value|
set_preference(name, value)
end
end
def clear_preferences
preferences.each_key { |pref| preference_store.delete preference_cache_key(pref) }
end
private
def add_pending_preference(name, value)
@pending_preferences ||= {}
@pending_preferences[name] = value
end
def get_pending_preference(name)
return unless @pending_preferences
@pending_preferences[name]
end
def convert_preference_value(value, type)
case type
when :string, :text
value.to_s
when :password
value.to_s
when :decimal
value = 0 if value.blank?
BigDecimal(value.to_s, exception: false)&.round(2, BigDecimal::ROUND_HALF_UP) || value
when :integer
value.to_i
when :boolean
if value.is_a?(FalseClass) ||
value.nil? ||
value == 0 ||
(value.is_a?(String) && value =~ /^(f|false|0)$/i) ||
(value.respond_to?(:empty?) && value.empty?)
false
else
true
end
else
value
end
end
def preference_store
Spree::Preferences::Store.instance
end
end
end
end