diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 91b958b867..9fd43b1e0a 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -443,23 +443,6 @@ Naming/VariableNumber: - 'spec/models/spree/tax_rate_spec.rb' - 'spec/requests/api/orders_spec.rb' -# Offense count: 12 -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: Include. -# Include: app/models/**/*.rb -Rails/ActiveRecordCallbacksOrder: - Exclude: - - 'app/models/customer.rb' - - 'app/models/enterprise.rb' - - 'app/models/enterprise_group.rb' - - 'app/models/enterprise_relationship.rb' - - 'app/models/spree/order.rb' - - 'app/models/spree/payment.rb' - - 'app/models/spree/product.rb' - - 'app/models/spree/return_authorization.rb' - - 'app/models/spree/user.rb' - - 'app/models/spree/variant.rb' - # Offense count: 1 # Configuration parameters: Severity, Include. # Include: app/models/**/*.rb diff --git a/app/models/customer.rb b/app/models/customer.rb index 8157681269..d06162366f 100644 --- a/app/models/customer.rb +++ b/app/models/customer.rb @@ -19,6 +19,9 @@ class Customer < ApplicationRecord belongs_to :enterprise belongs_to :user, class_name: "Spree::User", optional: true has_many :orders, class_name: "Spree::Order" + before_validation :downcase_email + before_validation :empty_code + before_create :associate_user before_destroy :update_orders_and_delete_canceled_subscriptions belongs_to :bill_address, class_name: "Spree::Address", optional: true @@ -29,9 +32,6 @@ class Customer < ApplicationRecord alias_attribute :shipping_address, :ship_address accepts_nested_attributes_for :ship_address - before_validation :downcase_email - before_validation :empty_code - validates :code, uniqueness: { scope: :enterprise_id, allow_nil: true } validates :email, presence: true, 'valid_email_2/email': true, uniqueness: { @@ -46,8 +46,6 @@ class Customer < ApplicationRecord scope :created_manually, -> { where(created_manually: true) } scope :visible, -> { where(id: Spree::Order.complete.select(:customer_id)).or(created_manually) } - before_create :associate_user - attr_accessor :gateway_recurring_payment_client_secret, :gateway_shop_id def full_name diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index 99ce5c85c7..71a3152585 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -125,12 +125,11 @@ class Enterprise < ApplicationRecord before_validation :set_unused_address_fields after_validation :ensure_owner_is_manager, if: lambda { owner_id_changed? && !owner_id.nil? } - after_touch :touch_distributors after_create :set_default_contact after_create :relate_to_owners_enterprises - after_create_commit :send_welcome_email - after_rollback :restore_permalink + after_touch :touch_distributors + after_create_commit :send_welcome_email scope :by_name, -> { order('name') } scope :visible, -> { where(visible: "public") } diff --git a/app/models/enterprise_group.rb b/app/models/enterprise_group.rb index 4221776554..38b7458aa2 100644 --- a/app/models/enterprise_group.rb +++ b/app/models/enterprise_group.rb @@ -14,13 +14,13 @@ class EnterpriseGroup < ApplicationRecord validates :address, presence: true, associated: true before_validation :set_undefined_address_fields before_validation :set_unused_address_fields - after_find :unset_undefined_address_fields + before_validation :sanitize_permalink after_save :unset_undefined_address_fields + after_find :unset_undefined_address_fields validates :name, presence: true validates :description, presence: true - before_validation :sanitize_permalink validates :permalink, uniqueness: true, presence: true delegate :phone, :address1, :address2, :city, :zipcode, :state, :country, to: :address diff --git a/app/models/enterprise_relationship.rb b/app/models/enterprise_relationship.rb index e8590215fa..30bddcc31d 100644 --- a/app/models/enterprise_relationship.rb +++ b/app/models/enterprise_relationship.rb @@ -11,9 +11,9 @@ class EnterpriseRelationship < ApplicationRecord message: I18n.t('validation_msg_relationship_already_established') } - after_save :update_permissions_of_child_variant_overrides before_destroy :revoke_all_child_variant_overrides before_destroy :destroy_related_exchanges + after_save :update_permissions_of_child_variant_overrides scope :with_enterprises, -> { joins(" diff --git a/app/models/spree/order.rb b/app/models/spree/order.rb index dead0129fc..38189703cf 100644 --- a/app/models/spree/order.rb +++ b/app/models/spree/order.rb @@ -104,9 +104,9 @@ module Spree before_validation :clone_billing_address, if: :use_billing? before_validation :ensure_customer - before_create :link_by_email before_save :update_shipping_fees!, if: :complete? before_save :update_payment_fees!, if: :complete? + before_create :link_by_email after_create :create_tax_charge! after_save :reapply_tax_on_changed_address diff --git a/app/models/spree/payment.rb b/app/models/spree/payment.rb index 8b4baeac05..21d5a55166 100644 --- a/app/models/spree/payment.rb +++ b/app/models/spree/payment.rb @@ -25,14 +25,15 @@ module Spree has_one :adjustment, as: :adjustable, dependent: :destroy validate :validate_source + after_initialize :build_source before_create :set_unique_identifier + # invalidate previously entered payments + after_create :invalidate_old_payments after_save :create_payment_profile, if: :profiles_supported? # update the order totals, etc. after_save :ensure_correct_adjustment, :update_order - # invalidate previously entered payments - after_create :invalidate_old_payments # Skips the validation of the source (for example, credit card) of the payment. # @@ -43,8 +44,6 @@ module Spree attr_accessor :skip_source_validation attr_accessor :source_attributes - after_initialize :build_source - scope :from_credit_card, -> { where(source_type: 'Spree::CreditCard') } scope :with_state, ->(s) { where(state: s.to_s) } scope :completed, -> { with_state('completed') } diff --git a/app/models/spree/product.rb b/app/models/spree/product.rb index b0b65b956a..4af85b6cd1 100755 --- a/app/models/spree/product.rb +++ b/app/models/spree/product.rb @@ -83,8 +83,8 @@ module Spree before_save :add_primary_taxon_to_taxons - after_save :remove_previous_primary_taxon_from_taxons after_create :ensure_standard_variant + after_save :remove_previous_primary_taxon_from_taxons after_save :update_units scope :with_properties, ->(*property_ids) { diff --git a/app/models/spree/return_authorization.rb b/app/models/spree/return_authorization.rb index 9f353cd2fd..b173e7c679 100644 --- a/app/models/spree/return_authorization.rb +++ b/app/models/spree/return_authorization.rb @@ -8,8 +8,8 @@ module Spree has_many :inventory_units, inverse_of: :return_authorization has_one :stock_location - before_create :generate_number before_save :force_positive_amount + before_create :generate_number validates :order, presence: true validates :amount, numericality: true diff --git a/app/models/spree/user.rb b/app/models/spree/user.rb index c11de4b4b9..1932542edc 100644 --- a/app/models/spree/user.rb +++ b/app/models/spree/user.rb @@ -23,6 +23,7 @@ module Spree has_many :spree_orders, class_name: "Spree::Order" before_validation :set_login + after_create :associate_customers, :associate_orders before_destroy :check_completed_orders roles_table_name = Role.table_name @@ -46,8 +47,6 @@ module Spree accepts_nested_attributes_for :bill_address accepts_nested_attributes_for :ship_address - after_create :associate_customers, :associate_orders - validates :email, 'valid_email_2/email': { mx: true }, if: :email_changed? validate :limit_owned_enterprises validates :uid, uniqueness: true, if: lambda { uid.present? } diff --git a/app/models/spree/variant.rb b/app/models/spree/variant.rb index 0c98ba005d..cb2e1df5e9 100644 --- a/app/models/spree/variant.rb +++ b/app/models/spree/variant.rb @@ -83,12 +83,10 @@ module Spree variant.new_record? || variant.changed_attributes.keys.intersection(NAME_FIELDS).any? } - after_save :save_default_price - after_create :create_stock_items after_create :set_position - around_destroy :destruction + after_save :save_default_price # default variant scope only lists non-deleted variants scope :deleted, lambda { where('deleted_at IS NOT NULL') }