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.

fix some issues with the admin products controller

Łukasz Adamczyka61d3508c812
diff --git a/admin/app/helpers/tom_select_helper.rb b/admin/app/helpers/tom_select_helper.rb
new file mode 100644
index 0000000000..c8b6daa154
--- /dev/null
+++ b/admin/app/helpers/tom_select_helper.rb
@@ -0,0 +1,42 @@
+# frozen_string_literal: true
+
+module TomSelectHelper
+ #
+ # @param [String] name
+ # @param [Hash] options
+ def tom_select_tag(name, options = { multiple: false, url: nil, active_option: nil, class: 'w-100', create: false, select_data: {} })
+ stimulus_options = options[:data] || {}
+ stimulus_options[:controller] = 'select'
+ stimulus_options['select-active-option-value'] = options[:active_option].as_json if options[:active_option]
+ stimulus_options['select-empty-option-value'] = options[:empty_option] if options[:empty_option]
+ stimulus_options['select-options-value'] = options[:options].as_json if options[:options]
+ stimulus_options['select-url-value'] = options[:url] if options[:url]
+ stimulus_options['select-remote-search-value'] = options[:remote_search] if options[:remote_search]
+ stimulus_options['select-remote-search-params-value'] = options[:remote_search_params] if options[:remote_search_params]
+ stimulus_options['select-remote-search-active-option-value'] = options[:remote_search_active_option].as_json if options[:remote_search_active_option]
+ stimulus_options['select-multiple-value'] = options[:multiple]
+ stimulus_options['select-create-value'] = options[:create]
+ stimulus_options['select-value-field-value'] = options[:value_field] if options[:value_field].present?
+ stimulus_options['select-search-field-value'] = options[:value_field] if options[:search_field].present?
+ stimulus_options['select-label-field-value'] = options[:value_field] if options[:label_field].present?
+ stimulus_options['select-sort-field-value'] = options[:value_field] if options[:sort_field].present?
+ html_select_options = if options[:grouped_options].present?
+ grouped_options_for_select(options[:grouped_options], options[:active_option])
+ else
+ options_for_select(
+ options[:options] || options[:preloaded_options] || [], options[:active_option]
+ )
+ end
+
+ content_tag :div, data: stimulus_options, class: options[:class] do
+ select_tag name, html_select_options,
+ multiple: options[:multiple],
+ data: { 'select-target': 'input', **(options[:select_data] || {}) },
+ class: options[:select_class] || 'd-none',
+ required: options[:required],
+ disabled: options[:disabled],
+ include_blank: options[:include_blank],
+ placeholder: options[:placeholder]
+ end
+ end
+end
diff --git a/admin/spec/controllers/spree/admin/products_controller_spec.rb b/admin/spec/controllers/spree/admin/products_controller_spec.rb
index 1836a96c7b..152ef6ae10 100644
--- a/admin/spec/controllers/spree/admin/products_controller_spec.rb
+++ b/admin/spec/controllers/spree/admin/products_controller_spec.rb
@@ -3,6 +3,8 @@ require 'spec_helper'
RSpec.describe Spree::Admin::ProductsController, type: :controller do
stub_authorization!
+ let(:user) { create(:admin_user) }
+
before do
allow(controller).to receive(:current_ability).and_call_original
end
@@ -140,9 +142,12 @@ RSpec.describe Spree::Admin::ProductsController, type: :controller do
describe 'POST #create' do
subject { post :create, params: { product: product_params } }
+
let(:stock_location) { create(:stock_location) }
let(:other_stock_location) { create(:stock_location) }
+ let(:shipping_category) { create(:shipping_category, name: 'Default') }
+
context 'without variants' do
let(:product_params) do
{
@@ -154,7 +159,8 @@ RSpec.describe Spree::Admin::ProductsController, type: :controller do
width: 10,
depth: 10,
dimensions_unit: 'cm',
- weight_unit: 'kg'
+ weight_unit: 'kg',
+ shipping_category_id: shipping_category.id
}
end
@@ -250,7 +256,8 @@ RSpec.describe Spree::Admin::ProductsController, type: :controller do
}
]
}
- }
+ },
+ shipping_category_id: shipping_category.id
}
end
@@ -353,7 +360,8 @@ RSpec.describe Spree::Admin::ProductsController, type: :controller do
backorderable: true
}
}
- }
+ },
+ shipping_category_id: shipping_category.id
}
end
@@ -377,9 +385,9 @@ RSpec.describe Spree::Admin::ProductsController, type: :controller do
end
context 'with product properties' do
- let!(:property_1) { create(:property, name: 'material', presentation: 'Material', kind: 'short_text') }
- let!(:property_2) { create(:property, name: 'short_description', presentation: 'Short description', kind: 'rich_text') }
- let!(:property_3) { create(:property, name: 'care', presentation: 'Care', kind: 'rich_text') }
+ let!(:property_1) { create(:property, name: 'material', presentation: 'Material') }
+ let!(:property_2) { create(:property, name: 'short_description', presentation: 'Short description') }
+ let!(:property_3) { create(:property, name: 'care', presentation: 'Care') }
let(:product_params) do
{
@@ -391,13 +399,14 @@ RSpec.describe Spree::Admin::ProductsController, type: :controller do
},
'1' => {
property_id: property_2.id,
- value: 'Short description with some <a href="https://example.com">link</a>'
+ value: 'Short description'
},
'2' => {
property_id: property_3.id,
value: ''
}
- }
+ },
+ shipping_category_id: shipping_category.id
}
end
@@ -407,7 +416,7 @@ RSpec.describe Spree::Admin::ProductsController, type: :controller do
product = Spree::Product.last
expect(product.properties.count).to eq 2
expect(product.property('material')).to eq 'Wool'
- expect(product.property('short_description')).to eq 'Short description with some <a href="https://example.com">link</a>'
+ expect(product.property('short_description')).to eq 'Short description'
end
end
@@ -417,7 +426,8 @@ RSpec.describe Spree::Admin::ProductsController, type: :controller do
let(:product_params) do
{
name: 'Product',
- store_ids: [store.id, store_2.id]
+ store_ids: [store.id, store_2.id],
+ shipping_category_id: shipping_category.id
}
end
@@ -432,7 +442,8 @@ RSpec.describe Spree::Admin::ProductsController, type: :controller do
let(:product_params) do
{
name: 'Product',
- store_ids: []
+ store_ids: [],
+ shipping_category_id: shipping_category.id
}
end
@@ -646,29 +657,6 @@ RSpec.describe Spree::Admin::ProductsController, type: :controller do
expect(variant2_stock_item.reload.count_on_hand).to eq 20
end
- context 'when product prices were imported' do
- before do
- variant1.prices.update_all(set_manually: false)
- variant2.prices.update_all(set_manually: false)
- end
-
- it 'updates prices and marks them as set manually' do
- send_request
-
- expect(variant1.price_in('PLN').amount).to eq 10
- expect(variant1.price_in('PLN')).to be_set_manually
-
- expect(variant1.price_in('USD').amount).to eq 20
- expect(variant1.price_in('USD')).to be_set_manually
-
- expect(variant2.price_in('PLN').amount).to eq 30
- expect(variant2.price_in('PLN')).to be_set_manually
-
- expect(variant2.price_in('USD').amount).to eq 40
- expect(variant2.price_in('USD')).to be_set_manually
- end
- end
-
context 'updating option types position' do
let(:product_params) do
{
@@ -809,19 +797,19 @@ RSpec.describe Spree::Admin::ProductsController, type: :controller do
expect(product.master.stock_items.reload.count).to be > 0
send_request
+
+ expect(product.reload.track_inventory).to be(false)
expect(product.master.stock_items.reload.count).to eq(0)
end
end
context 'with product properties' do
- let!(:property_1) { create(:property, name: 'material', presentation: 'Material', kind: 'short_text') }
- let!(:property_2) { create(:property, name: 'short_description', presentation: 'Short description', kind: 'rich_text') }
- let!(:property_3) { create(:property, name: 'care', presentation: 'Care', kind: 'rich_text') }
+ let!(:property_1) { create(:property, name: 'material', presentation: 'Material') }
+ let!(:property_2) { create(:property, name: 'short_description', presentation: 'Short description') }
+ let!(:property_3) { create(:property, name: 'care', presentation: 'Care') }
let(:product_property_1) { create(:product_property, product: product, property: property_1, value: 'Wool') }
- let(:product_property_2) do
- create(:product_property, product: product, property: property_2, value: 'Short description with some <a href="https://example.com">link</a>')
- end
+ let(:product_property_2) { create(:product_property, product: product, property: property_2, value: 'Short description') }
let(:product_params) do
{
diff --git a/core/app/models/acts_as_taggable_on/tag_decorator.rb b/core/app/models/acts_as_taggable_on/tag_decorator.rb
new file mode 100644
index 0000000000..44d81456ea
--- /dev/null
+++ b/core/app/models/acts_as_taggable_on/tag_decorator.rb
@@ -0,0 +1,10 @@
+module ActsAsTaggableOn
+ module TagDecorator
+ def self.prepended(base)
+ base.include ::Spree::RansackableAttributes
+ base.whitelisted_ransackable_attributes = %w[id name]
+ end
+ end
+
+ Tag.prepend(TagDecorator)
+end
diff --git a/core/app/models/spree/base.rb b/core/app/models/spree/base.rb
index 0492cdec57..a861f4ec56 100644
--- a/core/app/models/spree/base.rb
+++ b/core/app/models/spree/base.rb
@@ -23,6 +23,10 @@ class Spree::Base < ApplicationRecord
self.abstract_class = true
+ scope :for_ordering_with_translations, lambda { |klass, fields = nil|
+ select("#{klass.table_name}.*").select(*(fields || klass::TRANSLATABLE_FIELDS))
+ }
+
def self.belongs_to_required_by_default
false
end
diff --git a/core/app/models/spree/product.rb b/core/app/models/spree/product.rb
index 3f0857c630..aa059ff163 100644
--- a/core/app/models/spree/product.rb
+++ b/core/app/models/spree/product.rb
@@ -208,13 +208,45 @@ module Spree
return none if query.blank?
return none if query.length < 3
- Spree::Variant.search_by_product_name_or_sku(query)
+ product_ids = Spree::Variant.search_by_product_name_or_sku(query).pluck(:product_id)
+ where(id: product_ids.uniq.compact)
}
end
+ scope :archivable, -> { where(status: %w[active draft]) }
+ scope :by_source, ->(source) { send(source) }
+ scope :paused, -> { where(status: 'paused') }
+ scope :published, -> { where(status: 'active') }
+ scope :in_stock_items, -> { joins(:variants).merge(Spree::Variant.in_stock_or_backorderable) }
+ scope :out_of_stock_items, lambda {
+ joins(variants_including_master: :stock_items).
+ where(spree_variants: { track_inventory: true }).
+ where.not(id: Spree::Variant.where(track_inventory: false).pluck(:product_id).uniq).
+ where(spree_stock_items: { backorderable: false }).
+ group(:id).
+ having("SUM(#{Spree::StockItem.table_name}.count_on_hand) <= 0")
+ }
+ scope :out_of_stock, -> { joins(:stock_items).where("#{Spree::StockItem.table_name}.count_on_hand <= ? OR #{Spree::Variant.table_name}.track_inventory = ?", 0, false) }
+ scope :single_variant, lambda {
+ where(variants_count: 0)
+ }
+
attr_accessor :option_values_hash
- accepts_nested_attributes_for :product_properties, allow_destroy: true, reject_if: ->(pp) { pp[:property_name].blank? }
+ accepts_nested_attributes_for :product_properties, allow_destroy: true, reject_if: ->(pp) { pp[:property_id].blank? || (pp[:id].blank? && pp[:value].blank?) }
+ accepts_nested_attributes_for(
+ :variants,
+ allow_destroy: true,
+ reject_if: lambda do |v|
+ v[:option_value_variants_attributes].blank? && v[:stock_items_attributes].blank? && v[:prices_attributes].blank?
+ end
+ )
+ accepts_nested_attributes_for :master, reject_if: :all_blank
+ accepts_nested_attributes_for(
+ :product_option_types,
+ allow_destroy: true,
+ reject_if: ->(pot) { pot[:option_type_id].blank? || pot[:position].blank? }
+ )
alias options product_option_types
diff --git a/core/app/models/spree/variant.rb b/core/app/models/spree/variant.rb
index 8f316d7a9a..b77f285bab 100644
--- a/core/app/models/spree/variant.rb
+++ b/core/app/models/spree/variant.rb
@@ -148,6 +148,24 @@ module Spree
end
end
+ accepts_nested_attributes_for(
+ :stock_items,
+ reject_if: ->(attributes) { attributes['stock_location_id'].blank? || attributes['count_on_hand'].blank? },
+ allow_destroy: false
+ )
+
+ accepts_nested_attributes_for(
+ :prices,
+ reject_if: ->(attributes) { attributes['currency'].blank? || attributes['amount'].blank? },
+ allow_destroy: false
+ )
+
+ accepts_nested_attributes_for(
+ :option_value_variants,
+ reject_if: ->(attributes) { attributes['option_value_id'].blank? },
+ allow_destroy: false
+ )
+
self.whitelisted_ransackable_associations = %w[option_values product tax_category prices default_price]
self.whitelisted_ransackable_attributes = %w[weight depth width height sku discontinue_on is_master cost_price cost_currency track_inventory deleted_at]
self.whitelisted_ransackable_scopes = %i(product_name_or_sku_cont search_by_product_name_or_sku)
@@ -172,6 +190,14 @@ module Spree
product_name_or_sku_cont(query)
end
+ def human_name
+ @human_name ||= option_values.
+ joins(option_type: :product_option_types).
+ merge(product.product_option_types).
+ reorder('spree_product_option_types.position').
+ pluck(:presentation).join('/')
+ end
+
def available?
!discontinued? && product.available?
end
diff --git a/core/app/services/spree/products/prepare_nested_attributes.rb b/core/app/services/spree/products/prepare_nested_attributes.rb
index 5c0bda7c4a..cb884af43c 100644
--- a/core/app/services/spree/products/prepare_nested_attributes.rb
+++ b/core/app/services/spree/products/prepare_nested_attributes.rb
@@ -125,7 +125,7 @@ module Spree
existing_option_value_variant = existing_variant&.option_value_variants&.find { |ovv| ovv.option_value_id == option_value.id }
- option_value_variant_params[index] = { id: existing_option_value_variant&.id, option_value_id: option_value.id }.compact_blank
+ option_value_variant_params[index.to_s] = { id: existing_option_value_variant&.id, option_value_id: option_value.id }.compact_blank
next if product_option_types_params.find { |_i, v| v[:option_type_id] == option_type.id }