Sha256: 368ced757d2b779ca4e3e01a0200b1593a640bf680ba030a9b91a10c9a9b9ff4

Contents?: true

Size: 1.81 KB

Versions: 1

Compression:

Stored size: 1.81 KB

Contents

module Spree
  class ShippingMethod < ActiveRecord::Base
    include Spree::Core::CalculatedAdjustments
    DISPLAY = [:both, :front_end, :back_end]

    default_scope -> { where(deleted_at: nil) }

    has_many :shipments
    has_many :shipping_method_categories
    has_many :shipping_categories, through: :shipping_method_categories
    has_many :shipping_rates, inverse_of: :shipping_method

    has_and_belongs_to_many :zones, :join_table => 'spree_shipping_methods_zones',
                                    :class_name => 'Spree::Zone',
                                    :foreign_key => 'shipping_method_id'

    validates :name, presence: true

    validate :at_least_one_shipping_category

    def adjustment_label
      Spree.t(:shipping)
    end

    def include?(address)
      return false unless address
      zones.any? do |zone|
        zone.include?(address)
      end
    end

    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

    def self.calculators
      spree_calculators.send(model_name_without_spree_namespace).select{ |c| c < Spree::ShippingCalculator }
    end

    # Some shipping methods are only meant to be set via backend
    def frontend?
      self.display_on != "back_end"
    end

    private
      def at_least_one_shipping_category
        if self.shipping_categories.empty?
          self.errors[:base] << "You need to select at least one shipping category"
        end
      end

      def self.on_backend_query
        "#{table_name}.display_on != 'front_end' OR #{table_name}.display_on IS NULL"
      end

      def self.on_frontend_query
        "#{table_name}.display_on != 'back_end' OR #{table_name}.display_on IS NULL"
      end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
spree_core-2.1.4 app/models/spree/shipping_method.rb