diff --git a/app/models/spree/shipping_method.rb b/app/models/spree/shipping_method.rb index 53d77d60c2..042d7d07a0 100644 --- a/app/models/spree/shipping_method.rb +++ b/app/models/spree/shipping_method.rb @@ -3,7 +3,10 @@ module Spree class ShippingMethod < ApplicationRecord include CalculatedAdjustments - DISPLAY = [:both, :front_end, :back_end].freeze + DISPLAY_ON_OPTIONS = { + both: "", + back_end: "back_end" + }.freeze acts_as_paranoid acts_as_taggable @@ -27,6 +30,7 @@ module Spree validates :name, presence: true validate :distributor_validation validate :at_least_one_shipping_category + validates :display_on, inclusion: { in: DISPLAY_ON_OPTIONS.values }, allow_nil: true after_save :touch_distributors @@ -101,12 +105,12 @@ module Spree ] end - def self.on_backend_query - "#{table_name}.display_on != 'front_end' OR #{table_name}.display_on IS NULL" + def self.backend + where("spree_shipping_methods.display_on = ?", DISPLAY_ON_OPTIONS[:back_end]) end - def self.on_frontend_query - "#{table_name}.display_on != 'back_end' OR #{table_name}.display_on IS NULL" + def self.frontend + where("spree_shipping_methods.display_on IS NULL OR spree_shipping_methods.display_on = ''") end private diff --git a/app/models/spree/shipping_rate.rb b/app/models/spree/shipping_rate.rb index ce94b5f808..5116a2de6b 100644 --- a/app/models/spree/shipping_rate.rb +++ b/app/models/spree/shipping_rate.rb @@ -8,7 +8,7 @@ module Spree scope :frontend, -> { includes(:shipping_method). - where(ShippingMethod.on_frontend_query). + merge(ShippingMethod.frontend). references(:shipping_method). order("cost ASC") } diff --git a/app/views/spree/admin/shipping_methods/_form.html.haml b/app/views/spree/admin/shipping_methods/_form.html.haml index 2148576ca4..5ca959f927 100644 --- a/app/views/spree/admin/shipping_methods/_form.html.haml +++ b/app/views/spree/admin/shipping_methods/_form.html.haml @@ -19,7 +19,7 @@ .alpha.four.columns = f.label :display_on, t(:display) .omega.twelve.columns - = select(:shipping_method, :display_on, [[t(".both"), nil], [t(".back_end"), "back_end"]], {}, {class: 'select2 fullwidth'}) + = select(:shipping_method, :display_on, Spree::ShippingMethod::DISPLAY_ON_OPTIONS.map { |key, value| [t(".#{key}"), value] }, {}, {class: 'select2 fullwidth'}) = error_message_on :shipping_method, :display_on .row diff --git a/spec/models/spree/shipping_method_spec.rb b/spec/models/spree/shipping_method_spec.rb index b505a3a808..14d7333120 100644 --- a/spec/models/spree/shipping_method_spec.rb +++ b/spec/models/spree/shipping_method_spec.rb @@ -143,6 +143,23 @@ module Spree expect(shipping_method.errors[:name].first).to eq "can't be blank" end + describe "#display_on" do + it "is valid when it's set to nil, an empty string or 'back_end'" do + shipping_method = build_stubbed(:shipping_method) + [nil, "", "back_end"].each do |display_on_option| + shipping_method.display_on = display_on_option + shipping_method.valid? + expect(shipping_method.errors[:display_on]).to be_empty + end + end + + it "is not valid when it's set to an unknown value" do + shipping_method = build_stubbed(:shipping_method, display_on: "front_end") + expect(shipping_method).not_to be_valid + expect(shipping_method.errors[:display_on]).to eq ["is not included in the list"] + end + end + context "shipping category" do it "validates presence of at least one" do shipping_method = build_stubbed(