Safely autocorrect Rails/ActiveRecordCallbacksOrder

Inspecting 1481 files
...........................................................................................................................................................................................................................................................C..C.CC........................................................................C...C..........C..C..................CC........................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................

Offenses:

app/models/customer.rb:32:3: C: [Corrected] Rails/ActiveRecordCallbacksOrder: before_validation is supposed to appear before before_destroy.
  before_validation :downcase_email
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
app/models/customer.rb:33:3: C: [Corrected] Rails/ActiveRecordCallbacksOrder: before_validation is supposed to appear before before_destroy.
  before_validation :empty_code
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
app/models/customer.rb:34:1: C: [Corrected] Layout/EmptyLines: Extra blank line detected.
app/models/customer.rb:49:1: C: [Corrected] Layout/EmptyLines: Extra blank line detected.
app/models/customer.rb:49:3: C: [Corrected] Rails/ActiveRecordCallbacksOrder: before_create is supposed to appear before before_destroy.
  before_create :associate_user
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
app/models/enterprise.rb:129:3: C: [Corrected] Rails/ActiveRecordCallbacksOrder: after_create is supposed to appear before after_touch.
  after_create :set_default_contact
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
app/models/enterprise.rb:130:3: C: [Corrected] Rails/ActiveRecordCallbacksOrder: after_create is supposed to appear before after_touch.
  after_create :relate_to_owners_enterprises
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
app/models/enterprise.rb:133:3: C: [Corrected] Rails/ActiveRecordCallbacksOrder: after_rollback is supposed to appear before after_touch.
  after_rollback :restore_permalink
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
app/models/enterprise.rb:134:1: C: [Corrected] Layout/EmptyLines: Extra blank line detected.
app/models/enterprise_group.rb:18:3: C: [Corrected] Rails/ActiveRecordCallbacksOrder: after_save is supposed to appear before after_find.
  after_save :unset_undefined_address_fields
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
app/models/enterprise_group.rb:18:3: C: [Corrected] Rails/ActiveRecordCallbacksOrder: before_validation is supposed to appear before after_save.
  before_validation :sanitize_permalink
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
app/models/enterprise_group.rb:19:3: C: [Corrected] Rails/ActiveRecordCallbacksOrder: before_validation is supposed to appear before after_find.
  before_validation :sanitize_permalink
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
app/models/enterprise_group.rb:23:3: C: [Corrected] Rails/ActiveRecordCallbacksOrder: before_validation is supposed to appear before after_save.
  before_validation :sanitize_permalink
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
app/models/enterprise_relationship.rb:15:3: C: [Corrected] Rails/ActiveRecordCallbacksOrder: before_destroy is supposed to appear before after_save.
  before_destroy :revoke_all_child_variant_overrides
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
app/models/enterprise_relationship.rb:16:3: C: [Corrected] Rails/ActiveRecordCallbacksOrder: before_destroy is supposed to appear before after_save.
  before_destroy :destroy_related_exchanges
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
app/models/spree/order.rb:108:5: C: [Corrected] Rails/ActiveRecordCallbacksOrder: before_save is supposed to appear before before_create.
    before_save :update_shipping_fees!, if: :complete?
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
app/models/spree/order.rb:109:5: C: [Corrected] Rails/ActiveRecordCallbacksOrder: before_save is supposed to appear before before_create.
    before_save :update_payment_fees!, if: :complete?
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
app/models/spree/payment.rb:30:5: C: [Corrected] Rails/ActiveRecordCallbacksOrder: after_initialize is supposed to appear before before_create.
    after_initialize :build_source
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
app/models/spree/payment.rb:32:5: C: [Corrected] Rails/ActiveRecordCallbacksOrder: after_initialize is supposed to appear before after_create.
    after_initialize :build_source
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
app/models/spree/payment.rb:33:5: C: [Corrected] Rails/ActiveRecordCallbacksOrder: after_create is supposed to appear before after_save.
    after_create :invalidate_old_payments
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
app/models/spree/payment.rb:34:5: C: [Corrected] Rails/ActiveRecordCallbacksOrder: after_initialize is supposed to appear before after_save.
    after_initialize :build_source
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
app/models/spree/payment.rb:35:5: C: [Corrected] Rails/ActiveRecordCallbacksOrder: after_create is supposed to appear before after_save.
    after_create :invalidate_old_payments
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
app/models/spree/payment.rb:36:5: C: [Corrected] Rails/ActiveRecordCallbacksOrder: after_initialize is supposed to appear before after_save.
    after_initialize :build_source
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
app/models/spree/payment.rb:46:5: C: [Corrected] Rails/ActiveRecordCallbacksOrder: after_initialize is supposed to appear before after_create.
    after_initialize :build_source
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
app/models/spree/payment.rb:47:1: C: [Corrected] Layout/EmptyLines: Extra blank line detected.
app/models/spree/product.rb:87:5: C: [Corrected] Rails/ActiveRecordCallbacksOrder: after_create is supposed to appear before after_save.
    after_create :ensure_standard_variant
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
app/models/spree/return_authorization.rb:12:5: C: [Corrected] Rails/ActiveRecordCallbacksOrder: before_save is supposed to appear before before_create.
    before_save :force_positive_amount
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
app/models/spree/user.rb:49:5: C: [Corrected] Rails/ActiveRecordCallbacksOrder: after_create is supposed to appear before before_destroy.
    after_create :associate_customers, :associate_orders
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
app/models/spree/user.rb:50:1: C: [Corrected] Layout/EmptyLines: Extra blank line detected.
app/models/spree/variant.rb:88:5: C: [Corrected] Rails/ActiveRecordCallbacksOrder: after_create is supposed to appear before after_save.
    after_create :create_stock_items
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
app/models/spree/variant.rb:89:5: C: [Corrected] Rails/ActiveRecordCallbacksOrder: after_create is supposed to appear before after_save.
    after_create :set_position
    ^^^^^^^^^^^^^^^^^^^^^^^^^^
app/models/spree/variant.rb:90:1: C: [Corrected] Layout/EmptyLines: Extra blank line detected.
app/models/spree/variant.rb:91:1: C: [Corrected] Layout/EmptyLines: Extra blank line detected.
app/models/spree/variant.rb:91:5: C: [Corrected] Rails/ActiveRecordCallbacksOrder: around_destroy is supposed to appear before after_save.
    around_destroy :destruction
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^
app/models/spree/variant.rb:92:1: C: [Corrected] Layout/EmptyLines: Extra blank line detected.

1481 files inspected, 35 offenses detected, 35 offenses corrected
This commit is contained in:
Neal Chambers
2023-08-01 10:00:24 +09:00
parent a1560263a1
commit 44cf2faa1e
11 changed files with 16 additions and 40 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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") }

View File

@@ -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

View File

@@ -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("

View File

@@ -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

View File

@@ -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') }

View File

@@ -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) {

View File

@@ -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

View File

@@ -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? }

View File

@@ -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') }