Skip to content

armgit / spree

Public Imported from GitHub
0

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.

Mobility Gem integration for products and taxons (#11823)

nciemniaka4f6eb5b1983
diff --git a/.circleci/config.yml b/.circleci/config.yml
index 3724b809eb..9d5509b401 100644
--- a/.circleci/config.yml
+++ b/.circleci/config.yml
@@ -248,7 +248,7 @@ jobs:
- run:
name: Run Brakeman spree/api
command: |
- bundle exec brakeman -p api/ --skip-files app/controllers/spree/api/v1/ --exit-on-warn --exit-on-error
+ bundle exec brakeman -p api/ --ignore-config api/brakeman.ignore --skip-files app/controllers/spree/api/v1/ --exit-on-warn --exit-on-error
- run:
name: Run Brakeman spree/core
command: |
diff --git a/api/app/controllers/spree/api/v2/platform/variants_controller.rb b/api/app/controllers/spree/api/v2/platform/variants_controller.rb
index d3ca47e8d0..93c436b467 100644
--- a/api/app/controllers/spree/api/v2/platform/variants_controller.rb
+++ b/api/app/controllers/spree/api/v2/platform/variants_controller.rb
@@ -12,6 +12,16 @@ module Spree
def spree_permitted_attributes
super + [:option_value_ids, :price, :currency]
end
+
+ def collection
+ # if filtering on products, manually join on product translation to workaround mobility-ransack issue
+ if params.key?(:filter) && params[:filter].keys.any? { |k| k.include? 'product' }
+ @collection ||= scope.join_product_translations.
+ ransack(params[:filter]).result
+ else
+ super
+ end
+ end
end
end
end
diff --git a/api/app/controllers/spree/api/v2/resource_controller.rb b/api/app/controllers/spree/api/v2/resource_controller.rb
index 9992b2ee6a..916d12d47e 100644
--- a/api/app/controllers/spree/api/v2/resource_controller.rb
+++ b/api/app/controllers/spree/api/v2/resource_controller.rb
@@ -35,7 +35,7 @@ module Spree
base_scope = model_class.for_store(current_store)
base_scope = base_scope.accessible_by(current_ability, :show) unless skip_cancancan
base_scope = base_scope.includes(scope_includes) if scope_includes.any? && action_name == 'index'
- base_scope
+ model_class.include?(TranslatableResource) ? base_scope.i18n : base_scope
end
def scope_includes
diff --git a/api/app/helpers/spree/api/v2/collection_options_helpers.rb b/api/app/helpers/spree/api/v2/collection_options_helpers.rb
index 768313af08..a39157739c 100644
--- a/api/app/helpers/spree/api/v2/collection_options_helpers.rb
+++ b/api/app/helpers/spree/api/v2/collection_options_helpers.rb
@@ -23,7 +23,7 @@ module Spree
# leaving this method in public scope so it's still possible to modify
# those params to support non-standard non-JSON API parameters
def collection_permitted_params
- params.permit(:format, :page, :per_page, :sort, :include, fields: {}, filter: {})
+ params.permit(:format, :page, :per_page, :sort, :include, :locale, fields: {}, filter: {})
end
private
diff --git a/api/brakeman.ignore b/api/brakeman.ignore
new file mode 100644
index 0000000000..e253a812ea
--- /dev/null
+++ b/api/brakeman.ignore
@@ -0,0 +1,7 @@
+{
+ "ignored_warnings": [
+
+ ],
+ "updated": "2022-12-29 09:29:46 +0100",
+ "brakeman_version": "5.4.0"
+}
diff --git a/api/spec/integration/api/v2/platform/taxons_spec.rb b/api/spec/integration/api/v2/platform/taxons_spec.rb
index d16dea858d..48f2474562 100644
--- a/api/spec/integration/api/v2/platform/taxons_spec.rb
+++ b/api/spec/integration/api/v2/platform/taxons_spec.rb
@@ -16,7 +16,13 @@ describe 'Taxons API', swagger: true do
let!(:taxon_b) { create(:taxon, name: 'Shorts', taxonomy: taxonomy) }
let(:records_list) { create_list(:taxon, 2, taxonomy: taxonomy) }
- let(:valid_create_param_value) { build(:taxon, taxonomy: taxonomy).attributes }
+ let(:valid_create_param_value) do {
+ parent_id: taxonomy.root.id,
+ position: 0,
+ name: 'new_taxon',
+ taxonomy_id: taxonomy.id
+ }
+ end
let(:valid_update_param_value) do
{
name: 'T-Shirts',
diff --git a/api/spec/models/concerns/spree/webhooks/has_webhooks_spec.rb b/api/spec/models/concerns/spree/webhooks/has_webhooks_spec.rb
index 7fe74735e2..3d36b83591 100644
--- a/api/spec/models/concerns/spree/webhooks/has_webhooks_spec.rb
+++ b/api/spec/models/concerns/spree/webhooks/has_webhooks_spec.rb
@@ -88,7 +88,10 @@ describe Spree::Webhooks::HasWebhooks do
context 'when only timestamps change' do
let(:event_name) { 'product.update' }
- before { product.save }
+ before {
+ product.save
+ product.translations.first.save
+ }
context 'on created_at change' do
it do
diff --git a/api/spec/requests/spree/api/v2/storefront/products_spec.rb b/api/spec/requests/spree/api/v2/storefront/products_spec.rb
index 8beefd0afb..feb1d1c5b2 100644
--- a/api/spec/requests/spree/api/v2/storefront/products_spec.rb
+++ b/api/spec/requests/spree/api/v2/storefront/products_spec.rb
@@ -365,7 +365,9 @@ describe 'API V2 Storefront Products Spec', type: :request do
it 'returns products sorted by name' do
expect(json_response['data'].count).to eq store.products.available.count
- expect(json_response['data'].pluck(:id)).to eq store.products.available.order(:name).map(&:id).map(&:to_s)
+ expect(json_response['data'].pluck(:id)).to eq store.products.i18n.available
+ .select("#{Spree::Product.table_name}.*, #{Spree::Product::Translation.table_name}_en.name")
+ .order(:name).map(&:id).map(&:to_s)
end
end
@@ -376,7 +378,9 @@ describe 'API V2 Storefront Products Spec', type: :request do
it 'returns products sorted by name with descending order' do
expect(json_response['data'].count).to eq store.products.available.count
- expect(json_response['data'].pluck(:id)).to eq store.products.available.order(name: :desc).map(&:id).map(&:to_s)
+ expect(json_response['data'].pluck(:id)).to eq store.products.available
+ .select("#{Spree::Product.table_name}.*, #{Spree::Product::Translation.table_name}_en.name")
+ .order(name: :desc).map(&:id).map(&:to_s)
end
end
end
@@ -727,7 +731,9 @@ describe 'API V2 Storefront Products Spec', type: :request do
end
context 'when filter by product property is applied' do
- before { get "/api/v2/storefront/products?filter[properties][#{property.filter_param}]=#{product_property.filter_param}" }
+ before {
+ get "/api/v2/storefront/products?filter[properties][#{property.filter_param}]=#{product_property.filter_param}"
+ }
it 'returns list of all available filters for products' do
expect(json_response['meta']['filters']['option_types'].count).to eq 2
diff --git a/api/spec/requests/spree/api/v2/storefront/taxons_spec.rb b/api/spec/requests/spree/api/v2/storefront/taxons_spec.rb
index 39ddd0d2d4..eabd678c84 100644
--- a/api/spec/requests/spree/api/v2/storefront/taxons_spec.rb
+++ b/api/spec/requests/spree/api/v2/storefront/taxons_spec.rb
@@ -109,7 +109,7 @@ describe 'Taxons Spec', type: :request do
it_behaves_like 'returns 200 HTTP status'
- it 'returns taxonx by name' do
+ it 'returns taxons by name' do
expect(json_response['data'].size).to eq(1)
expect(json_response['data'].last['attributes']['name']).to eq(taxons.last.name)
end
diff --git a/core/app/finders/spree/products/find.rb b/core/app/finders/spree/products/find.rb
index 320f6d47c0..fc06af2bd7 100644
--- a/core/app/finders/spree/products/find.rb
+++ b/core/app/finders/spree/products/find.rb
@@ -146,7 +146,12 @@ module Spree
def by_name(products)
return products unless name?
- products.like_any([:name], [name])
+ product_name = name
+
+ # i18n scope doesn't automatically get set here (mobility gem bug?) set it explicitly
+ products.i18n do
+ name.matches("%#{product_name}%")
+ end
end
def by_options(products)
@@ -209,21 +214,19 @@ module Spree
products
end
when 'name-a-z'
- products.order(name: :asc)
+ # workaround for Mobility issue #596 - explicitly select fields to avoid error when selecting distinct
+ products.i18n.
+ select("#{Product.table_name}.*").select(:name).order(name: :asc)
when 'name-z-a'
- products.order(name: :desc)
+ # workaround for Mobility issue #596
+ products.i18n.
+ select("#{Product.table_name}.*").select(:name).order(name: :desc)
when 'newest-first'
products.order(available_on: :desc)
when 'price-high-to-low'
- products.
- select("#{Product.table_name}.*, #{Spree::Price.table_name}.amount").
- reorder('').
- send(:descend_by_master_price)
+ order_by_price(products, :descend_by_master_price)
when 'price-low-to-high'
- products.
- select("#{Product.table_name}.*, #{Spree::Price.table_name}.amount").
- reorder('').
- send(:ascend_by_master_price)
+ order_by_price(products, :ascend_by_master_price)
end
end
@@ -265,6 +268,13 @@ module Spree
taxons = store.taxons.where(id: taxons_ids.to_s.split(','))
taxons.map(&:cached_self_and_descendants_ids).flatten.compact.uniq.map(&:to_s)
end
+
+ def order_by_price(scope, order_type)
+ scope.
+ select("#{Product.table_name}.*, #{Spree::Price.table_name}.amount").
+ reorder('').
+ send(order_type)
+ end
end
end
end
diff --git a/core/app/finders/spree/taxons/find.rb b/core/app/finders/spree/taxons/find.rb
index 0c3ac27f0d..6858455c8b 100644
--- a/core/app/finders/spree/taxons/find.rb
+++ b/core/app/finders/spree/taxons/find.rb
@@ -50,10 +50,6 @@ module Spree
name.present?
end
- def name_matcher
- Spree::Taxon.arel_table[:name].matches("%#{name}%")
- end
-
def by_ids(taxons)
return taxons unless ids?
@@ -91,7 +87,10 @@ module Spree
def by_name(taxons)
return taxons unless name?
- taxons.where(name_matcher)
+ taxon_name = name
+
+ # i18n mobility scope doesn't automatically get set for query blocks (Mobility issue #599) - set it explicitly
+ taxons.i18n { name.matches("%#{taxon_name}%") }
end
end
end
diff --git a/core/app/models/concerns/spree/product_scopes.rb b/core/app/models/concerns/spree/product_scopes.rb
index 4db73faaf8..340ad930c6 100644
--- a/core/app/models/concerns/spree/product_scopes.rb
+++ b/core/app/models/concerns/spree/product_scopes.rb
@@ -295,19 +295,10 @@ module Spree
# .search_by_name
if defined?(PgSearch)
include PgSearch::Model
-
- if defined?(SpreeGlobalize)
- pg_search_scope :search_by_name, associated_against: { translations: :name }, using: { tsearch: { any_word: true, prefix: true } }
- else
- pg_search_scope :search_by_name, against: :name, using: { tsearch: { any_word: true, prefix: true } }
- end
+ pg_search_scope :search_by_name, against: :name, using: { tsearch: { any_word: true, prefix: true } }
else
def self.search_by_name(query)
- if defined?(SpreeGlobalize)
- joins(:translations).order(:name).where("LOWER(#{Product::Translation.table_name}.name) LIKE LOWER(:query)", query: "%#{query}%").distinct
- else
- where("LOWER(#{Product.table_name}.name) LIKE LOWER(:query)", query: "%#{query}%")
- end
+ i18n { name.lower.matches("%#{query.downcase}%") }
end
end
search_scopes << :search_by_name
@@ -339,7 +330,10 @@ module Spree
case t
when ApplicationRecord then t
else
- Taxon.where(Taxon.arel_table[:name].eq(t)).or(Taxon.where(Taxon.arel_table[:id].eq(t))).or(Taxon.where(Taxon.arel_table[:permalink].matches("%/#{t}/"))).or(Taxon.where(Taxon.arel_table[:permalink].matches("#{t}/"))).first
+ Taxon.where(name: t).
+ or(Taxon.where(Taxon.arel_table[:id].eq(t))).
+ or(Taxon.where(Taxon.arel_table[:permalink].matches("%/#{t}/"))).
+ or(Taxon.where(Taxon.arel_table[:permalink].matches("#{t}/"))).first
end
end.compact.flatten.uniq
end
diff --git a/core/app/models/concerns/spree/translatable_resource.rb b/core/app/models/concerns/spree/translatable_resource.rb
new file mode 100644
index 0000000000..73c0de27b4
--- /dev/null
+++ b/core/app/models/concerns/spree/translatable_resource.rb
@@ -0,0 +1,25 @@
+module Spree
+ module TranslatableResource
+ extend ActiveSupport::Concern
+
+ included do
+ extend Mobility
+ default_scope { i18n }
+
+ def get_field_with_locale(locale, field_name, fallback: false)
+ # method will return nil if no translation is present due to fallback: false setting
+ public_send(field_name, locale: locale, fallback: fallback)
+ end
+ end
+
+ class_methods do
+ def translatable_fields
+ const_get(:TRANSLATABLE_FIELDS)
+ end
+
+ def translation_table_alias
+ "#{self::Translation.table_name}_#{Mobility.locale}"
+ end
+ end
+ end
+end
diff --git a/core/app/models/spree/product.rb b/core/app/models/spree/product.rb
index e4983fe0c3..8013939172 100644
--- a/core/app/models/spree/product.rb
+++ b/core/app/models/spree/product.rb
@@ -23,6 +23,7 @@ module Spree
extend FriendlyId
include ProductScopes
include MultiStoreResource
+ include TranslatableResource
include MemoizedData
include Metadata
if defined?(Spree::Webhooks)
@@ -32,12 +33,15 @@ module Spree
include Spree::VendorConcern
end
- MEMOIZED_METHODS = %w(total_on_hand taxonomy_ids taxon_and_ancestors category
+ MEMOIZED_METHODS = %w[total_on_hand taxonomy_ids taxon_and_ancestors category
default_variant_id tax_category default_variant
- purchasable? in_stock? backorderable?)
+ purchasable? in_stock? backorderable?]
- friendly_id :slug_candidates, use: :history
+ TRANSLATABLE_FIELDS = %i[name description slug meta_description meta_keywords meta_title].freeze
+ translates(*TRANSLATABLE_FIELDS)
+ Product::Translation.class_eval { acts_as_paranoid }
+ friendly_id :slug_candidates, use: [:history, :mobility]
acts_as_paranoid
auto_strip_attributes :name
@@ -415,7 +419,11 @@ module Spree
def punch_slug
# punch slug with date prefix to allow reuse of original
- update_column :slug, "#{Time.current.to_i}_#{slug}"[0..254] unless frozen?
+ return if frozen?
+
+ translations.with_deleted.each do |t|
+ t.update_column :slug, "#{Time.current.to_i}_#{t.slug}"[0..254]
+ end
end
def update_slug_history
diff --git a/core/app/models/spree/taxon.rb b/core/app/models/spree/taxon.rb
index d4be621e82..6589fbf9a1 100644
--- a/core/app/models/spree/taxon.rb
+++ b/core/app/models/spree/taxon.rb
@@ -3,6 +3,7 @@ require 'stringex'
module Spree
class Taxon < Spree::Base
+ include TranslatableResource
include Metadata
if defined?(Spree::Webhooks)
include Spree::Webhooks::HasWebhooks
@@ -27,7 +28,7 @@ module Spree
has_many :promotion_rule_taxons, class_name: 'Spree::PromotionRuleTaxon', dependent: :destroy
has_many :promotion_rules, through: :promotion_rule_taxons, class_name: 'Spree::PromotionRule'
- validates :name, presence: true, uniqueness: { scope: [:parent_id, :taxonomy_id], allow_blank: true, case_sensitive: false }
+ validates :name, presence: true, uniqueness: { scope: [:parent_id, :taxonomy_id], case_sensitive: false }
validates :taxonomy, presence: true
validates :permalink, uniqueness: { case_sensitive: false, scope: [:parent_id, :taxonomy_id] }
validates :hide_from_nav, inclusion: { in: [true, false] }
@@ -54,6 +55,9 @@ module Spree
scope :for_stores, ->(stores) { joins(:taxonomy).where(spree_taxonomies: { store_id: stores.ids }) }
+ TRANSLATABLE_FIELDS = %i[name description].freeze
+ translates(*TRANSLATABLE_FIELDS)
+
# indicate which filters should be used for a taxon
# this method should be customized to your own site
def applicable_filters
diff --git a/core/app/models/spree/variant.rb b/core/app/models/spree/variant.rb
index 02e79e4095..2be9a29c3b 100644
--- a/core/app/models/spree/variant.rb
+++ b/core/app/models/spree/variant.rb
@@ -122,16 +122,19 @@ module Spree
self.whitelisted_ransackable_scopes = %i(product_name_or_sku_cont search_by_product_name_or_sku)
def self.product_name_or_sku_cont(query)
- joins(:product).where("LOWER(#{Product.table_name}.name) LIKE LOWER(:query) OR LOWER(sku) LIKE LOWER(:query)", query: "%#{query}%")
+ join_product_translations.
+ where("LOWER(#{Product.translation_table_alias}.name) LIKE LOWER(:query)
+ OR LOWER(sku) LIKE LOWER(:query)", query: "%#{query}%")
+ end
+
+ def self.join_product_translations
+ joins(:product).joins("LEFT OUTER JOIN #{Product::Translation.table_name} #{Product.translation_table_alias}
+ ON #{Product.translation_table_alias}.spree_product_id = #{Product.table_name}.id
+ AND #{Product.translation_table_alias}.locale = '#{Mobility.locale}'")
end
def self.search_by_product_name_or_sku(query)
- if defined?(SpreeGlobalize)
- joins(product: :translations).where("LOWER(#{Product::Translation.table_name}.name) LIKE LOWER(:query) OR LOWER(sku) LIKE LOWER(:query)",
- query: "%#{query}%")
- else
- product_name_or_sku_cont(query)
- end
+ product_name_or_sku_cont(query)
end
def available?
diff --git a/core/app/sorters/spree/products/sort.rb b/core/app/sorters/spree/products/sort.rb
index dab39cbc0e..0eca2d54dd 100644
--- a/core/app/sorters/spree/products/sort.rb
+++ b/core/app/sorters/spree/products/sort.rb
@@ -11,6 +11,8 @@ module Spree
products = by_price(products)
products = by_sku(products)
+ products = select_translatable_fields(products)
+
products.distinct
end
@@ -46,6 +48,27 @@ module Spree
def sort_by?(field)
sort.detect { |s| s[0] == field }
end
+
+ # Add translatable fields to SELECT statement to avoid InvalidColumnReference error (Mobility bug workaround)
+ def select_translatable_fields(scope)
+ translatable_fields = translatable_sortable_fields
+ return scope if translatable_fields.empty?
+
+ # if sorting by 'sku' or 'price', spree_products.* is already included in SELECT statement
+ if sort_by?('sku') || sort_by?('price')
+ scope.i18n.select(*translatable_fields)
+ else
+ scope.i18n.select("#{Product.table_name}.*").select(*translatable_fields)
+ end
+ end
+
+ def translatable_sortable_fields
+ fields = []
+ Product.translatable_fields.each do |field|
+ fields << field if sort_by?(field.to_s)
+ end
+ fields
+ end
end
end
end
diff --git a/core/brakeman.ignore b/core/brakeman.ignore
index c82e0794ed..24e68c5178 100644
--- a/core/brakeman.ignore
+++ b/core/brakeman.ignore
@@ -1,20 +1,144 @@
{
- "ignored_warnings": [
- {
- "fingerprint": "011b2643940ba1112f7a737e403abe3616ad91764703c801cc35a48d36b721da",
- "note": "interpolating table name"
- },
- {
- "fingerprint": "965d3919f811ab63b7b8d62da528559a7f38dc122c57efea7136e7ec5ef1f062",
- "note": "interpolating table name"
- },
- {
- "fingerprint": "abd8e90e7a7dfbcdcd6d44fd3fb550598aee6d7a9ef2bb132ad1a18a3c50be30",
- "note": "interpolating table name"
- },
- {
- "fingerprint": "efcc57e1a5648d7db59d1beaf5e399d2278539a8667b19c520b305a6ca7e15e8",
- "note": "interpolating table name"
- }
- ]
+ "ignored_warnings": [
+ {
+ "warning_type": "SQL Injection",
+ "warning_code": 0,
+ "fingerprint": "011b2643940ba1112f7a737e403abe3616ad91764703c801cc35a48d36b721da",
+ "check_name": "SQL",
+ "message": "Possible SQL injection",
+ "file": "app/models/concerns/spree/product_scopes.rb",
+ "line": 63,
+ "link": "https://brakemanscanner.org/docs/warning_types/sql_injection/",
+ "code": "where(\"#{price_table_name}.amount <= ?\", price)",
+ "render_path": null,
+ "location": {
+ "type": "method",
+ "class": "Spree",
+ "method": null
+ },
+ "user_input": "price_table_name",
+ "confidence": "Medium",
+ "cwe_id": [
+ 89
+ ],
+ "note": "interpolating table name"
+ },
+ {
+ "warning_type": "SQL Injection",
+ "warning_code": 0,
+ "fingerprint": "67e80bcfa8898315b0f8642bc61990c8319bc7a468453efc048757a439e53e0b",
+ "check_name": "SQL",
+ "message": "Possible SQL injection",
+ "file": "app/models/spree/variant.rb",
+ "line": 131,
+ "link": "https://brakemanscanner.org/docs/warning_types/sql_injection/",
+ "code": "joins(:product).joins(\"LEFT OUTER JOIN #{Product::Translation.table_name} #{Product.translation_table_alias}\\n ON #{Product.translation_table_alias}.spree_product_id = #{Product.table_name}.id\\n AND #{Product.translation_table_alias}.locale = '#{Mobility.locale}'\")",
+ "render_path": null,
+ "location": {
+ "type": "method",
+ "class": "Spree::Variant",
+ "method": "Spree::Variant.join_product_translations"
+ },
+ "user_input": "Product.translation_table_alias",
+ "confidence": "Weak",
+ "cwe_id": [
+ 89
+ ],
+ "note": ""
+ },
+ {
+ "warning_type": "SQL Injection",
+ "warning_code": 0,
+ "fingerprint": "965d3919f811ab63b7b8d62da528559a7f38dc122c57efea7136e7ec5ef1f062",
+ "check_name": "SQL",
+ "message": "Possible SQL injection",
+ "file": "app/models/concerns/spree/product_scopes.rb",
+ "line": 67,
+ "link": "https://brakemanscanner.org/docs/warning_types/sql_injection/",
+ "code": "where(\"#{price_table_name}.amount >= ?\", price)",
+ "render_path": null,
+ "location": {
+ "type": "method",
+ "class": "Spree::ProductScopes",
+ "method": null
+ },
+ "user_input": "price_table_name",
+ "confidence": "Medium",
+ "cwe_id": [
+ 89
+ ],
+ "note": "interpolating table name"
+ },
+ {
+ "warning_type": "SQL Injection",
+ "warning_code": 0,
+ "fingerprint": "abd8e90e7a7dfbcdcd6d44fd3fb550598aee6d7a9ef2bb132ad1a18a3c50be30",
+ "check_name": "SQL",
+ "message": "Possible SQL injection",
+ "file": "app/models/concerns/spree/product_scopes.rb",
+ "line": 63,
+ "link": "https://brakemanscanner.org/docs/warning_types/sql_injection/",
+ "code": "where(\"#{price_table_name}.amount <= ?\", price)",
+ "render_path": null,
+ "location": {
+ "type": "method",
+ "class": "Spree::ProductScopes",
+ "method": null
+ },
+ "user_input": "price_table_name",
+ "confidence": "Medium",
+ "cwe_id": [
+ 89
+ ],
+ "note": "interpolating table name"
+ },
+ {
+ "warning_type": "SQL Injection",
+ "warning_code": 0,
+ "fingerprint": "e6bb08c012af19589f796e26b4dc2230ea1c1949c7cd6464ca025939513d8b3d",
+ "check_name": "SQL",
+ "message": "Possible SQL injection",
+ "file": "app/models/spree/variant.rb",
+ "line": 126,
+ "link": "https://brakemanscanner.org/docs/warning_types/sql_injection/",
+ "code": "join_product_translations.where(\"LOWER(#{Product.translation_table_alias}.name) LIKE LOWER(:query)\\n OR LOWER(sku) LIKE LOWER(:query)\", :query => (\"%#{query}%\"))",
+ "render_path": null,
+ "location": {
+ "type": "method",
+ "class": "Spree::Variant",
+ "method": "Spree::Variant.product_name_or_sku_cont"
+ },
+ "user_input": "Product.translation_table_alias",
+ "confidence": "Weak",
+ "cwe_id": [
+ 89
+ ],
+ "note": ""
+ },
+ {
+ "warning_type": "SQL Injection",
+ "warning_code": 0,
+ "fingerprint": "efcc57e1a5648d7db59d1beaf5e399d2278539a8667b19c520b305a6ca7e15e8",
+ "check_name": "SQL",
+ "message": "Possible SQL injection",
+ "file": "app/models/concerns/spree/product_scopes.rb",
+ "line": 67,
+ "link": "https://brakemanscanner.org/docs/warning_types/sql_injection/",
+ "code": "where(\"#{price_table_name}.amount >= ?\", price)",
+ "render_path": null,
+ "location": {
+ "type": "method",
+ "class": "Spree",
+ "method": null
+ },
+ "user_input": "price_table_name",
+ "confidence": "Medium",
+ "cwe_id": [
+ 89
+ ],
+ "note": "interpolating table name"
+ }
+ ],
+ "updated": "2022-12-29 09:32:26 +0100",
+ "brakeman_version": "5.4.0"
}
diff --git a/core/config/initializers/mobility.rb b/core/config/initializers/mobility.rb
new file mode 100644
index 0000000000..a554d7dafc
--- /dev/null
+++ b/core/config/initializers/mobility.rb
@@ -0,0 +1,18 @@
+Mobility.configure do |config|
+ config.plugins do
+ ransack
+ backend :table
+ active_record
+ reader
+ writer
+ backend_reader
+ query
+ cache
+ fallbacks
+ locale_accessors
+ presence
+ dirty
+ end
+
+ config.defaults[:fallbacks] = true
+end
diff --git a/core/config/locales/en.yml b/core/config/locales/en.yml
index db521ee52d..6e73298ae8 100644
--- a/core/config/locales/en.yml
+++ b/core/config/locales/en.yml
@@ -1112,6 +1112,7 @@ en:
notice_messages:
icon_removed: Image has been successfully removed
prices_saved: Prices successfully saved
+ translations_saved: Translations successfully saved
product_cloned: Product has been cloned
product_deleted: Product has been deleted
product_not_cloned: "Product could not be cloned. Reason: %{error}"
diff --git a/core/db/migrate/20220706112554_create_product_name_and_description_translations_for_mobility_table_backend.rb b/core/db/migrate/20220706112554_create_product_name_and_description_translations_for_mobility_table_backend.rb
new file mode 100644
index 0000000000..fd00e191c8
--- /dev/null
+++ b/core/db/migrate/20220706112554_create_product_name_and_description_translations_for_mobility_table_backend.rb
@@ -0,0 +1,25 @@
+class CreateProductNameAndDescriptionTranslationsForMobilityTableBackend < ActiveRecord::Migration[6.1]
+ def change
+ # create translation table only if spree_globalize has not already created it
+ if ActiveRecord::Base.connection.table_exists? 'spree_product_translations'
+ # replacing this with index on spree_product_id and locale
+ remove_index :spree_product_translations, name: "index_spree_product_translations_on_spree_product_id", if_exists: true
+ else
+ create_table :spree_product_translations do |t|
+
+ # Translated attribute(s)
+ t.string :name
+ t.text :description
+
+ t.string :locale, null: false
+ t.references :spree_product, null: false, foreign_key: true, index: false
+
+ t.timestamps null: false
+ end
+
+ add_index :spree_product_translations, :locale, name: :index_spree_product_translations_on_locale
+ end
+
+ add_index :spree_product_translations, [:spree_product_id, :locale], name: :unique_product_id_per_locale, unique: true
+ end
+end
diff --git a/core/db/migrate/20220715083542_create_spree_product_meta_description_and_meta_keywords_and_meta_title_translations_for_mobility_table_backend.rb b/core/db/migrate/20220715083542_create_spree_product_meta_description_and_meta_keywords_and_meta_title_translations_for_mobility_table_backend.rb
new file mode 100644
index 0000000000..5d712d9cb0
--- /dev/null
+++ b/core/db/migrate/20220715083542_create_spree_product_meta_description_and_meta_keywords_and_meta_title_translations_for_mobility_table_backend.rb
@@ -0,0 +1,7 @@
+class CreateSpreeProductMetaDescriptionAndMetaKeywordsAndMetaTitleTranslationsForMobilityTableBackend < ActiveRecord::Migration[6.1]
+ def change
+ add_column :spree_product_translations, :meta_description, :text, if_not_exists: true
+ add_column :spree_product_translations, :meta_keywords, :string, if_not_exists: true
+ add_column :spree_product_translations, :meta_title, :string, if_not_exists: true
+ end
+end
diff --git a/core/db/migrate/20220715120222_change_product_name_null_to_true.rb b/core/db/migrate/20220715120222_change_product_name_null_to_true.rb
new file mode 100644
index 0000000000..007da64c9b
--- /dev/null
+++ b/core/db/migrate/20220715120222_change_product_name_null_to_true.rb
@@ -0,0 +1,5 @@
+class ChangeProductNameNullToTrue < ActiveRecord::Migration[6.1]
+ def change
+ change_column_null :spree_products, :name, true
+ end
+end
diff --git a/core/db/migrate/20220718100743_create_spree_taxon_name_and_description_translations_for_mobility_table_backend.rb b/core/db/migrate/20220718100743_create_spree_taxon_name_and_description_translations_for_mobility_table_backend.rb
new file mode 100644
index 0000000000..4b654a69f8
--- /dev/null
+++ b/core/db/migrate/20220718100743_create_spree_taxon_name_and_description_translations_for_mobility_table_backend.rb
@@ -0,0 +1,24 @@
+class CreateSpreeTaxonNameAndDescriptionTranslationsForMobilityTableBackend < ActiveRecord::Migration[6.1]
+ def change
+ # create translation table only if spree_globalize has not already created it
+ if ActiveRecord::Base.connection.table_exists? 'spree_taxon_translations'
+ # replacing this with index on spree_taxon_id and locale
+ remove_index :spree_taxon_translations, name: "index_spree_taxon_translations_on_spree_taxon_id", if_exists: true
+ else
+ create_table :spree_taxon_translations do |t|
+ # Translated attribute(s)
+ t.string :name
+ t.text :description
+
+ t.string :locale, null: false
+ t.references :spree_taxon, null: false, foreign_key: true, index: false
+
+ t.timestamps null: false
+ end
+
+ add_index :spree_taxon_translations, :locale, name: :index_spree_taxon_translations_on_locale
+ end
+
+ add_index :spree_taxon_translations, [:spree_taxon_id, :locale], name: :index_spree_taxon_translations_on_spree_taxon_id_and_locale, unique: true
+ end
+end
diff --git a/core/db/migrate/20220718100948_change_taxon_name_null_to_true.rb b/core/db/migrate/20220718100948_change_taxon_name_null_to_true.rb
new file mode 100644
index 0000000000..0133915630
--- /dev/null
+++ b/core/db/migrate/20220718100948_change_taxon_name_null_to_true.rb
@@ -0,0 +1,5 @@
+class ChangeTaxonNameNullToTrue < ActiveRecord::Migration[6.1]
+ def change
+ change_column_null :spree_taxons, :name, true
+ end
+end
diff --git a/core/db/migrate/20220802070609_add_locale_to_friendly_id_slugs.rb b/core/db/migrate/20220802070609_add_locale_to_friendly_id_slugs.rb
new file mode 100644
index 0000000000..1c2ba8aebe
--- /dev/null
+++ b/core/db/migrate/20220802070609_add_locale_to_friendly_id_slugs.rb
@@ -0,0 +1,11 @@
+class AddLocaleToFriendlyIdSlugs < ActiveRecord::Migration[6.1]
+ def change
+ add_column :friendly_id_slugs, :locale, :string, null: :false, after: :scope
+
+ remove_index :friendly_id_slugs, [:slug, :sluggable_type]
+ add_index :friendly_id_slugs, [:slug, :sluggable_type, :locale], length: { slug: 140, sluggable_type: 50, locale: 2 }
+ remove_index :friendly_id_slugs, [:slug, :sluggable_type, :scope]
+ add_index :friendly_id_slugs, [:slug, :sluggable_type, :scope, :locale], length: { slug: 70, sluggable_type: 50, scope: 70, locale: 2 }, unique: true, name: :index_friendly_id_slugs_unique
+ add_index :friendly_id_slugs, :locale
+ end
+end
diff --git a/core/db/migrate/20220802073225_create_spree_product_slug_translations_for_mobility_table_backend.rb b/core/db/migrate/20220802073225_create_spree_product_slug_translations_for_mobility_table_backend.rb
new file mode 100644
index 0000000000..4d8f972459
--- /dev/null
+++ b/core/db/migrate/20220802073225_create_spree_product_slug_translations_for_mobility_table_backend.rb
@@ -0,0 +1,5 @@
+class CreateSpreeProductSlugTranslationsForMobilityTableBackend < ActiveRecord::Migration[6.1]
+ def change
+ add_column :spree_product_translations, :slug, :string
+ end
+end
diff --git a/core/db/migrate/20221219123957_add_deleted_at_to_product_translations.rb b/core/db/migrate/20221219123957_add_deleted_at_to_product_translations.rb
new file mode 100644
index 0000000000..28e74325e7
--- /dev/null
+++ b/core/db/migrate/20221219123957_add_deleted_at_to_product_translations.rb
@@ -0,0 +1,6 @@
+class AddDeletedAtToProductTranslations < ActiveRecord::Migration[6.1]
+ def change
+ add_column :spree_product_translations, :deleted_at, :datetime
+ add_index :spree_product_translations, :deleted_at
+ end
+end
diff --git a/core/db/migrate/20221220133432_add_uniqueness_constraint_to_product_translations.rb b/core/db/migrate/20221220133432_add_uniqueness_constraint_to_product_translations.rb
new file mode 100644
index 0000000000..b100e67c0a
--- /dev/null
+++ b/core/db/migrate/20221220133432_add_uniqueness_constraint_to_product_translations.rb
@@ -0,0 +1,5 @@
+class AddUniquenessConstraintToProductTranslations < ActiveRecord::Migration[6.1]
+ def change
+ add_index :spree_product_translations, [:locale, :slug], unique: true, name: 'unique_slug_per_locale'
+ end
+end
diff --git a/core/db/migrate/20230110142344_backfill_friendly_id_slug_locale.rb b/core/db/migrate/20230110142344_backfill_friendly_id_slug_locale.rb
new file mode 100644
index 0000000000..c2c168d94c
--- /dev/null
+++ b/core/db/migrate/20230110142344_backfill_friendly_id_slug_locale.rb
@@ -0,0 +1,15 @@
+class BackfillFriendlyIdSlugLocale < ActiveRecord::Migration[6.1]
+ DEFAULT_LOCALE = 'en'
+
+ def up
+ ActiveRecord::Base.connection.execute("
+ UPDATE friendly_id_slugs SET locale = '#{DEFAULT_LOCALE}'
+ ")
+ end
+
+ def down
+ ActiveRecord::Base.connection.execute("
+ UPDATE friendly_id_slugs SET locale = NULL
+ ")
+ end
+end
diff --git a/core/db/migrate/20230111121534_add_additional_taxon_translation_fields.rb b/core/db/migrate/20230111121534_add_additional_taxon_translation_fields.rb
new file mode 100644
index 0000000000..f59a7ac10b
--- /dev/null
+++ b/core/db/migrate/20230111121534_add_additional_taxon_translation_fields.rb
@@ -0,0 +1,8 @@
+class AddAdditionalTaxonTranslationFields < ActiveRecord::Migration[6.1]
+ def change
+ add_column :spree_taxon_translations, :meta_title, :string, if_not_exists: true
+ add_column :spree_taxon_translations, :meta_description, :string, if_not_exists: true
+ add_column :spree_taxon_translations, :meta_keywords, :string, if_not_exists: true
+ add_column :spree_taxon_translations, :permalink, :string, if_not_exists: true
+ end
+end
diff --git a/core/db/migrate/20230111122511_transfer_product_and_taxon_data_to_translatable_tables.rb b/core/db/migrate/20230111122511_transfer_product_and_taxon_data_to_translatable_tables.rb
new file mode 100644
index 0000000000..8c5b024769
--- /dev/null
+++ b/core/db/migrate/20230111122511_transfer_product_and_taxon_data_to_translatable_tables.rb
@@ -0,0 +1,82 @@
+class TransferProductAndTaxonDataToTranslatableTables < ActiveRecord::Migration[6.1]
+ DEFAULT_LOCALE = 'en'
+ PRODUCTS_TABLE = 'spree_products'
+ PRODUCT_TRANSLATIONS_TABLE = 'spree_product_translations'
+ TAXONS_TABLE = 'spree_taxons'
+ TAXON_TRANSLATIONS_TABLE = 'spree_taxon_translations'
+
+ def up
+ # Only transfer data if translation tables are being newly created / no translations exist
+ # Otherwise, assume translation data is already in place from spree_globalize
+
+ # Products
+ if not Spree::Product::Translation.exists?
+ ActiveRecord::Base.connection.execute("
+ INSERT INTO #{PRODUCT_TRANSLATIONS_TABLE} (name, description, locale, spree_product_id, created_at, updated_at, meta_description, meta_keywords, meta_title, slug)
+ SELECT name, description, '#{DEFAULT_LOCALE}' as locale, id, created_at, updated_at, meta_description, meta_keywords, meta_title, slug FROM #{PRODUCTS_TABLE};
+ ")
+ ActiveRecord::Base.connection.execute("
+ UPDATE #{PRODUCTS_TABLE}
+ SET name=null, description=null, meta_description=null, meta_keywords=null, meta_title=null, slug=null;
+ ")
+ end
+
+ # Taxons
+ if not Spree::Taxon::Translation.exists?
+ ActiveRecord::Base.connection.execute("
+ INSERT INTO #{TAXON_TRANSLATIONS_TABLE} (name, description, meta_title, meta_description, meta_keywords, permalink, locale, spree_taxon_id, created_at, updated_at)
+ SELECT name, description, meta_title, meta_description, meta_keywords, permalink, '#{DEFAULT_LOCALE}' as locale, id, created_at, updated_at FROM #{TAXONS_TABLE};
+ ")
+ ActiveRecord::Base.connection.execute("
+ UPDATE #{TAXONS_TABLE}
+ SET name=null, description=null, meta_title=null, meta_description=null, meta_keywords=null, permalink=null;
+ ")
+ end
+ end
+
+ def down
+ ActiveRecord::Base.connection.execute("
+ UPDATE #{PRODUCTS_TABLE} as products
+ SET (name,
+ description,
+ meta_description,
+ meta_keywords,
+ meta_title,
+ slug) =
+ (t_products.name,
+ t_products.description,
+ t_products.meta_description,
+ t_products.meta_keywords,
+ t_products.meta_title,
+ t_products.slug)
+ FROM #{PRODUCT_TRANSLATIONS_TABLE} AS t_products
+ WHERE t_products.spree_product_id = products.id
+ ")
+
+ ActiveRecord::Base.connection.execute("
+ TRUNCATE TABLE #{PRODUCT_TRANSLATIONS_TABLE}
+ ")
+
+ ActiveRecord::Base.connection.execute("
+ UPDATE #{TAXONS_TABLE} as taxons
+ SET (name,
+ description,
+ meta_title,
+ meta_description,
+ meta_keywords,
+ permalink) =
+ (t_taxons.name,
+ t_taxons.description,
+ t_taxons.meta_title,
+ t_taxons.meta_description,
+ t_taxons.meta_keywords,
+ t_taxons.permalink)
+ FROM #{TAXON_TRANSLATIONS_TABLE} AS t_taxons
+ WHERE t_taxons.spree_taxon_id = taxons.id
+ ")
+
+ ActiveRecord::Base.connection.execute("
+ TRUNCATE TABLE #{TAXON_TRANSLATIONS_TABLE}
+ ")
+ end
+end
diff --git a/core/lib/spree/core.rb b/core/lib/spree/core.rb
index 79c5b4858a..31a04ed58b 100644
--- a/core/lib/spree/core.rb
+++ b/core/lib/spree/core.rb
@@ -13,6 +13,7 @@ require 'cancan'
require 'friendly_id'
require 'kaminari'
require 'monetize'
+require 'mobility'
require 'paranoia'
require 'ransack'
require 'state_machines-activerecord'
diff --git a/core/lib/spree/core/product_duplicator.rb b/core/lib/spree/core/product_duplicator.rb
index f5ae3c0b03..91b308c58a 100644
--- a/core/lib/spree/core/product_duplicator.rb
+++ b/core/lib/spree/core/product_duplicator.rb
@@ -26,7 +26,7 @@ module Spree
def duplicate_product
product.dup.tap do |new_product|
- new_product.name = "COPY OF #{product.name}"
+ product.translations.each { |t| new_product.send(:name=, "COPY OF #{t.name}", locale: t.locale) }
new_product.taxons = product.taxons
new_product.stores = product.stores
new_product.created_at = nil
diff --git a/core/spec/finders/spree/taxons/find_spec.rb b/core/spec/finders/spree/taxons/find_spec.rb
new file mode 100644
index 0000000000..077b04af3d
--- /dev/null
+++ b/core/spec/finders/spree/taxons/find_spec.rb
@@ -0,0 +1,28 @@
+require 'spec_helper'
+
+module Spree
+ describe Taxons::Find do
+ let!(:taxon_shirts) { create(:taxon, name: 'Shirts') }
+ let!(:taxon_shorts) { create(:taxon, name: 'Shorts') }
+ let!(:taxon_shoes) { create(:taxon, name: 'Shoes') }
+
+ describe 'filtering by taxon property' do
+ subject do
+ described_class.new(
+ scope: Spree::Taxon.all,
+ params: params
+ ).execute
+ end
+
+ context 'when filtering by taxon name' do
+ let(:params) { { filter: { 'name': 'Shirts' } } }
+
+ it 'returns taxon with matching name' do
+ expect(subject).to contain_exactly(taxon_shirts)
+ end
+ end
+
+ end
+
+ end
+end
diff --git a/core/spec/models/spree/product_spec.rb b/core/spec/models/spree/product_spec.rb
index 21c0eeed03..455354d39d 100644
--- a/core/spec/models/spree/product_spec.rb
+++ b/core/spec/models/spree/product_spec.rb
@@ -69,6 +69,18 @@ describe Spree::Product, type: :model do
expect(clone.images.size).to eq(product.images.size)
end
+ context 'when translations exist for another locale' do
+ before do
+ Mobility.with_locale(:fr) { product.name = "french name" }
+ product.save!
+ end
+
+ it 'duplicates translations for all locales' do
+ clone = product.duplicate
+ expect(clone.name(locale: :fr)).to eq ('COPY OF ' + product.name(locale: :fr))
+ end
+ end
+
it 'calls #duplicate_extra' do
expect_any_instance_of(Spree::Product).to receive(:duplicate_extra).
with(product)
@@ -236,7 +248,21 @@ describe Spree::Product, type: :model do
context 'when product destroyed' do
it 'renames slug' do
- expect { product.destroy }.to change(product, :slug)
+ product.destroy
+ expect(product.translations.with_deleted.where(locale: :en).first.slug).to match(/[0-9]+_product-[0-9]+/)
+ end
+
+ context 'when more than one translation exists' do
+ before {
+ product.send(:slug=, "french_slug", locale: :fr)
+ product.save!
+ }
+
+ it 'renames slug for all translations' do
+ product.destroy
+ expect(product.translations.with_deleted.where(locale: :en).first.slug).to match(/[0-9]+_product-[0-9]+/)
+ expect(product.translations.with_deleted.where(locale: :fr).first.slug).to match(/[0-9]+_french_slug/)
+ end
end
context 'when slug is already at or near max length' do
diff --git a/core/spree_core.gemspec b/core/spree_core.gemspec
index c8f197ec06..57a37a7523 100644
--- a/core/spree_core.gemspec
+++ b/core/spree_core.gemspec
@@ -54,4 +54,8 @@ Gem::Specification.new do |s|
s.add_dependency 'image_processing', '~> 1.2'
s.add_dependency 'active_storage_validations', '~> 0.9', '<= 0.9.5'
s.add_dependency 'activerecord-typedstore'
+ s.add_dependency 'mobility', '~> 1.2.9'
+ # Upgrade mobility-ransack version to 1.2.0 after this issue is resolved
+ s.add_dependency 'mobility-ransack', '~> 1.0.1'
+ s.add_dependency 'friendly_id-mobility', '~> 1.0.3'
end
diff --git a/sample/db/samples/product_properties.rb b/sample/db/samples/product_properties.rb
index ac603d68b4..4b2695cebd 100644
--- a/sample/db/samples/product_properties.rb
+++ b/sample/db/samples/product_properties.rb
@@ -43,7 +43,6 @@ men_tshirts_properties = [
'gender' => 'Men\'s'
}
]
-
Spree::Taxon.find_by!(name: 'Men').children.find_by!(name: 'T-shirts').products.each do |product|
men_tshirts_properties.sample.each do |prop_name, prop_value|
product.set_property(prop_name, prop_value, prop_name.gsub('_', ' ').capitalize)
diff --git a/sample/db/samples/products.rb b/sample/db/samples/products.rb
index 7beac2b2fd..671f446fd1 100644
--- a/sample/db/samples/products.rb
+++ b/sample/db/samples/products.rb
@@ -19,17 +19,20 @@ end.uniq
PRODUCTS.each do |(parent_name, taxon_name, product_name)|
parent = Spree::Taxon.find_by!(name: parent_name)
taxon = parent.children.find_by!(name: taxon_name)
- Spree::Product.where(name: product_name.titleize).first_or_create! do |product|
+ next if Spree::Product.where(name: product_name.titleize).any?
+
+ Spree::Product.create!(name: product_name.titleize) do |product|
product.price = rand(10...100) + 0.99
product.description = FFaker::Lorem.paragraph
product.available_on = Time.zone.now
product.make_active_at = Time.zone.now
product.status = 'active'
- if parent_name == 'Women' and %w[Dresses Skirts].include?(taxon_name)
- product.option_types = [color, length, size]
- else
- product.option_types = [color, size]
- end
+ product.option_types =
+ if parent_name == 'Women' && %w[Dresses Skirts].include?(taxon_name)
+ [color, length, size]
+ else
+ [color, size]
+ end
product.shipping_category = default_shipping_category
product.tax_category = clothing_tax_category
product.sku = [taxon_name.delete(' '), product_name.delete(' '), product.price].join('_')
diff --git a/sample/db/samples/taxons.rb b/sample/db/samples/taxons.rb
index 40bcf62475..c3f0bb66f4 100644
--- a/sample/db/samples/taxons.rb
+++ b/sample/db/samples/taxons.rb
@@ -16,34 +16,65 @@ categories = Spree::Taxonomy.find_by!(name: I18n.t('spree.taxonomy_categories_na
categories_taxon = Spree::Taxon.where(name: I18n.t('spree.taxonomy_categories_name')).first_or_create!
TAXON_NAMES.each do |taxon_name|
- taxon = categories_taxon.children.where(name: taxon_name).first_or_create!
+ taxon =
+ if not categories_taxon.children.where(name: taxon_name).exists?
+ Spree::Taxon.create!(name: taxon_name, parent_id: categories_taxon.id, taxonomy: categories)
+ else
+ categories_taxon.children.where(name: taxon_name).first
+ end
taxon.taxonomy = categories
taxon.save!
end
CHILDREN_TAXON_NAMES.each do |(parent_name, taxon_name)|
parent = Spree::Taxon.where(name: parent_name).first
- taxon = parent.children.where(name: taxon_name).first_or_create!
+ taxon =
+ if parent.children.where(name: taxon_name).any?
+ parent.children.where(name: taxon_name).first
+ else
+ Spree::Taxon.create!(name: taxon_name, parent_id: parent.id, taxonomy: categories)
+ end
taxon.taxonomy = categories
taxon.save!
end
-taxon = categories_taxon.children.where(name: 'New').first_or_create!
+taxon =
+ if categories_taxon.children.where(name: 'New').any?
+ categories_taxon.children.where(name: 'New').first
+ else
+ Spree::Taxon.create!(name: 'New', parent_id: categories_taxon.id, taxonomy: categories)
+ end
taxon.taxonomy = categories
taxon.save!
ADDITIONAL_TAXONS.each do |taxon_name|
- taxon = categories_taxon.children.where(name: taxon_name).first_or_create!
+ taxon =
+ if categories_taxon.children.where(name: taxon_name).any?
+ categories_taxon.children.where(name: taxon_name).first
+ else
+ Spree::Taxon.create!(name: taxon_name, parent_id: categories_taxon.id, taxonomy: categories)
+ end
taxon.taxonomy = categories
taxon.save!
end
SPECIAL_TAXONS.each do |parent_name, taxon_name|
- parent = categories_taxon.children.where(name: parent_name.to_s).first_or_create!
+ parent =
+ if categories_taxon.children.where(name: parent_name.to_s).any?
+ categories_taxon.children.where(name: parent_name.to_s).first
+ else
+ Spree::Taxon.create!(name: parent_name.to_s, parent_id: categories_taxon.id, taxonomy: categories)
+ end
parent.taxonomy = categories
parent.save!
- taxon = parent.children.where(name: taxon_name).first_or_create!
+ taxon =
+ if parent.children.where(name: taxon_name).any?
+ parent.children.where(name: taxon_name).first
+ else
+ Spree::Taxon.create!(name: taxon_name, parent_id: parent.id, taxonomy: categories)
+ end
+
taxon.taxonomy = categories
taxon.save!
end
diff --git a/sample/lib/spree_sample.rb b/sample/lib/spree_sample.rb
index 4421947d90..ff014991b5 100644
--- a/sample/lib/spree_sample.rb
+++ b/sample/lib/spree_sample.rb
@@ -5,6 +5,7 @@ module SpreeSample
class Engine < Rails::Engine
engine_name 'spree_sample'
+ config.active_record.yaml_column_permitted_classes = [Symbol]
# Needs to be here so we can access it inside the tests
def self.load_samples
Spree::Sample.load_sample('addresses')