diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 8ed82cc4a7..3092909883 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -660,15 +660,10 @@ Rails/RedundantActiveRecordAllMethod: - 'app/models/spree/variant.rb' - 'spec/system/admin/product_import_spec.rb' -# Offense count: 20 +# Offense count: 14 # This cop supports unsafe autocorrection (--autocorrect-all). Rails/RedundantPresenceValidationOnBelongsTo: Exclude: - - 'app/models/enterprise_fee.rb' - - 'app/models/exchange.rb' - - 'app/models/inventory_item.rb' - - 'app/models/order_cycle.rb' - - 'app/models/spree/address.rb' - 'app/models/spree/line_item.rb' - 'app/models/spree/order.rb' - 'app/models/spree/product_property.rb' diff --git a/app/models/enterprise_fee.rb b/app/models/enterprise_fee.rb index a4bcdd272d..16f61a3cd3 100644 --- a/app/models/enterprise_fee.rb +++ b/app/models/enterprise_fee.rb @@ -21,7 +21,6 @@ class EnterpriseFee < ApplicationRecord validates :fee_type, inclusion: { in: FEE_TYPES } validates :name, presence: true - validates :enterprise_id, presence: true before_save :ensure_valid_tax_category_settings diff --git a/app/models/exchange.rb b/app/models/exchange.rb index 796a251ed1..32aa3237ab 100644 --- a/app/models/exchange.rb +++ b/app/models/exchange.rb @@ -10,8 +10,6 @@ # shopfront (outgoing products). But the set of shown products can be smaller # than all incoming products. class Exchange < ApplicationRecord - self.belongs_to_required_by_default = false - acts_as_taggable belongs_to :order_cycle @@ -24,7 +22,6 @@ class Exchange < ApplicationRecord has_many :exchange_fees, dependent: :destroy has_many :enterprise_fees, through: :exchange_fees - validates :order_cycle, :sender, :receiver, presence: true validates :sender_id, uniqueness: { scope: [:order_cycle_id, :receiver_id, :incoming] } before_destroy :delete_related_exchange_variants, prepend: true diff --git a/app/models/inventory_item.rb b/app/models/inventory_item.rb index d013ea52f2..8d90a4271f 100644 --- a/app/models/inventory_item.rb +++ b/app/models/inventory_item.rb @@ -1,14 +1,10 @@ # frozen_string_literal: true class InventoryItem < ApplicationRecord - self.belongs_to_required_by_default = false - belongs_to :enterprise belongs_to :variant, class_name: "Spree::Variant" validates :variant_id, uniqueness: { scope: :enterprise_id } - validates :enterprise, presence: true - validates :variant, presence: true validates :visible, inclusion: { in: [true, false], message: I18n.t(:inventory_item_visibility_error) } diff --git a/app/models/order_cycle.rb b/app/models/order_cycle.rb index e6b0d1dff1..7b71a3a79e 100644 --- a/app/models/order_cycle.rb +++ b/app/models/order_cycle.rb @@ -3,8 +3,6 @@ require 'open_food_network/scope_variant_to_hub' class OrderCycle < ApplicationRecord - self.belongs_to_required_by_default = false - searchable_attributes :orders_open_at, :orders_close_at, :coordinator_id searchable_scopes :active, :inactive, :active_or_complete, :upcoming, :closed, :not_closed, :dated, :undated, :soonest_opening, :soonest_closing, :most_recently_closed @@ -44,7 +42,7 @@ class OrderCycle < ApplicationRecord before_update :reset_processed_at, if: :will_save_change_to_orders_close_at? after_save :sync_subscriptions, if: :opening? - validates :name, :coordinator_id, presence: true + validates :name, presence: true validate :orders_close_at_after_orders_open_at? preference :product_selection_from_coordinator_inventory_only, :boolean, default: false diff --git a/app/models/spree/address.rb b/app/models/spree/address.rb index 59363374f1..9c305be78d 100644 --- a/app/models/spree/address.rb +++ b/app/models/spree/address.rb @@ -4,19 +4,17 @@ module Spree class Address < ApplicationRecord include AddressDisplay - self.belongs_to_required_by_default = false - searchable_attributes :firstname, :lastname, :phone, :full_name, :full_name_reversed, :full_name_with_comma, :full_name_with_comma_reversed searchable_associations :country, :state belongs_to :country, class_name: "Spree::Country" - belongs_to :state, class_name: "Spree::State" + belongs_to :state, class_name: "Spree::State", optional: true has_one :enterprise, dependent: :restrict_with_exception has_many :shipments - validates :address1, :city, :country, :phone, presence: true + validates :address1, :city, :phone, presence: true validates :company, presence: true, unless: -> { first_name.blank? || last_name.blank? } validates :firstname, :lastname, presence: true, if: -> do company.blank? || company == 'unused' diff --git a/db/migrate/20240422140057_require_order_cycle_and_sender_and_receiver_on_exchange.rb b/db/migrate/20240422140057_require_order_cycle_and_sender_and_receiver_on_exchange.rb new file mode 100644 index 0000000000..b159516a3a --- /dev/null +++ b/db/migrate/20240422140057_require_order_cycle_and_sender_and_receiver_on_exchange.rb @@ -0,0 +1,7 @@ +class RequireOrderCycleAndSenderAndReceiverOnExchange < ActiveRecord::Migration[7.0] + def change + change_column_null :exchanges, :order_cycle_id, false + change_column_null :exchanges, :sender_id, false + change_column_null :exchanges, :receiver_id, false + end +end diff --git a/db/migrate/20240422145353_require_name_and_coordinator_on_order_cycle.rb b/db/migrate/20240422145353_require_name_and_coordinator_on_order_cycle.rb new file mode 100644 index 0000000000..a58db5462d --- /dev/null +++ b/db/migrate/20240422145353_require_name_and_coordinator_on_order_cycle.rb @@ -0,0 +1,6 @@ +class RequireNameAndCoordinatorOnOrderCycle < ActiveRecord::Migration[7.0] + def change + change_column_null :order_cycles, :name, false + change_column_null :order_cycles, :coordinator_id, false + end +end diff --git a/db/migrate/20240422150502_require_address1_and_city_and_phone_and_country_and_on_address.rb b/db/migrate/20240422150502_require_address1_and_city_and_phone_and_country_and_on_address.rb new file mode 100644 index 0000000000..f200225349 --- /dev/null +++ b/db/migrate/20240422150502_require_address1_and_city_and_phone_and_country_and_on_address.rb @@ -0,0 +1,8 @@ +class RequireAddress1AndCityAndPhoneAndCountryAndOnAddress < ActiveRecord::Migration[7.0] + def change + change_column_null :spree_addresses, :address1, false + change_column_null :spree_addresses, :city, false + change_column_null :spree_addresses, :phone, false + change_column_null :spree_addresses, :country_id, false + end +end diff --git a/db/schema.rb b/db/schema.rb index 51491dce7f..68d8d5206d 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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_02_13_044159) do +ActiveRecord::Schema[7.0].define(version: 2024_04_22_150502) do # These are extensions that must be enabled in order to support this database enable_extension "pg_stat_statements" enable_extension "plpgsql" @@ -258,9 +258,9 @@ ActiveRecord::Schema[7.0].define(version: 2024_02_13_044159) do end create_table "exchanges", id: :serial, force: :cascade do |t| - t.integer "order_cycle_id" - t.integer "sender_id" - t.integer "receiver_id" + t.integer "order_cycle_id", null: false + t.integer "sender_id", null: false + t.integer "receiver_id", null: false t.text "pickup_time" t.text "pickup_instructions" t.datetime "created_at", precision: nil, null: false @@ -330,10 +330,10 @@ ActiveRecord::Schema[7.0].define(version: 2024_02_13_044159) do end create_table "order_cycles", id: :serial, force: :cascade do |t| - t.string "name", limit: 255 + t.string "name", limit: 255, null: false t.datetime "orders_open_at", precision: nil t.datetime "orders_close_at", precision: nil - t.integer "coordinator_id" + t.integer "coordinator_id", null: false t.datetime "created_at", precision: nil, null: false t.datetime "updated_at", precision: nil, null: false t.datetime "processed_at", precision: nil @@ -420,15 +420,15 @@ ActiveRecord::Schema[7.0].define(version: 2024_02_13_044159) do create_table "spree_addresses", id: :serial, force: :cascade do |t| t.string "firstname", limit: 255 t.string "lastname", limit: 255 - t.string "address1", limit: 255 + t.string "address1", limit: 255, null: false t.string "address2", limit: 255 - t.string "city", limit: 255 + t.string "city", limit: 255, null: false t.string "zipcode", limit: 255 - t.string "phone", limit: 255 + t.string "phone", limit: 255, null: false t.string "state_name", limit: 255 t.string "alternative_phone", limit: 255 t.integer "state_id" - t.integer "country_id" + t.integer "country_id", null: false t.datetime "created_at", precision: nil, null: false t.datetime "updated_at", precision: nil, null: false t.string "company", limit: 255