Class: Spree::ShippingMethod
- Includes:
- CalculatedAdjustments, SoftDeletable
- Defined in:
- app/models/spree/shipping_method.rb
Overview
Represents a means of having a shipment delivered, such as FedEx or UPS.
Constant Summary collapse
- DISPLAY =
ActiveSupport::Deprecation::DeprecatedObjectProxy.new( [:both, :front_end, :back_end], "Spree::ShippingMethod::DISPLAY is deprecated", Spree::Deprecation )
Class Method Summary collapse
-
.available_for_address(address) ⇒ ActiveRecord::Relation
Shipping methods which are associated with zones matching the provided address.
-
.available_in_stock_location(stock_location) ⇒ ActiveRecord::Relation
Shipping methods which are available with the stock location or are marked available_to_all.
-
.with_all_shipping_category_ids(shipping_category_ids) ⇒ ActiveRecord::Relation
Shipping methods which are associated with all of the provided shipping categories.
Instance Method Summary collapse
- #build_tracking_url(tracking) ⇒ Object
- #display_on ⇒ Object
- #display_on=(value) ⇒ Object
-
#frontend? ⇒ Boolean
Some shipping methods are only meant to be set via backend.
- #include?(address) ⇒ Boolean
Methods included from CalculatedAdjustments
#calculator_type, #calculator_type=
Methods inherited from Base
display_includes, page, preference, #preferences
Methods included from Core::Permalinks
#generate_permalink, #save_permalink
Class Method Details
.available_for_address(address) ⇒ ActiveRecord::Relation
Returns shipping methods which are associated with zones matching the provided address.
82 83 84 |
# File 'app/models/spree/shipping_method.rb', line 82 def self.available_for_address(address) joins(:zones).merge(Zone.for_address(address)) end |
.available_in_stock_location(stock_location) ⇒ ActiveRecord::Relation
Returns shipping methods which are available with the stock location or are marked available_to_all.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'app/models/spree/shipping_method.rb', line 60 def self.available_in_stock_location(stock_location) smsl_table = Spree::ShippingMethodStockLocation.arel_table # We are searching for either a matching entry in the stock location join # table or available_to_all being true. # We need to use an outer join otherwise a shipping method with no # associated stock locations will be filtered out of the results. In # rails 5 this will be easy using .left_join and .or, but for now we must # use arel to achieve this. arel_join = arel_table.join(smsl_table, Arel::Nodes::OuterJoin). on(arel_table[:id].eq(smsl_table[:shipping_method_id])). join_sources arel_condition = arel_table[:available_to_all].eq(true).or(smsl_table[:stock_location_id].eq(stock_location.id)) joins(arel_join).where(arel_condition).distinct end |
.with_all_shipping_category_ids(shipping_category_ids) ⇒ ActiveRecord::Relation
Returns shipping methods which are associated with all of the provided shipping categories.
43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'app/models/spree/shipping_method.rb', line 43 def self.with_all_shipping_category_ids(shipping_category_ids) # Some extra care is needed with the having clause to ensure we are # counting distinct records of the join table. Otherwise a join could # cause this to return incorrect results. join_table = Spree::ShippingMethodCategory.arel_table having = join_table[:id].count(true).eq(shipping_category_ids.count) subquery = joins(:shipping_method_categories). where(spree_shipping_method_categories: { shipping_category_id: shipping_category_ids }). group('spree_shipping_methods.id'). having(having) where(id: subquery.select(:id)) end |
Instance Method Details
#build_tracking_url(tracking) ⇒ Object
93 94 95 96 |
# File 'app/models/spree/shipping_method.rb', line 93 def build_tracking_url(tracking) return if tracking.blank? || tracking_url.blank? tracking_url.gsub(/:tracking/, ERB::Util.url_encode(tracking)) # :url_encode exists in 1.8.7 through 2.1.0 end |
#display_on ⇒ Object
98 99 100 101 102 103 104 |
# File 'app/models/spree/shipping_method.rb', line 98 def display_on if available_to_users? "both" else "back_end" end end |
#display_on=(value) ⇒ Object
107 108 109 |
# File 'app/models/spree/shipping_method.rb', line 107 def display_on=(value) self.available_to_users = (value != "back_end") end |
#frontend? ⇒ Boolean
Some shipping methods are only meant to be set via backend
113 114 115 |
# File 'app/models/spree/shipping_method.rb', line 113 def frontend? available_to_users? end |
#include?(address) ⇒ Boolean
86 87 88 89 90 91 |
# File 'app/models/spree/shipping_method.rb', line 86 def include?(address) return false unless address zones.any? do |zone| zone.include?(address) end end |