Merge pull request #12414 from cyrillefr/RedundantPresenceValidationOnBelongs_part_II

Fix RedundantPresenceValidationOnBelongs on some files (part II)
This commit is contained in:
Gaetan Craig-Riou
2024-05-01 13:00:47 +10:00
committed by GitHub
12 changed files with 87 additions and 18 deletions

View File

@@ -649,15 +649,12 @@ Rails/RedundantActiveRecordAllMethod:
- 'app/models/spree/variant.rb'
- 'spec/system/admin/product_import_spec.rb'
# Offense count: 14
# Offense count: 11
# This cop supports unsafe autocorrection (--autocorrect-all).
Rails/RedundantPresenceValidationOnBelongsTo:
Exclude:
- 'app/models/spree/line_item.rb'
- 'app/models/spree/order.rb'
- 'app/models/spree/product_property.rb'
- 'app/models/spree/return_authorization.rb'
- 'app/models/spree/state.rb'
- 'app/models/spree/stock_item.rb'
- 'app/models/spree/stock_movement.rb'
- 'app/models/spree/tax_rate.rb'

View File

@@ -2,12 +2,9 @@
module Spree
class ProductProperty < ApplicationRecord
self.belongs_to_required_by_default = false
belongs_to :product, class_name: "Spree::Product", touch: true
belongs_to :property, class_name: 'Spree::Property'
validates :property, presence: true
validates :value, length: { maximum: 255 }
default_scope -> { order("#{table_name}.position") }

View File

@@ -2,8 +2,6 @@
module Spree
class ReturnAuthorization < ApplicationRecord
self.belongs_to_required_by_default = false
acts_as_paranoid
belongs_to :order, class_name: 'Spree::Order', inverse_of: :return_authorizations
@@ -13,7 +11,6 @@ module Spree
before_save :force_positive_amount
before_create :generate_number
validates :order, presence: true
validates :amount, numericality: true
validate :must_have_shipped_units

View File

@@ -2,11 +2,9 @@
module Spree
class State < ApplicationRecord
self.belongs_to_required_by_default = false
belongs_to :country, class_name: 'Spree::Country'
validates :country, :name, presence: true
validates :name, presence: true
def self.find_all_by_name_or_abbr(name_or_abbr)
where('name = ? OR abbr = ?', name_or_abbr, name_or_abbr)

View File

@@ -0,0 +1,7 @@
# frozen_string_literal: true
class RequirePropertyOnProductProperty < ActiveRecord::Migration[7.0]
def change
change_column_null :spree_product_properties, :property_id, false
end
end

View File

@@ -0,0 +1,7 @@
# frozen_string_literal: true
class RequireOrderOnReturnAuthorization < ActiveRecord::Migration[7.0]
def change
change_column_null :spree_return_authorizations, :order_id, false
end
end

View File

@@ -0,0 +1,7 @@
# frozen_string_literal: true
class RequireCountryOnState < ActiveRecord::Migration[7.0]
def change
change_column_null :spree_states, :country_id, false
end
end

View File

@@ -0,0 +1,5 @@
class RequireProductOnProductProperty < ActiveRecord::Migration[7.0]
def change
change_column_null :spree_product_properties, :product_id, false
end
end

View File

@@ -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_04_22_150502) do
ActiveRecord::Schema[7.0].define(version: 2024_04_30_075133) do
# These are extensions that must be enabled in order to support this database
enable_extension "pg_stat_statements"
enable_extension "plpgsql"
@@ -674,8 +674,8 @@ ActiveRecord::Schema[7.0].define(version: 2024_04_22_150502) do
create_table "spree_product_properties", id: :serial, force: :cascade do |t|
t.string "value", limit: 255
t.integer "product_id"
t.integer "property_id"
t.integer "product_id", null: false
t.integer "property_id", null: false
t.datetime "created_at", precision: nil, null: false
t.datetime "updated_at", precision: nil, null: false
t.integer "position", default: 0
@@ -718,7 +718,7 @@ ActiveRecord::Schema[7.0].define(version: 2024_04_22_150502) do
t.string "number", limit: 255
t.string "state", limit: 255
t.decimal "amount", precision: 10, scale: 2, default: "0.0", null: false
t.integer "order_id"
t.integer "order_id", null: false
t.text "reason"
t.datetime "created_at", precision: nil, null: false
t.datetime "updated_at", precision: nil, null: false
@@ -814,7 +814,7 @@ ActiveRecord::Schema[7.0].define(version: 2024_04_22_150502) do
create_table "spree_states", id: :serial, force: :cascade do |t|
t.string "name", limit: 255
t.string "abbr", limit: 255
t.integer "country_id"
t.integer "country_id", null: false
end
create_table "spree_stock_items", id: :serial, force: :cascade do |t|

View File

@@ -0,0 +1,18 @@
# frozen_string_literal: true
namespace :ofn do
namespace :data do
desc 'Checking missing country_id in Spree::State'
task check_missing_country_id_in_spree_states: :environment do
puts 'Checking for null country_id'
ids = Spree::State.where(country_id: nil).pluck(:id)
if ids.empty?
puts 'No NULL country_id found in spree_states'
else
puts 'NULL country_ids s have been found in spree_states:'
print ids
end
end
end
end

View File

@@ -0,0 +1,18 @@
# frozen_string_literal: true
namespace :ofn do
namespace :data do
desc 'Checking order_id in ReturnAuthorization'
task check_missing_order_id_in_return_authorizations: :environment do
puts 'Checking for null order_id'
ids = Spree::ReturnAuthorization.where(order_id: nil).pluck(:id)
if ids.empty?
puts 'No NULL order_id found in spree_return_authorizations'
else
puts 'NULL order_id s have been found in spree_return_authorizations:'
print ids
end
end
end
end

View File

@@ -0,0 +1,18 @@
# frozen_string_literal: true
namespace :ofn do
namespace :data do
desc 'Checking missing property_id in ProductProperty'
task check_missing_property_in_joint_table_product_property: :environment do
puts 'Checking for null property_id'
ids = Spree::ProductProperty.where(property_id: nil).pluck(:id)
if ids.empty?
puts 'No NULL property_id found in spree_product_properties'
else
puts 'NULL property_ids s have been found in spree_product_properties:'
print ids
end
end
end
end