From 6df97245271884a8c2172d9fa6f8fab9a62c256b Mon Sep 17 00:00:00 2001 From: Sylvain Date: Tue, 11 Oct 2022 11:43:32 +0200 Subject: [PATCH] (quality) disable Rails/RedundantPresenceValidationOnBelongsTo This setting was disabled in config/application.rb: `config.active_record.belongs_to_required_by_default = false`, so we must configure the linter accordingly. Also: linting some little bit of code --- .rubocop.yml | 2 ++ app/controllers/api/wallet_controller.rb | 4 ++-- app/models/wallet.rb | 6 ++---- app/models/wallet_transaction.rb | 1 + 4 files changed, 7 insertions(+), 6 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index dfafb9e72..a7f761036 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -36,3 +36,5 @@ Style/FormatString: EnforcedStyle: sprintf Style/FormatStringToken: EnforcedStyle: template +Rails/RedundantPresenceValidationOnBelongsTo: + Enabled: false diff --git a/app/controllers/api/wallet_controller.rb b/app/controllers/api/wallet_controller.rb index 75e450e55..159ee0a7e 100644 --- a/app/controllers/api/wallet_controller.rb +++ b/app/controllers/api/wallet_controller.rb @@ -18,7 +18,7 @@ class API::WalletController < API::ApiController end def credit - return head 422 unless Setting.get('wallet_module') + return head :unprocessable_entity unless Setting.get('wallet_module') @wallet = Wallet.find(credit_params[:id]) authorize @wallet @@ -28,7 +28,7 @@ class API::WalletController < API::ApiController service.create_avoir(transaction, credit_params[:avoir_date], credit_params[:avoir_description]) if credit_params[:avoir] render :show else - head 422 + head :unprocessable_entity end end diff --git a/app/models/wallet.rb b/app/models/wallet.rb index 3ff32dffa..e4c76a018 100644 --- a/app/models/wallet.rb +++ b/app/models/wallet.rb @@ -11,6 +11,8 @@ class Wallet < ApplicationRecord validates :invoicing_profile, presence: true + delegate :user, to: :invoicing_profile + def credit(amount) if amount.is_a?(Numeric) && amount >= 0 self.amount += amount @@ -26,8 +28,4 @@ class Wallet < ApplicationRecord end false end - - def user - invoicing_profile.user - end end diff --git a/app/models/wallet_transaction.rb b/app/models/wallet_transaction.rb index 9ad84a30f..e95d08b0a 100644 --- a/app/models/wallet_transaction.rb +++ b/app/models/wallet_transaction.rb @@ -14,6 +14,7 @@ class WalletTransaction < ApplicationRecord has_one :invoice_item, as: :object, dependent: :destroy validates :transaction_type, inclusion: { in: %w[credit debit] } + validates :invoicing_profile, :wallet, presence: true delegate :user, to: :invoicing_profile