Skip to content

Commit

Permalink
Fix RedundantPresenceValidationOnBelongs on some files (part VI)
Browse files Browse the repository at this point in the history
- presence: true is redundant since Rails 5.0 BUT applies
   with new default config of
   belongs_to_required_by_default to true.
   Lots of files with belongs_to_required_by_default = false
   (backward compatibility).
   So: deleting this setting implies to adding optional: true
 - added 'NOT NULL' constraints so model constraints match
   with contraints on DB tables.
 - corresponding migration files to match AR Models &
   DB tables
 - rake tasks to check corrupt data (ie: NULL/nil in id fields)
   (previous commit)
 - updated the todo
  • Loading branch information
cyrillefr committed May 29, 2024
1 parent 65fc144 commit 4bb996e
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 9 deletions.
3 changes: 1 addition & 2 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -642,11 +642,10 @@ Rails/RedundantActiveRecordAllMethod:
- 'app/models/spree/variant.rb'
- 'spec/system/admin/product_import_spec.rb'

# Offense count: 4
# Offense count: 3
# This cop supports unsafe autocorrection (--autocorrect-all).
Rails/RedundantPresenceValidationOnBelongsTo:
Exclude:
- 'app/models/spree/line_item.rb'
- 'app/models/spree/order.rb'

# Offense count: 1
Expand Down
5 changes: 1 addition & 4 deletions app/models/spree/line_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ class LineItem < ApplicationRecord
include VariantUnits::VariantAndLineItemNaming
include LineItemStockChanges

self.belongs_to_required_by_default = false

searchable_attributes :price, :quantity, :order_id, :variant_id, :tax_category_id
searchable_associations :order, :order_cycle, :variant, :product, :supplier, :tax_category
searchable_scopes :with_tax, :without_tax
Expand All @@ -19,7 +17,7 @@ class LineItem < ApplicationRecord
belongs_to :variant, -> { with_deleted }, class_name: "Spree::Variant"
has_one :product, through: :variant
has_one :supplier, through: :product
belongs_to :tax_category, class_name: "Spree::TaxCategory"
belongs_to :tax_category, class_name: "Spree::TaxCategory", optional: true

has_many :adjustments, as: :adjustable, dependent: :destroy

Expand All @@ -28,7 +26,6 @@ class LineItem < ApplicationRecord
before_validation :copy_tax_category
before_validation :copy_dimensions

validates :variant, presence: true
validates :quantity, numericality: {
only_integer: true,
greater_than: -1,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class RequireOrderAndVariantOnLineItem < ActiveRecord::Migration[7.0]
def change
change_column_null :spree_line_items, :order_id, false
change_column_null :spree_line_items, :variant_id, false
end
end
6 changes: 3 additions & 3 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.0].define(version: 2024_05_17_121235) do
ActiveRecord::Schema[7.0].define(version: 2024_05_29_081209) do
# These are extensions that must be enabled in order to support this database
enable_extension "pg_stat_statements"
enable_extension "plpgsql"
Expand Down Expand Up @@ -541,8 +541,8 @@
end

create_table "spree_line_items", id: :serial, force: :cascade do |t|
t.integer "order_id"
t.integer "variant_id"
t.integer "order_id", null: false
t.integer "variant_id", null: false
t.integer "quantity", null: false
t.decimal "price", precision: 10, scale: 2, null: false
t.datetime "created_at", precision: nil, null: false
Expand Down

0 comments on commit 4bb996e

Please sign in to comment.