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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Use Product#taxons. UrlValidator -> Spree::UrlValidator.
Sebastian Palma6e85ede24458
diff --git a/api/app/jobs/spree/base_job.rb b/api/app/jobs/spree/base_job.rbdeleted file mode 100644index 4e59602156..0000000000--- a/api/app/jobs/spree/base_job.rb+++ /dev/null@@ -1,4 +0,0 @@-module Spree- class BaseJob < ApplicationJob- end-enddiff --git a/api/app/models/spree/webhooks/endpoint.rb b/api/app/models/spree/webhooks/endpoint.rbindex c9eb975c9d..c4db46a9c9 100644--- a/api/app/models/spree/webhooks/endpoint.rb+++ b/api/app/models/spree/webhooks/endpoint.rb@@ -1,7 +1,7 @@module Spreemodule Webhooksclass Endpoint < Spree::Webhooks::Base- validates :url, url: true+ validates :url, 'spree/url': trueendendenddiff --git a/api/app/serializers/spree/api/v2/platform/product_serializer.rb b/api/app/serializers/spree/api/v2/platform/product_serializer.rbindex 0f319af5d0..34124d3837 100644--- a/api/app/serializers/spree/api/v2/platform/product_serializer.rb+++ b/api/app/serializers/spree/api/v2/platform/product_serializer.rb@@ -60,7 +60,11 @@ module Spreehas_many :option_typeshas_many :product_propertieshas_many :taxons, serializer: :taxon, record_type: :taxon do |object, params|- object.taxons_for_store(params[:store])+ if params[:store].present?+ object.taxons_for_store(params[:store])+ else+ object.taxons+ endendhas_many :images,diff --git a/core/app/models/spree/product.rb b/core/app/models/spree/product.rbindex 4b5ca7cac2..f055f6f286 100644--- a/core/app/models/spree/product.rb+++ b/core/app/models/spree/product.rb@@ -326,14 +326,8 @@ module Spreeenddef taxons_for_store(store)- if store.present?- Rails.cache.fetch("#{cache_key_with_version}/taxons-per-store/#{store.id}") do- taxons.for_store(store)- end- else- Rails.cache.fetch("#{cache_key_with_version}/taxons-per-store/#{store_ids.join('/')}") do- taxons.for_stores(stores)- end+ Rails.cache.fetch("#{cache_key_with_version}/taxons-per-store/#{store.id}") do+ taxons.for_store(store)endenddiff --git a/core/app/validators/spree/url_validator.rb b/core/app/validators/spree/url_validator.rbnew file mode 100644index 0000000000..28abec11a6--- /dev/null+++ b/core/app/validators/spree/url_validator.rb@@ -0,0 +1,23 @@+module Spree+ class UrlValidator < ActiveModel::EachValidator+ def validate_each(record, attribute, value)+ unless url_valid?(value)+ record.errors.add(attribute, (options[:message] || ERROR_MESSAGE))+ end+ end++ private++ ERROR_MESSAGE = 'must be a valid URL'+ private_constant :ERROR_MESSAGE++ def url_valid?(url)+ uri = begin+ URI.parse(url)+ rescue URI::InvalidURIError+ return false+ end+ uri.host.present? && uri.is_a?(URI::HTTP)+ end+ end+enddiff --git a/core/app/validators/url_validator.rb b/core/app/validators/url_validator.rbdeleted file mode 100644index 8c96d009fd..0000000000--- a/core/app/validators/url_validator.rb+++ /dev/null@@ -1,21 +0,0 @@-class UrlValidator < ActiveModel::EachValidator- def validate_each(record, attribute, value)- unless url_valid?(value)- record.errors.add(attribute, (options[:message] || ERROR_MESSAGE))- end- end-- private-- ERROR_MESSAGE = 'must be a valid URL'- private_constant :ERROR_MESSAGE-- def url_valid?(url)- uri = begin- URI.parse(url)- rescue URI::InvalidURIError- return false- end- uri.host.present? && uri.is_a?(URI::HTTP)- end-enddiff --git a/core/spec/models/spree/product_spec.rb b/core/spec/models/spree/product_spec.rbindex 437506ae1b..6f8fe6acda 100644--- a/core/spec/models/spree/product_spec.rb+++ b/core/spec/models/spree/product_spec.rb@@ -808,25 +808,18 @@ describe Spree::Product, type: :model dodescribe '#taxons_for_store' dolet(:store) { create(:store) }let(:store_2) { create(:store) }- let(:taxon) { create(:taxon, taxonomy: create(:taxonomy, store: store)) }- let(:taxon_2) { create(:taxon, taxonomy: create(:taxonomy, store: store_2)) }- let!(:product) { create(:product, stores: [store, store_2], taxons: [taxon, taxon_2]) }+ let(:product) { create(:product, stores: [store, store_2], taxons: [taxon, taxon_2]) }+ let(:taxonomy) { create(:taxonomy, store: store) }+ let(:taxonomy_2) { create(:taxonomy, store: store_2) }+ let(:taxon) { create(:taxon, taxonomy: taxonomy) }+ let(:taxon_2) { create(:taxon, taxonomy: taxonomy_2) }+ let(:taxon_3) { create(:taxon, taxonomy: taxonomy) }- context 'with a store' do- it 'returns product taxons for specified store' do- expect(product.taxons_for_store(store)).to eq([taxon])- expect(product.taxons_for_store(store_2)).to eq([taxon_2])- end-- it { expect(product.taxons_for_store(store)).to be_a(ActiveRecord::Relation) }+ it 'returns product taxons for specified store' do+ expect(product.taxons_for_store(store)).to eq([taxon])+ expect(product.taxons_for_store(store_2)).to eq([taxon_2])end- context 'without a store' do- it 'returns the product taxons for all the stores' do- expect(product.taxons_for_store(nil)).to eq([taxon, taxon_2])- end-- it { expect(product.taxons_for_store(nil)).to be_a(ActiveRecord::Relation) }- end+ it { expect(product.taxons_for_store(store)).to be_a(ActiveRecord::Relation) }endenddiff --git a/core/spec/validators/spree/url_validator_spec.rb b/core/spec/validators/spree/url_validator_spec.rbnew file mode 100644index 0000000000..97789da492--- /dev/null+++ b/core/spec/validators/spree/url_validator_spec.rb@@ -0,0 +1,70 @@+require 'spec_helper'++module Spree+ module Test+ class Product < ActiveRecord::Base+ self.table_name = 'test_products'++ validates :url, 'spree/url': true+ end+ end+end++describe Spree::UrlValidator do+ before(:all) do+ ActiveRecord::Base.connection.create_table :test_products, force: true do |t|+ t.string :url+ end+ end++ after(:all) do+ ActiveRecord::Base.connection.drop_table :test_products, if_exists: true+ end++ describe 'validating the given URL' do+ context 'is invalid' do+ it { expect(Spree::Test::Product.new(url: nil).valid?).to eq(false) }+ it { expect(Spree::Test::Product.new(url: '').valid?).to eq(false) }+ it { expect(Spree::Test::Product.new(url: 'google.com').valid?).to eq(false) }+ it { expect(Spree::Test::Product.new(url: 'http:/google').valid?).to eq(false) }+ it { expect(Spree::Test::Product.new(url: 'www.google.com').valid?).to eq(false) }+ end++ context 'is valid' do+ it { expect(Spree::Test::Product.new(url: 'http://google.com').valid?).to eq(true) }+ it { expect(Spree::Test::Product.new(url: 'https://google.com').valid?).to eq(true) }+ it { expect(Spree::Test::Product.new(url: 'http://www.google.com').valid?).to eq(true) }+ it { expect(Spree::Test::Product.new(url: 'https://www.google.com').valid?).to eq(true) }+ # Due to simplicity, this is valid even without tld+ it { expect(Spree::Test::Product.new(url: 'http://google').valid?).to eq(true) }+ end+ end++ describe 'using a given message or a defined one' do+ let(:test_product) { Spree::Test::Product.new(url: nil) }++ context 'using the message option' do+ let(:message) { 'is needed' }++ it 'adds the given message to the record url errors array' do+ # Remove the validation set at the beginning+ Spree::Test::Product.clear_validators!+ Spree::Test::Product.validates(:url, 'spree/url': { message: message })++ test_product.valid?+ expect(test_product.errors.messages).to eq(url: [message])++ # Add back the validation removed previously+ Spree::Test::Product.clear_validators!+ Spree::Test::Product.validates(:url, 'spree/url': true)+ end+ end++ context 'without using the message option' do+ it 'adds a pre-defined message to the record url error array' do+ test_product.valid?+ expect(test_product.errors.messages).to eq(url: ['must be a valid URL'])+ end+ end+ end+enddiff --git a/core/spec/validators/url_validator_spec.rb b/core/spec/validators/url_validator_spec.rbdeleted file mode 100644index f719de7fdb..0000000000--- a/core/spec/validators/url_validator_spec.rb+++ /dev/null@@ -1,67 +0,0 @@-require 'spec_helper'--module Test- class Product < ActiveRecord::Base- self.table_name = 'test_products'-- validates :url, url: true- end-end--describe UrlValidator do- before(:all) do- ActiveRecord::Base.connection.create_table :test_products, force: true do |t|- t.string :url- end- end-- after(:all) do- ActiveRecord::Base.connection.drop_table :test_products, if_exists: true- end-- describe 'validating the given URL' do- context 'is invalid' do- it { expect(Test::Product.new(url: nil).valid?).to eq(false) }- it { expect(Test::Product.new(url: 'google.com').valid?).to eq(false) }- it { expect(Test::Product.new(url: 'http:/google').valid?).to eq(false) }- it { expect(Test::Product.new(url: 'www.google.com').valid?).to eq(false) }- end-- context 'is valid' do- it { expect(Test::Product.new(url: 'http://google.com').valid?).to eq(true) }- it { expect(Test::Product.new(url: 'https://google.com').valid?).to eq(true) }- it { expect(Test::Product.new(url: 'http://www.google.com').valid?).to eq(true) }- it { expect(Test::Product.new(url: 'https://www.google.com').valid?).to eq(true) }- # Due to simplicity, this is valid even without tld- it { expect(Test::Product.new(url: 'http://google').valid?).to eq(true) }- end- end-- describe 'using a given message or a defined one' do- let(:test_product) { Test::Product.new(url: nil) }-- context 'using the message option' do- let(:message) { 'is needed' }-- it 'adds the given message to the record url errors array' do- # Remove the validation set at the beginning- Test::Product.clear_validators!- Test::Product.validates(:url, url: { message: message })-- test_product.valid?- expect(test_product.errors.messages).to eq(url: [message])-- # Add back the validation removed previously- Test::Product.clear_validators!- Test::Product.validates(:url, url: true)- end- end-- context 'without using the message option' do- it 'adds a pre-defined message to the record url error array' do- test_product.valid?- expect(test_product.errors.messages).to eq(url: ['must be a valid URL'])- end- end- end-end