Skip to content

Commit

Permalink
Adds more flexibility for Product validation
Browse files Browse the repository at this point in the history
  • Loading branch information
damianlegawiec committed May 6, 2021
1 parent 57982e8 commit ab117b0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
13 changes: 11 additions & 2 deletions core/app/models/spree/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,9 @@ class Product < Spree::Base
validates :meta_title
end
with_options presence: true do
validates :name, :shipping_category
validates :price, if: proc { Spree::Config[:require_master_price] }
validates :name
validates :shipping_category, if: :requires_shipping_category?
validates :price, if: :requires_price?
end

validates :slug, presence: true, uniqueness: { allow_blank: true, case_sensitive: false }
Expand Down Expand Up @@ -485,5 +486,13 @@ def reset_memoized_data
instance_variable_set(:"@#{v}", nil)
end
end

def requires_price?
Spree::Config[:require_master_price]
end

def requires_shipping_category?
true
end
end
end
6 changes: 6 additions & 0 deletions core/spec/models/spree/product_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -744,4 +744,10 @@ def build_option_type_with_values(name, values)
expect(key).to eq("spree/default-variant/#{product.cache_key_with_version}/false")
end
end

describe '#requires_shipping_category?' do
let(:product) { build(:product, shipping_category: nil) }

it { expect(product.save).to eq(false) }
end
end

0 comments on commit ab117b0

Please sign in to comment.