From 7cefdda5796854daab6d073bdc41a24c5a7ef720 Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Fri, 7 Aug 2020 10:21:09 +0100 Subject: [PATCH] Run rubocop autocorrect --- app/models/spree/address.rb | 88 +++++++++++--------- app/models/spree/shipment.rb | 2 +- app/models/spree/shipping_category.rb | 2 + app/models/spree/shipping_method.rb | 33 ++++---- app/models/spree/shipping_method_category.rb | 2 + app/models/spree/shipping_rate.rb | 34 ++++---- spec/models/spree/address_spec.rb | 61 +++++++------- spec/models/spree/shipping_rate_spec.rb | 20 +++-- 8 files changed, 134 insertions(+), 108 deletions(-) diff --git a/app/models/spree/address.rb b/app/models/spree/address.rb index af7643dd31..10f97c431f 100644 --- a/app/models/spree/address.rb +++ b/app/models/spree/address.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Spree class Address < ActiveRecord::Base include AddressDisplay @@ -23,7 +25,11 @@ module Spree geocoded_by :geocode_address def self.default - country = Spree::Country.find(Spree::Config[:default_country_id]) rescue Spree::Country.first + country = begin + Spree::Country.find(Spree::Config[:default_country_id]) + rescue StandardError + Spree::Country.first + end new(country: country) end @@ -42,6 +48,7 @@ module Spree def same_as?(other) return false if other.nil? + attributes.except('id', 'updated_at', 'created_at') == other.attributes.except('id', 'updated_at', 'created_at') end @@ -52,11 +59,11 @@ module Spree end def clone - self.class.new(self.attributes.except('id', 'updated_at', 'created_at')) + self.class.new(attributes.except('id', 'updated_at', 'created_at')) end def ==(other_address) - self_attrs = self.attributes + self_attrs = attributes other_attrs = other_address.respond_to?(:attributes) ? other_address.attributes : {} [self_attrs, other_attrs].each { |attrs| attrs.except!('id', 'created_at', 'updated_at', 'order_id') } @@ -99,51 +106,52 @@ module Spree end private - def require_phone? - true - end - def require_zipcode? - true - end + def require_phone? + true + end - def state_validate - # Skip state validation without country (also required) - # or when disabled by preference - return if country.blank? || !Spree::Config[:address_requires_state] - return unless country.states_required + def require_zipcode? + true + end - # ensure associated state belongs to country - if state.present? - if state.country == country - self.state_name = nil #not required as we have a valid state and country combo + def state_validate + # Skip state validation without country (also required) + # or when disabled by preference + return if country.blank? || !Spree::Config[:address_requires_state] + return unless country.states_required + + # ensure associated state belongs to country + if state.present? + if state.country == country + self.state_name = nil # not required as we have a valid state and country combo + else + if state_name.present? + self.state = nil else - if state_name.present? - self.state = nil - else - errors.add(:state, :invalid) - end + errors.add(:state, :invalid) end end - - # ensure state_name belongs to country without states, or that it matches a predefined state name/abbr - if state_name.present? - if country.states.present? - states = country.states.find_all_by_name_or_abbr(state_name) - - if states.size == 1 - self.state = states.first - self.state_name = nil - else - errors.add(:state, :invalid) - end - end - end - - # ensure at least one state field is populated - errors.add :state, :blank if state.blank? && state_name.blank? end + # ensure state_name belongs to country without states, or that it matches a predefined state name/abbr + if state_name.present? + if country.states.present? + states = country.states.find_all_by_name_or_abbr(state_name) + + if states.size == 1 + self.state = states.first + self.state_name = nil + else + errors.add(:state, :invalid) + end + end + end + + # ensure at least one state field is populated + errors.add :state, :blank if state.blank? && state_name.blank? + end + def touch_enterprise enterprise.andand.touch end diff --git a/app/models/spree/shipment.rb b/app/models/spree/shipment.rb index 98ea4ac982..b81a6e4ba8 100644 --- a/app/models/spree/shipment.rb +++ b/app/models/spree/shipment.rb @@ -249,7 +249,7 @@ module Spree def to_package package = OrderManagement::Stock::Package.new(stock_location, order) - inventory_units.includes(:variant).each do |inventory_unit| + inventory_units.includes(:variant).find_each do |inventory_unit| package.add inventory_unit.variant, 1, inventory_unit.state_name end package diff --git a/app/models/spree/shipping_category.rb b/app/models/spree/shipping_category.rb index c3d7ca3f24..741a27b573 100644 --- a/app/models/spree/shipping_category.rb +++ b/app/models/spree/shipping_category.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Spree class ShippingCategory < ActiveRecord::Base validates :name, presence: true diff --git a/app/models/spree/shipping_method.rb b/app/models/spree/shipping_method.rb index 30aee7f6ca..786a8d1add 100644 --- a/app/models/spree/shipping_method.rb +++ b/app/models/spree/shipping_method.rb @@ -1,7 +1,9 @@ +# frozen_string_literal: true + module Spree class ShippingMethod < ActiveRecord::Base include Spree::Core::CalculatedAdjustments - DISPLAY = [:both, :front_end, :back_end] + DISPLAY = [:both, :front_end, :back_end].freeze acts_as_taggable @@ -14,9 +16,9 @@ module Spree has_many :distributor_shipping_methods has_many :distributors, through: :distributor_shipping_methods, class_name: 'Enterprise', foreign_key: 'distributor_id' - has_and_belongs_to_many :zones, :join_table => 'spree_shipping_methods_zones', - :class_name => 'Spree::Zone', - :foreign_key => 'shipping_method_id' + has_and_belongs_to_many :zones, join_table: 'spree_shipping_methods_zones', + class_name: 'Spree::Zone', + foreign_key: 'shipping_method_id' validates :name, presence: true validate :distributor_validation @@ -67,7 +69,7 @@ module Spree # Some shipping methods are only meant to be set via backend def frontend? - self.display_on != "back_end" + display_on != "back_end" end def has_distributor?(distributor) @@ -97,19 +99,20 @@ module Spree end private - def at_least_one_shipping_category - if self.shipping_categories.empty? - self.errors[:base] << "You need to select at least one shipping category" - end - end - def self.on_backend_query - "#{table_name}.display_on != 'front_end' OR #{table_name}.display_on IS NULL" + def at_least_one_shipping_category + if shipping_categories.empty? + errors[:base] << "You need to select at least one shipping category" end + end - def self.on_frontend_query - "#{table_name}.display_on != 'back_end' OR #{table_name}.display_on IS NULL" - end + def self.on_backend_query + "#{table_name}.display_on != 'front_end' OR #{table_name}.display_on IS NULL" + end + + def self.on_frontend_query + "#{table_name}.display_on != 'back_end' OR #{table_name}.display_on IS NULL" + end def touch_distributors distributors.each do |distributor| diff --git a/app/models/spree/shipping_method_category.rb b/app/models/spree/shipping_method_category.rb index 08c691114b..a8ad0612e3 100644 --- a/app/models/spree/shipping_method_category.rb +++ b/app/models/spree/shipping_method_category.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Spree class ShippingMethodCategory < ActiveRecord::Base belongs_to :shipping_method, class_name: 'Spree::ShippingMethod' diff --git a/app/models/spree/shipping_rate.rb b/app/models/spree/shipping_rate.rb index b930cda43b..ded784900b 100644 --- a/app/models/spree/shipping_rate.rb +++ b/app/models/spree/shipping_rate.rb @@ -1,28 +1,34 @@ +# frozen_string_literal: true + module Spree class ShippingRate < ActiveRecord::Base belongs_to :shipment, class_name: 'Spree::Shipment' belongs_to :shipping_method, class_name: 'Spree::ShippingMethod' scope :frontend, - -> { includes(:shipping_method). - where(ShippingMethod.on_frontend_query). - references(:shipping_method). - order("cost ASC") } + -> { + includes(:shipping_method). + where(ShippingMethod.on_frontend_query). + references(:shipping_method). + order("cost ASC") + } scope :backend, - -> { includes(:shipping_method). - where(ShippingMethod.on_backend_query). - references(:shipping_method). - order("cost ASC") } - + -> { + includes(:shipping_method). + where(ShippingMethod.on_backend_query). + references(:shipping_method). + order("cost ASC") + } + delegate :order, :currency, to: :shipment delegate :name, to: :shipping_method def display_price - if Spree::Config[:shipment_inc_vat] - price = (1 + Spree::TaxRate.default) * cost - else - price = cost - end + price = if Spree::Config[:shipment_inc_vat] + (1 + Spree::TaxRate.default) * cost + else + cost + end Spree::Money.new(price, { currency: currency }) end diff --git a/spec/models/spree/address_spec.rb b/spec/models/spree/address_spec.rb index 96accfa587..8e56cf4139 100644 --- a/spec/models/spree/address_spec.rb +++ b/spec/models/spree/address_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe Spree::Address do @@ -5,18 +7,18 @@ describe Spree::Address do it "creates a copy of the address with the exception of the id, updated_at and created_at attributes" do state = create(:state) original = create(:address, - :address1 => 'address1', - :address2 => 'address2', - :alternative_phone => 'alternative_phone', - :city => 'city', - :country => Spree::Country.first, - :firstname => 'firstname', - :lastname => 'lastname', - :company => 'company', - :phone => 'phone', - :state_id => state.id, - :state_name => state.name, - :zipcode => 'zip_code') + address1: 'address1', + address2: 'address2', + alternative_phone: 'alternative_phone', + city: 'city', + country: Spree::Country.first, + firstname: 'firstname', + lastname: 'lastname', + company: 'company', + phone: 'phone', + state_id: state.id, + state_name: state.name, + zipcode: 'zip_code') cloned = original.clone @@ -60,12 +62,12 @@ describe Spree::Address do end end - let(:country) { mock_model(Spree::Country, :states => [state], :states_required => true) } - let(:state) { stub_model(Spree::State, :name => 'maryland', :abbr => 'md') } - let(:address) { build(:address, :country => country) } + let(:country) { mock_model(Spree::Country, states: [state], states_required: true) } + let(:state) { stub_model(Spree::State, name: 'maryland', abbr: 'md') } + let(:address) { build(:address, country: country) } before do - country.states.stub :find_all_by_name_or_abbr => [state] + country.states.stub find_all_by_name_or_abbr: [state] end it "state_name is not nil and country does not have any states" do @@ -119,7 +121,7 @@ describe Spree::Address do end it "address_requires_state preference is false" do - Spree::Config.set :address_requires_state => false + Spree::Config.set address_requires_state: false address.state = nil address.state_name = nil address.should be_valid @@ -138,7 +140,7 @@ describe Spree::Address do end context "phone not required" do - before { address.instance_eval{ self.stub :require_phone? => false } } + before { address.instance_eval{ stub require_phone?: false } } it "shows no errors when phone is blank" do address.phone = "" @@ -148,7 +150,7 @@ describe Spree::Address do end context "zipcode not required" do - before { address.instance_eval{ self.stub :require_zipcode? => false } } + before { address.instance_eval{ stub require_zipcode?: false } } it "shows no errors when phone is blank" do address.zipcode = "" @@ -181,48 +183,47 @@ describe Spree::Address do context '#full_name' do context 'both first and last names are present' do - let(:address) { stub_model(Spree::Address, :firstname => 'Michael', :lastname => 'Jackson') } + let(:address) { stub_model(Spree::Address, firstname: 'Michael', lastname: 'Jackson') } specify { address.full_name.should == 'Michael Jackson' } end context 'first name is blank' do - let(:address) { stub_model(Spree::Address, :firstname => nil, :lastname => 'Jackson') } + let(:address) { stub_model(Spree::Address, firstname: nil, lastname: 'Jackson') } specify { address.full_name.should == 'Jackson' } end context 'last name is blank' do - let(:address) { stub_model(Spree::Address, :firstname => 'Michael', :lastname => nil) } + let(:address) { stub_model(Spree::Address, firstname: 'Michael', lastname: nil) } specify { address.full_name.should == 'Michael' } end context 'both first and last names are blank' do - let(:address) { stub_model(Spree::Address, :firstname => nil, :lastname => nil) } + let(:address) { stub_model(Spree::Address, firstname: nil, lastname: nil) } specify { address.full_name.should == '' } end - end context '#state_text' do context 'state is blank' do - let(:address) { stub_model(Spree::Address, :state => nil, :state_name => 'virginia') } + let(:address) { stub_model(Spree::Address, state: nil, state_name: 'virginia') } specify { address.state_text.should == 'virginia' } end context 'both name and abbr is present' do - let(:state) { stub_model(Spree::State, :name => 'virginia', :abbr => 'va') } - let(:address) { stub_model(Spree::Address, :state => state) } + let(:state) { stub_model(Spree::State, name: 'virginia', abbr: 'va') } + let(:address) { stub_model(Spree::Address, state: state) } specify { address.state_text.should == 'va' } end context 'only name is present' do - let(:state) { stub_model(Spree::State, :name => 'virginia', :abbr => nil) } - let(:address) { stub_model(Spree::Address, :state => state) } + let(:state) { stub_model(Spree::State, name: 'virginia', abbr: nil) } + let(:address) { stub_model(Spree::Address, state: state) } specify { address.state_text.should == 'virginia' } end end context "defines require_phone? helper method" do let(:address) { stub_model(Spree::Address) } - specify { address.instance_eval{ require_phone? }.should be_true} + specify { address.instance_eval{ require_phone? }.should be_true } end end diff --git a/spec/models/spree/shipping_rate_spec.rb b/spec/models/spree/shipping_rate_spec.rb index c66ffd62e9..f1f0ca09e6 100644 --- a/spec/models/spree/shipping_rate_spec.rb +++ b/spec/models/spree/shipping_rate_spec.rb @@ -1,14 +1,16 @@ -# encoding: utf-8 +# frozen_string_literal: true require 'spec_helper' describe Spree::ShippingRate do let(:shipment) { create(:shipment) } let(:shipping_method) { create(:shipping_method) } - let(:shipping_rate) { Spree::ShippingRate.new(:shipment => shipment, - :shipping_method => shipping_method, - :cost => 10.55) } - before { Spree::TaxRate.stub(:default => 0.05) } + let(:shipping_rate) { + Spree::ShippingRate.new(shipment: shipment, + shipping_method: shipping_method, + cost: 10.55) + } + before { Spree::TaxRate.stub(default: 0.05) } context "#display_price" do context "when shipment includes VAT" do @@ -26,9 +28,11 @@ describe Spree::ShippingRate do end context "when the currency is JPY" do - let(:shipping_rate) { shipping_rate = Spree::ShippingRate.new(:cost => 205) - shipping_rate.stub(:currency => "JPY") - shipping_rate } + let(:shipping_rate) { + shipping_rate = Spree::ShippingRate.new(cost: 205) + shipping_rate.stub(currency: "JPY") + shipping_rate + } it "displays the price in yen" do shipping_rate.display_price.to_s.should == "¥205"