Run rubocop autocorrect

This commit is contained in:
Luis Ramos
2020-08-06 11:44:20 +01:00
parent ed81ceaffe
commit 25e61897aa
11 changed files with 78 additions and 89 deletions

View File

@@ -1,3 +1,5 @@
# frozen_string_literal: true
# This is the primary location for defining spree preferences
#
# This file allows us to add global configuration variables, which
@@ -23,7 +25,6 @@ require "spree/core/search/base"
module Spree
class AppConfiguration < Preferences::Configuration
# Alphabetized to more easily lookup particular preferences
preference :address_requires_state, :boolean, default: true # should state/state_name be required
preference :admin_interface_logo, :string, default: 'logo/spree_50.png'
@@ -59,14 +60,14 @@ module Spree
preference :orders_per_page, :integer, default: 15
preference :prices_inc_tax, :boolean, default: false
preference :products_per_page, :integer, default: 12
preference :redirect_https_to_http, :boolean, :default => false
preference :redirect_https_to_http, :boolean, default: false
preference :require_master_price, :boolean, default: true
preference :shipment_inc_vat, :boolean, default: false
preference :shipping_instructions, :boolean, default: false # Request instructions/info for shipping
preference :show_only_complete_orders_by_default, :boolean, default: true
preference :show_variant_full_price, :boolean, default: false #Displays variant full price or difference with product price. Default false to be compatible with older behavior
preference :show_variant_full_price, :boolean, default: false # Displays variant full price or difference with product price. Default false to be compatible with older behavior
preference :show_products_without_price, :boolean, default: false
preference :show_raw_product_description, :boolean, :default => false
preference :show_raw_product_description, :boolean, default: false
preference :site_name, :string, default: 'Spree Demo Site'
preference :site_url, :string, default: 'demo.spreecommerce.com'
preference :tax_using_ship_address, :boolean, default: true
@@ -87,18 +88,18 @@ module Spree
preference :s3_host_alias, :string
# Default mail headers settings
preference :enable_mail_delivery, :boolean, :default => false
preference :mails_from, :string, :default => 'spree@example.com'
preference :mail_bcc, :string, :default => 'spree@example.com'
preference :intercept_email, :string, :default => nil
preference :enable_mail_delivery, :boolean, default: false
preference :mails_from, :string, default: 'spree@example.com'
preference :mail_bcc, :string, default: 'spree@example.com'
preference :intercept_email, :string, default: nil
# Default smtp settings
preference :override_actionmailer_config, :boolean, :default => true
preference :mail_host, :string, :default => 'localhost'
preference :mail_domain, :string, :default => 'localhost'
preference :mail_port, :integer, :default => 25
preference :secure_connection_type, :string, :default => Core::MailSettings::SECURE_CONNECTION_TYPES[0]
preference :mail_auth_type, :string, :default => Core::MailSettings::MAIL_AUTH[0]
preference :override_actionmailer_config, :boolean, default: true
preference :mail_host, :string, default: 'localhost'
preference :mail_domain, :string, default: 'localhost'
preference :mail_port, :integer, default: 25
preference :secure_connection_type, :string, default: Core::MailSettings::SECURE_CONNECTION_TYPES[0]
preference :mail_auth_type, :string, default: Core::MailSettings::MAIL_AUTH[0]
preference :smtp_username, :string
preference :smtp_password, :string
@@ -145,9 +146,7 @@ module Spree
@searcher_class ||= Spree::Core::Search::Base
end
def searcher_class=(sclass)
@searcher_class = sclass
end
attr_writer :searcher_class
attr_writer :package_factory, :order_updater_decorator

View File

@@ -1,3 +1,5 @@
# frozen_string_literal: true
class Spree::Preference < ActiveRecord::Base
serialize :value
@@ -16,13 +18,13 @@ class Spree::Preference < ActiveRecord::Base
when :password
self[:value].to_s
when :decimal
BigDecimal.new(self[:value].to_s).round(2, BigDecimal::ROUND_HALF_UP)
BigDecimal(self[:value].to_s).round(2, BigDecimal::ROUND_HALF_UP)
when :integer
self[:value].to_i
when :boolean
(self[:value].to_s =~ /^[t|1]/i) != nil
else
self[:value].is_a?(String) ? YAML.load(self[:value]) : self[:value]
self[:value].is_a?(String) ? YAML.safe_load(self[:value]) : self[:value]
end
else
self[:value]

View File

@@ -1,3 +1,5 @@
# frozen_string_literal: true
# This takes the preferrable methods and adds some
# syntatic sugar to access the preferences
#
@@ -33,7 +35,7 @@ module Spree::Preferences
end
def reset
preferences.each do |name, value|
preferences.each do |name, _value|
set_preference name, preference_default(name)
end
end
@@ -66,6 +68,5 @@ module Spree::Preferences
super
end
end
end
end

View File

@@ -1,3 +1,5 @@
# 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
@@ -9,23 +11,17 @@
# and copy all the definitions allowing the subclass to add
# additional defintions without affecting the base
module Spree::Preferences::Preferable
def self.included(base)
base.class_eval do
extend Spree::Preferences::PreferableClassMethods
if respond_to?(:after_create)
after_create do |obj|
obj.save_pending_preferences
end
after_create(&:save_pending_preferences)
end
if respond_to?(:after_destroy)
after_destroy do |obj|
obj.clear_preferences
end
after_destroy(&:clear_preferences)
end
end
end
@@ -57,7 +53,7 @@ module Spree::Preferences::Preferable
end
def has_preference!(name)
raise NoMethodError.new "#{name} preference not defined" unless has_preference? name
raise NoMethodError, "#{name} preference not defined" unless has_preference? name
end
def has_preference?(name)
@@ -78,18 +74,20 @@ module Spree::Preferences::Preferable
def preference_cache_key(name)
return unless id
[ENV["RAILS_CACHE_ID"], 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.keys.each {|pref| preference_store.delete preference_cache_key(pref)}
preferences.keys.each { |pref| preference_store.delete preference_cache_key(pref) }
end
private
@@ -101,6 +99,7 @@ module Spree::Preferences::Preferable
def get_pending_preference(name)
return unless @pending_preferences
@pending_preferences[name]
end
@@ -111,7 +110,7 @@ module Spree::Preferences::Preferable
when :password
value.to_s
when :decimal
BigDecimal.new(value.to_s).round(2, BigDecimal::ROUND_HALF_UP)
BigDecimal(value.to_s).round(2, BigDecimal::ROUND_HALF_UP)
when :integer
value.to_i
when :boolean
@@ -119,10 +118,10 @@ module Spree::Preferences::Preferable
value.nil? ||
value == 0 ||
value =~ /^(f|false|0)$/i ||
(value.respond_to? :empty? and value.empty?)
false
(value.respond_to?(:empty?) && value.empty?)
false
else
true
true
end
else
value
@@ -132,6 +131,4 @@ module Spree::Preferences::Preferable
def preference_store
Spree::Preferences::Store.instance
end
end

View File

@@ -1,6 +1,7 @@
# frozen_string_literal: true
module Spree::Preferences
module PreferableClassMethods
def preference(name, type, *args)
options = args.extract_options!
options.assert_valid_keys(:default, :description)
@@ -10,7 +11,6 @@ module Spree::Preferences
# cache_key will be nil for new objects, then if we check if there
# is a pending preference before going to default
define_method preference_getter_method(name) do
# perference_cache_key will only be nil/false for new records
#
if preference_cache_key(name)
@@ -59,7 +59,7 @@ module Spree::Preferences
end
def preference_setter_method(name)
"preferred_#{name}=".to_sym
"preferred_#{name}=".to_sym
end
def prefers_getter_method(name)
@@ -67,7 +67,7 @@ module Spree::Preferences
end
def prefers_setter_method(name)
"prefers_#{name}=".to_sym
"prefers_#{name}=".to_sym
end
def preference_default_getter_method(name)
@@ -81,6 +81,5 @@ module Spree::Preferences
def preference_description_getter_method(name)
"preferred_#{name}_description".to_sym
end
end
end

View File

@@ -1,3 +1,5 @@
# frozen_string_literal: true
# Use singleton class Spree::Preferences::Store.instance to access
#
# StoreInstance has a persistence flag that is on by default,
@@ -7,7 +9,6 @@
require 'singleton'
module Spree::Preferences
class StoreInstance
attr_accessor :persistence
@@ -23,10 +24,10 @@ module Spree::Preferences
def exist?(key)
@cache.exist?(key) ||
should_persist? && Spree::Preference.where(:key => key).exists?
should_persist? && Spree::Preference.where(key: key).exists?
end
def get(key,fallback=nil)
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
#
@@ -39,7 +40,7 @@ module Spree::Preferences
# has been cleared from the cache
# does it exist in the database?
if Spree::Preference.table_exists? && preference = Spree::Preference.find_by_key(key)
if Spree::Preference.table_exists? && preference = Spree::Preference.find_by(key: key)
# it does exist, so let's put it back into the cache
@cache.write(preference.key, preference.value)
@@ -55,7 +56,7 @@ module Spree::Preferences
@cache.write(key, fallback)
end
return fallback
fallback
end
def delete(key)
@@ -72,7 +73,7 @@ module Spree::Preferences
def persist(cache_key, value, type)
return unless should_persist?
preference = Spree::Preference.where(:key => cache_key).first_or_initialize
preference = Spree::Preference.where(key: cache_key).first_or_initialize
preference.value = value
preference.value_type = type
preference.save
@@ -81,18 +82,16 @@ module Spree::Preferences
def destroy(cache_key)
return unless should_persist?
preference = Spree::Preference.find_by_key(cache_key)
preference.destroy if preference
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

View File

@@ -1,7 +1,8 @@
# frozen_string_literal: true
require 'spec_helper'
describe Spree::AppConfiguration do
let (:prefs) { Rails.application.config.spree.preferences }
it "should be available from the environment" do

View File

@@ -1,7 +1,8 @@
# frozen_string_literal: true
require 'spec_helper'
describe Spree::Preference do
it "should require a key" do
@preference = Spree::Preference.new
@preference.key = :test
@@ -18,7 +19,7 @@ describe Spree::Preference do
p.key = key
p.save
Spree::Preference.find_by_key(key)
Spree::Preference.find_by(key: key)
end
it ":boolean" do
@@ -92,7 +93,5 @@ describe Spree::Preference do
pref.value.should eq value
pref.value_type.should == value_type.to_s
end
end
end

View File

@@ -1,10 +1,11 @@
# frozen_string_literal: true
require 'spec_helper'
describe Spree::Preferences::Configuration do
before :all do
class AppConfig < Spree::Preferences::Configuration
preference :color, :string, :default => :blue
preference :color, :string, default: :blue
end
@config = AppConfig.new
end
@@ -23,8 +24,4 @@ describe Spree::Preferences::Configuration do
@config.set :color, 'green'
@config.get(:color).should eq 'green'
end
end

View File

@@ -1,7 +1,8 @@
# frozen_string_literal: true
require 'spec_helper'
describe Spree::Preferences::Preferable do
before :all do
class A
include Spree::Preferences::Preferable
@@ -11,7 +12,7 @@ describe Spree::Preferences::Preferable do
@id = rand(999)
end
preference :color, :string, :default => 'green', :description => "My Favorite Color"
preference :color, :string, default: 'green', description: "My Favorite Color"
end
class B < A
@@ -21,9 +22,9 @@ describe Spree::Preferences::Preferable do
before :each do
@a = A.new
@a.stub(:persisted? => true)
@a.stub(persisted?: true)
@b = B.new
@b.stub(:persisted? => true)
@b.stub(persisted?: true)
# ensure we're persisting as that is the default
#
@@ -79,7 +80,6 @@ describe Spree::Preferences::Preferable do
@a.get_preference :flavor
}.to raise_error(NoMethodError, "flavor preference not defined")
end
end
describe "preference access" do
@@ -114,7 +114,7 @@ describe Spree::Preferences::Preferable do
it "builds a hash of preferences" do
@b.preferred_flavor = :strawberry
@b.preferences[:flavor].should eq 'strawberry'
@b.preferences[:color].should eq 'green' #default from A
@b.preferences[:color].should eq 'green' # default from A
end
context "database fallback" do
@@ -123,7 +123,7 @@ describe Spree::Preferences::Preferable do
end
it "retrieves a preference from the database before falling back to default" do
preference = double(:value => "chatreuse", :key => 'a/color/123')
preference = double(value: "chatreuse", key: 'a/color/123')
Spree::Preference.should_receive(:find_by_key).and_return(preference)
@a.preferred_color.should == 'chatreuse'
end
@@ -134,7 +134,6 @@ describe Spree::Preferences::Preferable do
end
end
context "converts integer preferences to integer values" do
before do
A.preference :is_integer, :integer
@@ -147,7 +146,6 @@ describe Spree::Preferences::Preferable do
@a.set_preference(:is_integer, '')
@a.preferences[:is_integer].should == 0
end
end
context "converts decimal preferences to BigDecimal values" do
@@ -171,7 +169,7 @@ describe Spree::Preferences::Preferable do
context "converts boolean preferences to boolean values" do
before do
A.preference :is_boolean, :boolean, :default => true
A.preference :is_boolean, :boolean, default: true
end
it "with strings" do
@@ -203,8 +201,8 @@ describe Spree::Preferences::Preferable do
context "converts any preferences to any values" do
before do
A.preference :product_ids, :any, :default => []
A.preference :product_attributes, :any, :default => {}
A.preference :product_ids, :any, default: []
A.preference :product_attributes, :any, default: {}
end
it "with array" do
@@ -215,11 +213,10 @@ describe Spree::Preferences::Preferable do
it "with hash" do
@a.preferences[:product_attributes].should == {}
@a.set_preference(:product_attributes, {:id => 1, :name => 2})
@a.preferences[:product_attributes].should == {:id => 1, :name => 2}
@a.set_preference(:product_attributes, { id: 1, name: 2 })
@a.preferences[:product_attributes].should == { id: 1, name: 2 }
end
end
end
describe "persisted preferables" do
@@ -241,8 +238,8 @@ describe Spree::Preferences::Preferable do
CreatePrefTest.migrate(:up)
class PrefTest < ActiveRecord::Base
preference :pref_test_pref, :string, :default => 'abc'
preference :pref_test_any, :any, :default => []
preference :pref_test_pref, :string, default: 'abc'
preference :pref_test_any, :any, default: []
end
end
@@ -306,7 +303,7 @@ describe Spree::Preferences::Preferable do
@pt.preferred_pref_test_pref = 'lmn'
@pt.save!
@pt.destroy
@pt1 = PrefTest.new(:col => 'aaaa')
@pt1 = PrefTest.new(col: 'aaaa')
@pt1.id = @pt.id
@pt1.save!
@pt1.get_preference(:pref_test_pref).should_not == 'lmn'
@@ -315,17 +312,14 @@ describe Spree::Preferences::Preferable do
end
it "builds cache keys" do
@a.preference_cache_key(:color).should match /a\/color\/\d+/
@a.preference_cache_key(:color).should match %r{a/color/\d+}
end
it "can add and remove preferences" do
A.preference :test_temp, :boolean, :default => true
A.preference :test_temp, :boolean, default: true
@a.preferred_test_temp.should be_true
A.remove_preference :test_temp
@a.has_preference?(:test_temp).should be_false
@a.respond_to?(:preferred_test_temp).should be_false
end
end

View File

@@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'spec_helper'
describe Spree::Preferences::Store do
@@ -17,7 +19,7 @@ describe Spree::Preferences::Store do
end
it "will return db value when cache is emtpy and cache the db value" do
preference = Spree::Preference.where(:key => 'test').first_or_initialize
preference = Spree::Preference.where(key: 'test').first_or_initialize
preference.value = '123'
preference.value_type = 'string'
preference.save
@@ -35,7 +37,7 @@ describe Spree::Preferences::Store do
it "should return and cache fallback value when persistence is disabled (i.e. during bootstrap)" do
Rails.cache.clear
@store.stub(:should_persist? => false)
@store.stub(should_persist?: false)
@store.get(:test, true).should be_true
Rails.cache.read(:test).should be_true
end
@@ -43,5 +45,4 @@ describe Spree::Preferences::Store do
it "should return nil when key can't be found and fallback value is not supplied" do
@store.get(:random_key).should be_nil
end
end