Files
openfoodnetwork/app/models/spree/user.rb
Neal Chambers 44cf2faa1e 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
2023-08-01 22:50:21 +09:00

204 lines
6.2 KiB
Ruby

# frozen_string_literal: true
module Spree
class User < ApplicationRecord
include SetUnusedAddressFields
searchable_attributes :email
devise :database_authenticatable, :token_authenticatable, :registerable, :recoverable,
:rememberable, :trackable, :validatable, :omniauthable,
:encryptable, :confirmable,
encryptor: 'authlogic_sha512', reconfirmable: true,
omniauth_providers: [:openid_connect]
has_many :orders
belongs_to :ship_address, class_name: 'Spree::Address'
belongs_to :bill_address, class_name: 'Spree::Address'
has_and_belongs_to_many :spree_roles,
join_table: 'spree_roles_users',
class_name: "Spree::Role"
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
scope :admin, lambda { includes(:spree_roles).where("#{roles_table_name}.name" => "admin") }
has_many :enterprise_roles, dependent: :destroy
has_many :enterprises, through: :enterprise_roles
has_many :owned_enterprises, class_name: 'Enterprise',
foreign_key: :owner_id, inverse_of: :owner
has_many :owned_groups, class_name: 'EnterpriseGroup',
foreign_key: :owner_id, inverse_of: :owner
has_many :customers
has_many :credit_cards
has_many :report_rendering_options, class_name: "::ReportRenderingOptions", dependent: :destroy
has_many :webhook_endpoints, dependent: :destroy
accepts_nested_attributes_for :enterprise_roles, allow_destroy: true
accepts_nested_attributes_for :webhook_endpoints
accepts_nested_attributes_for :bill_address
accepts_nested_attributes_for :ship_address
validates :email, 'valid_email_2/email': { mx: true }, if: :email_changed?
validate :limit_owned_enterprises
validates :uid, uniqueness: true, if: lambda { uid.present? }
# Same validation as in the openid_connect gem.
# This validator is totally outdated but we indirectly depend on it.
validates :uid, email: true, if: lambda { uid.present? }
class DestroyWithOrdersError < StandardError; end
def self.admin_created?
User.admin.count > 0
end
def link_from_omniauth(auth)
update!(provider: auth.provider, uid: auth.uid)
end
# Whether a user has a role or not.
def has_spree_role?(role_in_question)
spree_roles.where(name: role_in_question.to_s).any?
end
# Checks whether the specified user is a superadmin, with full control of the instance
def admin?
has_spree_role?('admin')
end
# Send devise-based user emails asyncronously via ActiveJob
# See: https://github.com/heartcombo/devise/tree/v3.5.10#activejob-integration
def send_devise_notification(notification, *args)
devise_mailer.public_send(notification, self, *args).deliver_later
end
def regenerate_reset_password_token
set_reset_password_token
end
def generate_api_key
self.spree_api_key = SecureRandom.hex(24)
end
def known_users
if admin?
Spree::User.where(nil)
else
Spree::User
.includes(:enterprises)
.references(:enterprises)
.where("enterprises.id IN (SELECT enterprise_id FROM enterprise_roles WHERE user_id = ?)",
id)
end
end
def build_enterprise_roles
Enterprise.all.find_each do |enterprise|
unless enterprise_roles.find_by enterprise_id: enterprise.id
enterprise_roles.build(enterprise: enterprise)
end
end
end
def customer_of(enterprise)
return nil unless enterprise
customers.find_by(enterprise_id: enterprise)
end
# This is a Devise Confirmable callback that runs on email confirmation
# It sends a welcome email after the user email is confirmed
def after_confirmation
return unless confirmed? && unconfirmed_email.nil? && !unconfirmed_email_changed?
send_signup_confirmation
end
def send_signup_confirmation
Spree::UserMailer.signup_confirmation(self).deliver_later
end
def associate_customers
self.customers = Customer.where(email: email)
end
def associate_orders
Spree::Order.where(customer: customers).find_each do |order|
order.associate_user!(self)
end
end
def can_own_more_enterprises?
owned_enterprises.reload.size < enterprise_limit
end
def default_card
# Don't re-fetch associated cards from the DB if they're already eager-loaded
if credit_cards.loaded?
credit_cards.to_a.find(&:is_default)
else
credit_cards.where(is_default: true).first
end
end
def last_incomplete_spree_order
spree_orders.incomplete.where(created_by_id: id).order('created_at DESC').first
end
def disabled
disabled_at.present?
end
def disabled=(value)
self.disabled_at = value == '1' ? Time.zone.now : nil
end
protected
def password_required?
!persisted? || password.present? || password_confirmation.present?
end
private
def check_completed_orders
raise DestroyWithOrdersError if orders.complete.present?
end
def set_login
# for now force login to be same as email, eventually we will make this configurable, etc.
self.login ||= email if email
end
# Generate a friendly string randomically to be used as token.
def self.friendly_token
SecureRandom.base64(15).tr('+/=', '-_ ').strip.delete("\n")
end
def limit_owned_enterprises
return unless owned_enterprises.size > enterprise_limit
errors.add(:owned_enterprises, I18n.t(:spree_user_enterprise_limit_error,
email: email,
enterprise_limit: enterprise_limit))
end
def remove_payments_in_checkout(enterprises)
enterprises.each do |enterprise|
enterprise.distributed_orders.each do |order|
order.payments.keep_if { |payment| payment.state != "checkout" }
end
end
end
end
end