Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Added a bunch of helper utility methods (#12063)

Damian Legawiecb48af4424c55
diff --git a/api/app/models/spree/oauth_application.rb b/api/app/models/spree/oauth_application.rb
index ec86fbcb7a..438014b6f4 100644
--- a/api/app/models/spree/oauth_application.rb
+++ b/api/app/models/spree/oauth_application.rb
@@ -6,6 +6,13 @@ module Spree
before_validation :set_blank_for_redirect_uri
+ # returns the last someone used this application
+ #
+ # @return [DateTime]
+ def last_used_at
+ access_tokens.order(:created_at).last&.created_at
+ end
+
private
def set_blank_for_redirect_uri
diff --git a/core/app/models/spree/adjustment.rb b/core/app/models/spree/adjustment.rb
index 8641c74021..e03b950ebb 100644
--- a/core/app/models/spree/adjustment.rb
+++ b/core/app/models/spree/adjustment.rb
@@ -64,6 +64,7 @@ module Spree
scope :charge, -> { where("#{quoted_table_name}.amount >= 0") }
scope :credit, -> { where("#{quoted_table_name}.amount < 0") }
scope :nonzero, -> { where("#{quoted_table_name}.amount != 0") }
+ scope :non_zero, -> { where.not(amount: [nil, 0]) }
scope :promotion, -> { where(source_type: 'Spree::PromotionAction') }
scope :return_authorization, -> { where(source_type: 'Spree::ReturnAuthorization') }
scope :is_included, -> { where(included: true) }
@@ -87,6 +88,10 @@ module Spree
source_type == 'Spree::PromotionAction'
end
+ def tax?
+ source_type == 'Spree::TaxRate'
+ end
+
# Passing a target here would always be recommended as it would avoid
# hitting the database again and would ensure you're compute values over
# the specific object amount passed here.
diff --git a/core/app/models/spree/line_item.rb b/core/app/models/spree/line_item.rb
index 11d0b4cf86..1a912ed8d9 100644
--- a/core/app/models/spree/line_item.rb
+++ b/core/app/models/spree/line_item.rb
@@ -99,6 +99,13 @@ module Spree
amount + taxable_adjustment_total
end
+ # returns the total tax amount
+ #
+ # @return [BigDecimal]
+ def tax_total
+ included_tax_total + additional_tax_total
+ end
+
alias discounted_money display_discounted_amount
alias discounted_amount taxable_amount
@@ -117,6 +124,20 @@ module Spree
!sufficient_stock?
end
+ # returns true if any of the inventory units are shipped
+ #
+ # @return [Boolean]
+ def any_shipped?
+ inventory_units.any?(&:shipped?)
+ end
+
+ # returns true if all of the inventory units are shipped
+ #
+ # @return [Boolean]
+ def fully_shipped?
+ inventory_units.all?(&:shipped?)
+ end
+
def options=(options = {})
return unless options.present?
diff --git a/core/app/models/spree/option_value_variant.rb b/core/app/models/spree/option_value_variant.rb
index f74fce82ff..36e55d22b1 100644
--- a/core/app/models/spree/option_value_variant.rb
+++ b/core/app/models/spree/option_value_variant.rb
@@ -5,5 +5,9 @@ module Spree
validates :option_value, :variant, presence: true
validates :option_value_id, uniqueness: { scope: :variant_id }
+
+ scope :for_option_types, lambda { |option_types|
+ joins(:option_value).merge(Spree::OptionValue.where(option_types: option_types))
+ }
end
end
diff --git a/core/app/models/spree/payment/gateway_options.rb b/core/app/models/spree/payment/gateway_options.rb
index 10bc8e5ed4..dd4a3d8b29 100644
--- a/core/app/models/spree/payment/gateway_options.rb
+++ b/core/app/models/spree/payment/gateway_options.rb
@@ -5,6 +5,10 @@ module Spree
@payment = payment
end
+ def statement_descriptor_suffix
+ order.number
+ end
+
def email
order.email
end