Fixes Rails/SkipsModelValidations offenses

- increments! & decrement! skip validations
 - replaced increment! method calls
 - one call was for a redefined increment! method
 - the other for a regular(ActiveRecord::Persistence)
 - removes increments/decrements definition now useless
This commit is contained in:
cyrillefr
2024-07-05 16:35:40 +02:00
parent 8d327355f9
commit ce8a2b3251
5 changed files with 5 additions and 37 deletions

View File

@@ -644,14 +644,6 @@ Rails/RootPathnameMethods:
Exclude:
- 'spec/lib/reports/orders_and_fulfillment/order_cycle_customer_totals_report_spec.rb'
# Offense count: 4
# Configuration parameters: ForbiddenMethods, AllowedMethods.
# ForbiddenMethods: decrement!, decrement_counter, increment!, increment_counter, insert, insert!, insert_all, insert_all!, toggle!, touch, touch_all, update_all, update_attribute, update_column, update_columns, update_counters, upsert, upsert_all
Rails/SkipsModelValidations:
Exclude:
- 'app/models/variant_override.rb'
- 'spec/models/spree/line_item_spec.rb'
# Offense count: 7
# This cop supports unsafe autocorrection (--autocorrect-all).
# Configuration parameters: EnforcedStyle.

View File

@@ -1,19 +0,0 @@
# frozen_string_literal: true
# Rails 5 introduced some breaking changes to these built-in methods, and the new versions
# no longer work correctly in relation to decrementing stock with LineItems / VariantOverrides.
# The following methods re-instate the pre-Rails-5 versions, which work as expected.
# https://apidock.com/rails/v4.2.9/ActiveRecord/Persistence/increment%21
# https://apidock.com/rails/v4.2.9/ActiveRecord/Persistence/decrement%21
module LineItemStockChanges
extend ActiveSupport::Concern
def increment!(attribute, by = 1)
increment(attribute, by).update_attribute(attribute, self[attribute])
end
def decrement!(attribute, by = 1)
decrement(attribute, by).update_attribute(attribute, self[attribute])
end
end

View File

@@ -5,7 +5,6 @@ require 'open_food_network/scope_variant_to_hub'
module Spree
class LineItem < ApplicationRecord
include VariantUnits::VariantAndLineItemNaming
include LineItemStockChanges
searchable_attributes :price, :quantity, :order_id, :variant_id, :tax_category_id
searchable_associations :order, :order_cycle, :variant, :product, :supplier, :tax_category

View File

@@ -52,11 +52,7 @@ class VariantOverride < ApplicationRecord
return
end
if quantity > 0
increment! :count_on_hand, quantity
elsif quantity < 0
decrement! :count_on_hand, -quantity
end
update!(count_on_hand: (count_on_hand || 0) + quantity)
end
def default_stock?

View File

@@ -349,7 +349,7 @@ module Spree
it "draws stock from the variant override" do
expect(vo.reload.count_on_hand).to eq 3
expect{ line_item.increment!(:quantity) }
expect{ line_item.update!(quantity: line_item.quantity + 1) }
.not_to change{ Spree::Variant.find(variant.id).on_hand }
expect(vo.reload.count_on_hand).to eq 2
end
@@ -357,9 +357,9 @@ module Spree
context "when a variant override does not apply" do
it "draws stock from the variant" do
expect{ line_item.increment!(:quantity) }.to change{
Spree::Variant.find(variant.id).on_hand
}.by(-1)
expect{ line_item.update!(quantity: line_item.quantity + 1) }.to change{
Spree::Variant.find(variant.id).on_hand
}.by(-1)
end
end
end