Skip to content

Commit

Permalink
Merge pull request #3309 from tvdeyen/make-belongs-to-optional
Browse files Browse the repository at this point in the history
Make all belongs_to associations optional
  • Loading branch information
tvdeyen authored Aug 22, 2019
2 parents 987c1f0 + 56cb3ef commit 82fb316
Show file tree
Hide file tree
Showing 69 changed files with 143 additions and 145 deletions.
2 changes: 1 addition & 1 deletion core/app/models/concerns/spree/user_address_book.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def mark_default(user_address)
has_many :addresses, through: :user_addresses

# bill_address is only minimally used now, but we can't get rid of it without a major version release
belongs_to :bill_address, class_name: 'Spree::Address'
belongs_to :bill_address, class_name: 'Spree::Address', optional: true

has_one :default_user_address, ->{ default }, class_name: 'Spree::UserAddress', foreign_key: 'user_id'
has_one :default_address, through: :default_user_address, source: :address
Expand Down
4 changes: 2 additions & 2 deletions core/app/models/spree/address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ module Spree
class Address < Spree::Base
extend ActiveModel::ForbiddenAttributesProtection

belongs_to :country, class_name: "Spree::Country"
belongs_to :state, class_name: "Spree::State"
belongs_to :country, class_name: "Spree::Country", optional: true
belongs_to :state, class_name: "Spree::State", optional: true

validates :firstname, :address1, :city, :country_id, presence: true
validates :zipcode, presence: true, if: :require_zipcode?
Expand Down
10 changes: 5 additions & 5 deletions core/app/models/spree/adjustment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ module Spree
# order's adjustment total. This allows an adjustment to be preserved if
# it becomes ineligible so it might be reinstated.
class Adjustment < Spree::Base
belongs_to :adjustable, polymorphic: true, touch: true
belongs_to :source, polymorphic: true
belongs_to :order, class_name: 'Spree::Order', inverse_of: :all_adjustments
belongs_to :promotion_code, class_name: 'Spree::PromotionCode'
belongs_to :adjustment_reason, class_name: 'Spree::AdjustmentReason', inverse_of: :adjustments
belongs_to :adjustable, polymorphic: true, touch: true, optional: true
belongs_to :source, polymorphic: true, optional: true
belongs_to :order, class_name: 'Spree::Order', inverse_of: :all_adjustments, optional: true
belongs_to :promotion_code, class_name: 'Spree::PromotionCode', optional: true
belongs_to :adjustment_reason, class_name: 'Spree::AdjustmentReason', inverse_of: :adjustments, optional: true

validates :adjustable, presence: true
validates :order, presence: true
Expand Down
2 changes: 1 addition & 1 deletion core/app/models/spree/asset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Spree
class Asset < Spree::Base
belongs_to :viewable, polymorphic: true, touch: true
belongs_to :viewable, polymorphic: true, touch: true, optional: true
acts_as_list scope: [:viewable_id, :viewable_type]
end
end
2 changes: 0 additions & 2 deletions core/app/models/spree/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ class Spree::Base < ActiveRecord::Base

include Spree::RansackableAttributes

self.belongs_to_required_by_default = false

def initialize_preference_defaults
if has_attribute?(:preferences)
self.preferences = default_preferences.merge(preferences)
Expand Down
2 changes: 1 addition & 1 deletion core/app/models/spree/calculator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Spree
class Calculator < Spree::Base
belongs_to :calculable, polymorphic: true
belongs_to :calculable, polymorphic: true, optional: true

# This method calls a compute_<computable> method. must be overriden in concrete calculator.
#
Expand Down
6 changes: 3 additions & 3 deletions core/app/models/spree/carton.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# frozen_string_literal: true

class Spree::Carton < Spree::Base
belongs_to :address, class_name: 'Spree::Address'
belongs_to :stock_location, class_name: 'Spree::StockLocation', inverse_of: :cartons
belongs_to :shipping_method, class_name: 'Spree::ShippingMethod', inverse_of: :cartons
belongs_to :address, class_name: 'Spree::Address', optional: true
belongs_to :stock_location, class_name: 'Spree::StockLocation', inverse_of: :cartons, optional: true
belongs_to :shipping_method, class_name: 'Spree::ShippingMethod', inverse_of: :cartons, optional: true

has_many :inventory_units, class_name: "Spree::InventoryUnit", inverse_of: :carton, dependent: :nullify
has_many :orders, -> { distinct }, through: :inventory_units
Expand Down
4 changes: 2 additions & 2 deletions core/app/models/spree/classification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ module Spree
class Classification < Spree::Base
self.table_name = 'spree_products_taxons'
acts_as_list scope: :taxon
belongs_to :product, class_name: "Spree::Product", inverse_of: :classifications, touch: true
belongs_to :taxon, class_name: "Spree::Taxon", inverse_of: :classifications, touch: true
belongs_to :product, class_name: "Spree::Product", inverse_of: :classifications, touch: true, optional: true
belongs_to :taxon, class_name: "Spree::Taxon", inverse_of: :classifications, touch: true, optional: true

# For https://github.com/spree/spree/issues/3494
validates_uniqueness_of :taxon_id, scope: :product_id, message: :already_linked
Expand Down
4 changes: 2 additions & 2 deletions core/app/models/spree/credit_card.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ module Spree
# The default `source` of a `Spree::Payment`.
#
class CreditCard < Spree::PaymentSource
belongs_to :user, class_name: Spree::UserClassHandle.new, foreign_key: 'user_id'
belongs_to :address
belongs_to :user, class_name: Spree::UserClassHandle.new, foreign_key: 'user_id', optional: true
belongs_to :address, optional: true

before_save :set_last_digits

Expand Down
2 changes: 1 addition & 1 deletion core/app/models/spree/customer_return.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Spree
class CustomerReturn < Spree::Base
belongs_to :stock_location
belongs_to :stock_location, optional: true

has_many :return_items, inverse_of: :customer_return
has_many :return_authorizations, through: :return_items
Expand Down
8 changes: 4 additions & 4 deletions core/app/models/spree/inventory_unit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ class InventoryUnit < Spree::Base
POST_SHIPMENT_STATES = %w(returned)
CANCELABLE_STATES = ['on_hand', 'backordered', 'shipped']

belongs_to :variant, -> { with_deleted }, class_name: "Spree::Variant", inverse_of: :inventory_units
belongs_to :shipment, class_name: "Spree::Shipment", touch: true, inverse_of: :inventory_units
belongs_to :carton, class_name: "Spree::Carton", inverse_of: :inventory_units
belongs_to :line_item, class_name: "Spree::LineItem", inverse_of: :inventory_units
belongs_to :variant, -> { with_deleted }, class_name: "Spree::Variant", inverse_of: :inventory_units, optional: true
belongs_to :shipment, class_name: "Spree::Shipment", touch: true, inverse_of: :inventory_units, optional: true
belongs_to :carton, class_name: "Spree::Carton", inverse_of: :inventory_units, optional: true
belongs_to :line_item, class_name: "Spree::LineItem", inverse_of: :inventory_units, optional: true

has_many :return_items, inverse_of: :inventory_unit, dependent: :destroy
has_one :original_return_item, class_name: "Spree::ReturnItem", foreign_key: :exchange_inventory_unit_id, dependent: :destroy
Expand Down
6 changes: 3 additions & 3 deletions core/app/models/spree/line_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ module Spree
class LineItem < Spree::Base
class CurrencyMismatch < StandardError; end

belongs_to :order, class_name: "Spree::Order", inverse_of: :line_items, touch: true
belongs_to :variant, -> { with_deleted }, class_name: "Spree::Variant", inverse_of: :line_items
belongs_to :tax_category, class_name: "Spree::TaxCategory"
belongs_to :order, class_name: "Spree::Order", inverse_of: :line_items, touch: true, optional: true
belongs_to :variant, -> { with_deleted }, class_name: "Spree::Variant", inverse_of: :line_items, optional: true
belongs_to :tax_category, class_name: "Spree::TaxCategory", optional: true

has_one :product, through: :variant

Expand Down
4 changes: 2 additions & 2 deletions core/app/models/spree/line_item_action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Spree
class LineItemAction < Spree::Base
belongs_to :line_item
belongs_to :action, class_name: "Spree::PromotionAction"
belongs_to :line_item, optional: true
belongs_to :action, class_name: "Spree::PromotionAction", optional: true
end
end
2 changes: 1 addition & 1 deletion core/app/models/spree/log_entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Spree
class LogEntry < Spree::Base
belongs_to :source, polymorphic: true
belongs_to :source, polymorphic: true, optional: true

def parsed_details
@details ||= YAML.load(details)
Expand Down
2 changes: 1 addition & 1 deletion core/app/models/spree/option_value.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Spree
class OptionValue < Spree::Base
belongs_to :option_type, class_name: 'Spree::OptionType', inverse_of: :option_values
belongs_to :option_type, class_name: 'Spree::OptionType', inverse_of: :option_values, optional: true
acts_as_list scope: :option_type

has_many :option_values_variants, dependent: :destroy
Expand Down
4 changes: 2 additions & 2 deletions core/app/models/spree/option_values_variant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Spree
class OptionValuesVariant < Spree::Base
belongs_to :variant
belongs_to :option_value
belongs_to :variant, optional: true
belongs_to :option_value, optional: true
end
end
14 changes: 7 additions & 7 deletions core/app/models/spree/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ class CannotRebuildShipments < StandardError; end
deprecate :temporary_credit_card= => :temporary_payment_source=, deprecator: Spree::Deprecation

# Customer info
belongs_to :user, class_name: Spree::UserClassHandle.new
belongs_to :bill_address, foreign_key: :bill_address_id, class_name: 'Spree::Address'
belongs_to :user, class_name: Spree::UserClassHandle.new, optional: true
belongs_to :bill_address, foreign_key: :bill_address_id, class_name: 'Spree::Address', optional: true
alias_attribute :billing_address, :bill_address

belongs_to :ship_address, foreign_key: :ship_address_id, class_name: 'Spree::Address'
belongs_to :ship_address, foreign_key: :ship_address_id, class_name: 'Spree::Address', optional: true
alias_attribute :shipping_address, :ship_address
alias_attribute :ship_total, :shipment_total

belongs_to :store, class_name: 'Spree::Store'
belongs_to :store, class_name: 'Spree::Store', optional: true

# Items
has_many :line_items, -> { order(:created_at, :id) }, dependent: :destroy, inverse_of: :order
Expand Down Expand Up @@ -101,9 +101,9 @@ def states

# Logging
has_many :state_changes, as: :stateful
belongs_to :created_by, class_name: Spree::UserClassHandle.new
belongs_to :approver, class_name: Spree::UserClassHandle.new
belongs_to :canceler, class_name: Spree::UserClassHandle.new
belongs_to :created_by, class_name: Spree::UserClassHandle.new, optional: true
belongs_to :approver, class_name: Spree::UserClassHandle.new, optional: true
belongs_to :canceler, class_name: Spree::UserClassHandle.new, optional: true

accepts_nested_attributes_for :line_items
accepts_nested_attributes_for :bill_address
Expand Down
2 changes: 1 addition & 1 deletion core/app/models/spree/order_mutex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Spree
class OrderMutex < Spree::Base
class LockFailed < StandardError; end

belongs_to :order, class_name: "Spree::Order"
belongs_to :order, class_name: "Spree::Order", optional: true

scope :expired, -> { where(arel_table[:created_at].lteq(Spree::Config[:order_mutex_max_age].seconds.ago)) }

Expand Down
6 changes: 3 additions & 3 deletions core/app/models/spree/order_promotion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ module Spree
class OrderPromotion < Spree::Base
self.table_name = 'spree_orders_promotions'

belongs_to :order, class_name: 'Spree::Order'
belongs_to :promotion, class_name: 'Spree::Promotion'
belongs_to :promotion_code, class_name: 'Spree::PromotionCode'
belongs_to :order, class_name: 'Spree::Order', optional: true
belongs_to :promotion, class_name: 'Spree::Promotion', optional: true
belongs_to :promotion_code, class_name: 'Spree::PromotionCode', optional: true

validates :order, presence: true
validates :promotion, presence: true
Expand Down
6 changes: 3 additions & 3 deletions core/app/models/spree/payment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ class Payment < Spree::Base
NON_RISKY_AVS_CODES = ['B', 'D', 'H', 'J', 'M', 'Q', 'T', 'V', 'X', 'Y'].freeze
RISKY_AVS_CODES = ['A', 'C', 'E', 'F', 'G', 'I', 'K', 'L', 'N', 'O', 'P', 'R', 'S', 'U', 'W', 'Z'].freeze

belongs_to :order, class_name: 'Spree::Order', touch: true, inverse_of: :payments
belongs_to :source, polymorphic: true
belongs_to :payment_method, -> { with_deleted }, class_name: 'Spree::PaymentMethod', inverse_of: :payments
belongs_to :order, class_name: 'Spree::Order', touch: true, inverse_of: :payments, optional: true
belongs_to :source, polymorphic: true, optional: true
belongs_to :payment_method, -> { with_deleted }, class_name: 'Spree::PaymentMethod', inverse_of: :payments, optional: true

has_many :offsets, -> { offset_payment }, class_name: "Spree::Payment", foreign_key: :source_id
has_many :log_entries, as: :source
Expand Down
2 changes: 1 addition & 1 deletion core/app/models/spree/payment_capture_event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Spree
class PaymentCaptureEvent < Spree::Base
belongs_to :payment, class_name: 'Spree::Payment'
belongs_to :payment, class_name: 'Spree::Payment', optional: true

def display_amount
Spree::Money.new(amount, { currency: payment.currency })
Expand Down
2 changes: 1 addition & 1 deletion core/app/models/spree/payment_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Spree
class PaymentSource < Spree::Base
self.abstract_class = true

belongs_to :payment_method
belongs_to :payment_method, optional: true

has_many :payments, as: :source
has_many :wallet_payment_sources, class_name: 'Spree::WalletPaymentSource', as: :payment_source, inverse_of: :payment_source
Expand Down
4 changes: 2 additions & 2 deletions core/app/models/spree/price.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ class Price < Spree::Base

MAXIMUM_AMOUNT = BigDecimal('99_999_999.99')

belongs_to :variant, -> { with_deleted }, class_name: 'Spree::Variant', touch: true
belongs_to :country, class_name: "Spree::Country", foreign_key: "country_iso", primary_key: "iso"
belongs_to :variant, -> { with_deleted }, class_name: 'Spree::Variant', touch: true, optional: true
belongs_to :country, class_name: "Spree::Country", foreign_key: "country_iso", primary_key: "iso", optional: true

delegate :product, to: :variant
delegate :tax_rates, to: :variant
Expand Down
4 changes: 2 additions & 2 deletions core/app/models/spree/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ class Product < Spree::Base
has_many :product_promotion_rules, dependent: :destroy
has_many :promotion_rules, through: :product_promotion_rules

belongs_to :tax_category, class_name: 'Spree::TaxCategory'
belongs_to :shipping_category, class_name: 'Spree::ShippingCategory', inverse_of: :products
belongs_to :tax_category, class_name: 'Spree::TaxCategory', optional: true
belongs_to :shipping_category, class_name: 'Spree::ShippingCategory', inverse_of: :products, optional: true

has_one :master,
-> { where(is_master: true).with_deleted },
Expand Down
4 changes: 2 additions & 2 deletions core/app/models/spree/product_option_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

module Spree
class ProductOptionType < Spree::Base
belongs_to :product, class_name: 'Spree::Product', inverse_of: :product_option_types, touch: true
belongs_to :option_type, class_name: 'Spree::OptionType', inverse_of: :product_option_types
belongs_to :product, class_name: 'Spree::Product', inverse_of: :product_option_types, touch: true, optional: true
belongs_to :option_type, class_name: 'Spree::OptionType', inverse_of: :product_option_types, optional: true
acts_as_list scope: :product
end
end
4 changes: 2 additions & 2 deletions core/app/models/spree/product_promotion_rule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Spree
class ProductPromotionRule < Spree::Base
belongs_to :product
belongs_to :promotion_rule
belongs_to :product, optional: true
belongs_to :promotion_rule, optional: true
end
end
4 changes: 2 additions & 2 deletions core/app/models/spree/product_property.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ class ProductProperty < Spree::Base

acts_as_list scope: :product

belongs_to :product, touch: true, class_name: 'Spree::Product', inverse_of: :product_properties
belongs_to :property, class_name: 'Spree::Property', inverse_of: :product_properties
belongs_to :product, touch: true, class_name: 'Spree::Product', inverse_of: :product_properties, optional: true
belongs_to :property, class_name: 'Spree::Property', inverse_of: :product_properties, optional: true

self.whitelisted_ransackable_attributes = ['value']
end
Expand Down
2 changes: 1 addition & 1 deletion core/app/models/spree/promotion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Promotion < Spree::Base

attr_reader :eligibility_errors

belongs_to :promotion_category
belongs_to :promotion_category, optional: true

has_many :promotion_rules, autosave: true, dependent: :destroy, inverse_of: :promotion
alias_method :rules, :promotion_rules
Expand Down
2 changes: 1 addition & 1 deletion core/app/models/spree/promotion_action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class PromotionAction < Spree::Base
include Discard::Model
self.discard_column = :deleted_at

belongs_to :promotion, class_name: 'Spree::Promotion', inverse_of: :promotion_actions
belongs_to :promotion, class_name: 'Spree::Promotion', inverse_of: :promotion_actions, optional: true

scope :of_type, ->(t) { where(type: Array.wrap(t).map(&:to_s)) }
scope :shipping, -> { of_type(Spree::Config.environment.promotions.shipping_actions.to_a) }
Expand Down
4 changes: 2 additions & 2 deletions core/app/models/spree/promotion_code.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# frozen_string_literal: true

class Spree::PromotionCode < Spree::Base
belongs_to :promotion, inverse_of: :codes
belongs_to :promotion_code_batch, class_name: "Spree::PromotionCodeBatch"
belongs_to :promotion, inverse_of: :codes, optional: true
belongs_to :promotion_code_batch, class_name: "Spree::PromotionCodeBatch", optional: true
has_many :adjustments

validates :value, presence: true, uniqueness: { allow_blank: true }
Expand Down
2 changes: 1 addition & 1 deletion core/app/models/spree/promotion_code_batch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class PromotionCodeBatch < ActiveRecord::Base
class CantProcessStartedBatch < StandardError
end

belongs_to :promotion, class_name: "Spree::Promotion"
belongs_to :promotion, class_name: "Spree::Promotion", optional: true
has_many :promotion_codes, class_name: "Spree::PromotionCode", dependent: :destroy

validates :number_of_codes, numericality: { greater_than: 0 }
Expand Down
2 changes: 1 addition & 1 deletion core/app/models/spree/promotion_rule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module Spree
# Base class for all promotion rules
class PromotionRule < Spree::Base
belongs_to :promotion, class_name: 'Spree::Promotion', inverse_of: :promotion_rules
belongs_to :promotion, class_name: 'Spree::Promotion', inverse_of: :promotion_rules, optional: true

scope :of_type, ->(t) { where(type: t) }

Expand Down
4 changes: 2 additions & 2 deletions core/app/models/spree/promotion_rule_role.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Spree
class PromotionRuleRole < ActiveRecord::Base
belongs_to :promotion_rule, class_name: 'Spree::PromotionRule'
belongs_to :role, class_name: 'Spree::Role'
belongs_to :promotion_rule, class_name: 'Spree::PromotionRule', optional: true
belongs_to :role, class_name: 'Spree::Role', optional: true
end
end
4 changes: 2 additions & 2 deletions core/app/models/spree/promotion_rule_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Spree
class PromotionRuleStore < Spree::Base
self.table_name = "spree_promotion_rules_stores"

belongs_to :promotion_rule, class_name: "Spree::PromotionRule"
belongs_to :store, class_name: "Spree::Store"
belongs_to :promotion_rule, class_name: "Spree::PromotionRule", optional: true
belongs_to :store, class_name: "Spree::Store", optional: true
end
end
4 changes: 2 additions & 2 deletions core/app/models/spree/promotion_rule_taxon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Spree
class PromotionRuleTaxon < Spree::Base
belongs_to :promotion_rule
belongs_to :taxon
belongs_to :promotion_rule, optional: true
belongs_to :taxon, optional: true
end
end
Loading

0 comments on commit 82fb316

Please sign in to comment.