Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add delegations #559

Closed
wants to merge 8 commits into from
2 changes: 1 addition & 1 deletion core/app/models/concerns/spree/named_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module NamedType
scope :active, -> { where(active: true) }
default_scope -> { order(arel_table[:name].lower) }

validates :name, presence: true, uniqueness: { case_sensitive: false }
validates :name, presence: true, uniqueness: { case_sensitive: false, allow_blank: true }
end
end
end
6 changes: 2 additions & 4 deletions core/app/models/spree/adjustment_reason.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ module Spree
class AdjustmentReason < Spree::Base
has_many :adjustments, inverse_of: :adjustment_reason

validates :name, presence: true
validates :name, uniqueness: {case_sensitive: false}
validates :code, presence: true
validates :code, uniqueness: {case_sensitive: false}
validates :name, presence: true, uniqueness: { case_sensitive: false, allow_blank: true }
validates :code, presence: true, uniqueness: { case_sensitive: false, allow_blank: true }

scope :active, -> { where(active: true) }
end
Expand Down
7 changes: 3 additions & 4 deletions core/app/models/spree/customer_return.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ class CustomerReturn < Spree::Base
money_methods pre_tax_total: { currency: Spree::Config[:currency] },
total: { currency: Spree::Config[:currency] }


delegate :id, to: :order, prefix: true, allow_nil: true

def total
return_items.map(&:total).sum
end
Expand All @@ -33,10 +36,6 @@ def order
return_items.first.inventory_unit.order
end

def order_id
order.try(:id)
end

def fully_reimbursed?
completely_decided? && return_items.accepted.includes(:reimbursement).all? { |return_item| return_item.reimbursement.try(:reimbursed?) }
end
Expand Down
12 changes: 5 additions & 7 deletions core/app/models/spree/line_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ class LineItem < Spree::Base
after_create :update_tax_charge

delegate :name, :description, :sku, :should_track_inventory?, to: :variant
# @note This will return the product even if it has been deleted.
# @return [Spree::Product, nil] the product associated with this line
# item, if there is one
delegate :product, to: :variant


attr_accessor :target_shipment

Expand Down Expand Up @@ -114,13 +119,6 @@ def insufficient_stock?
!sufficient_stock?
end

# @note This will return the product even if it has been deleted.
# @return [Spree::Product, nil] the product associated with this line
# item, if there is one
def product
variant.product
end

# @note This will return the variant even if it has been deleted.
# @return [Spree::Variant, nil] the variant associated with this line
# item, if there is one
Expand Down
2 changes: 1 addition & 1 deletion core/app/models/spree/option_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class OptionType < Spree::Base
has_many :products, through: :product_option_types
has_and_belongs_to_many :prototypes, join_table: 'spree_option_types_prototypes'

validates :name, presence: true, uniqueness: true
validates :name, presence: true, uniqueness: { allow_blank: true }
validates :presentation, presence: true

default_scope -> { order(:position) }
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 @@ -6,7 +6,7 @@ class OptionValue < Spree::Base
has_many :option_values_variants, dependent: :destroy
has_many :variants, through: :option_values_variants

validates :name, presence: true, uniqueness: { scope: :option_type_id }
validates :name, presence: true, uniqueness: { scope: :option_type_id, allow_blank: true }
validates :presentation, presence: true

after_save :touch, if: :changed?
Expand Down
16 changes: 6 additions & 10 deletions core/app/models/spree/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,11 @@ def states

make_permalink field: :number

delegate :update_totals, :persist_totals, :to => :updater
delegate :update_totals, :persist_totals, to: :updater
delegate :firstname, :lastname, to: :bill_address, prefix: true, allow_nil: true
alias_method :billing_firstname, :bill_address_firstname
alias_method :billing_lastname, :bill_address_lastname


class_attribute :update_hooks
self.update_hooks = Set.new
Expand Down Expand Up @@ -410,14 +414,6 @@ def available_payment_methods
).uniq
end

def billing_firstname
bill_address.try(:firstname)
end

def billing_lastname
bill_address.try(:lastname)
end

def insufficient_stock_lines
line_items.select(&:insufficient_stock?)
end
Expand Down Expand Up @@ -750,7 +746,7 @@ def after_resume
end

def use_billing?
@use_billing == true || @use_billing == 'true' || @use_billing == '1'
use_billing.in?([true, 'true', '1'])
end

def set_currency
Expand Down
2 changes: 1 addition & 1 deletion core/app/models/spree/preference.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class Spree::Preference < Spree::Base
serialize :value

validates :key, presence: true, uniqueness: true
validates :key, presence: true, uniqueness: { allow_blank: true }
end
2 changes: 1 addition & 1 deletion core/app/models/spree/promotion_code.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class Spree::PromotionCode < Spree::Base
belongs_to :promotion, inverse_of: :codes
has_many :adjustments

validates :value, presence: true, uniqueness: true
validates :value, presence: true, uniqueness: { allow_blank: true }
validates :promotion, presence: true

before_save :downcase_value
Expand Down
5 changes: 1 addition & 4 deletions core/app/models/spree/shipping_rate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class ShippingRate < Spree::Base

delegate :order, :currency, to: :shipment
delegate :name, to: :shipping_method
delegate :code, to: :shipping_method, prefix: true

def display_base_price
Spree::Money.new(cost, currency: currency)
Expand Down Expand Up @@ -46,10 +47,6 @@ def shipping_method
Spree::ShippingMethod.unscoped { super }
end

def shipping_method_code
shipping_method.code
end

def tax_rate
Spree::TaxRate.unscoped { super }
end
Expand Down
12 changes: 5 additions & 7 deletions core/app/models/spree/stock_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ class StockItem < Spree::Base
belongs_to :variant, class_name: 'Spree::Variant', inverse_of: :stock_items
has_many :stock_movements, inverse_of: :stock_item

validates_presence_of :stock_location, :variant
validates_uniqueness_of :variant_id, scope: [:stock_location_id, :deleted_at]
validates :stock_location, :variant, presence: true
validates :variant_id, uniqueness: { scope: [:stock_location_id, :deleted_at] }, allow_blank: true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did we want this allow_blank: true to be a part of the uniqueness clause?
validates :variant_id, uniqueness: { scope: [:stock_location_id, :deleted_at], allow_blank: true }

Same question for other calls below.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, this is valid. allow_blank can be passed directly into the UniquenessValidator or used as an option to the validates method. The difference being whether or not the validators are invoked, or are expected to handle :allow_blank directly (which uniqueness does). I've only ever passed allow_blank into specific validators, I didn't know you could short circuit using it outside of those validation statements. 👍

validates :count_on_hand, numericality: { greater_than_or_equal_to: 0 }, if: :verify_count_on_hand?

delegate :weight, :should_track_inventory?, to: :variant

# @return [String] the name of this stock item's variant
delegate :name, to: :variant, prefix: true

after_save :conditional_variant_touch, if: :changed?
after_touch { variant.touch }

Expand All @@ -23,11 +26,6 @@ def backordered_inventory_units
Spree::InventoryUnit.backordered_for_stock_item(self)
end

# @return [String] the name of this stock item's variant
def variant_name
variant.name
end

# Adjusts the count on hand by a given value.
#
# @note This will cause backorders to be processed.
Expand Down
4 changes: 2 additions & 2 deletions core/app/models/spree/transfer_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ class TransferItem < Spree::Base
belongs_to :variant

validate :stock_availability, if: :check_stock?
validates_presence_of :stock_transfer, :variant
validates_uniqueness_of :variant_id, scope: :stock_transfer_id
validates :stock_transfer, :variant, presence: true
validates :variant_id, uniqueness: { scope: :stock_transfer_id }, allow_blank: true
validates :expected_quantity, numericality: { greater_than: 0 }
validates :received_quantity, numericality: { greater_than_or_equal_to: 0, less_than_or_equal_to: :expected_quantity }

Expand Down
5 changes: 1 addition & 4 deletions core/app/models/spree/zone_member.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ class ZoneMember < Spree::Base
belongs_to :zone, class_name: 'Spree::Zone', counter_cache: true, inverse_of: :zone_members
belongs_to :zoneable, polymorphic: true

def name
return nil if zoneable.nil?
zoneable.name
end
delegate :name, to: :zoneable, allow_nil: true
end
end