Skip to content

Commit 1b4e1f4

Browse files
authored
Merge pull request solidusio#6109 from gms-electronics/add-primary-breadcrumb-to-product
Add primary Taxon to products (solidusio#6047)
2 parents 34abf41 + f35f325 commit 1b4e1f4

File tree

10 files changed

+111
-22
lines changed

10 files changed

+111
-22
lines changed

api/lib/spree/api_configuration.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class ApiConfiguration < Preferences::Configuration
77
preference :product_attributes, :array, default: [
88
:id, :name, :description, :available_on,
99
:slug, :meta_description, :meta_keywords, :shipping_category_id,
10-
:taxon_ids, :total_on_hand, :meta_title
10+
:taxon_ids, :total_on_hand, :meta_title, :primary_taxon_id
1111
]
1212

1313
preference :product_property_attributes, :array, default: [:id, :product_id, :property_id, :value, :property_name]

api/spec/requests/spree/api/products_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,13 @@ module Spree::Api
316316
expect(json_response["taxon_ids"]).to eq([taxon_1.id])
317317
end
318318

319+
it "puts primary taxon for the product" do
320+
product_data[:primary_taxon_id] = taxon_1.id.to_s
321+
post spree.api_products_path, params: { product: product_data }
322+
323+
expect(json_response["primary_taxon_id"]).to eq(taxon_1.id)
324+
end
325+
319326
# Regression test for https://github.com/spree/spree/issues/4123
320327
it "puts the created product in the given taxons" do
321328
product_data[:taxon_ids] = [taxon_1.id, taxon_2.id].join(',')
@@ -404,6 +411,13 @@ module Spree::Api
404411
expect(json_response["taxon_ids"]).to eq([taxon_1.id])
405412
end
406413

414+
it "puts primary taxon for the updated product" do
415+
product.primary_taxon_id = taxon_2.id
416+
put spree.api_product_path(product), params: { product: { primary_taxon_id: taxon_1.id } }
417+
418+
expect(json_response["primary_taxon_id"]).to eq(taxon_1.id)
419+
end
420+
407421
# Regression test for https://github.com/spree/spree/issues/4123
408422
it "puts the created product in the given taxons" do
409423
put spree.api_product_path(product), params: { product: { taxon_ids: [taxon_1.id, taxon_2.id].join(',') } }

backend/app/assets/javascripts/spree/backend/taxon_autocomplete.js

Lines changed: 49 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,49 @@
1-
$.fn.taxonAutocomplete = function () {
1+
$.fn.taxonAutocomplete = function (options) {
22
'use strict';
33

4+
var defaultOptions = {
5+
multiple: true,
6+
placeholder: Spree.translations.taxon_placeholder
7+
};
8+
9+
options = $.extend({}, defaultOptions, options);
10+
411
this.select2({
5-
placeholder: Spree.translations.taxon_placeholder,
6-
multiple: true,
12+
placeholder: options.placeholder,
13+
multiple: options.multiple,
714
initSelection: function (element, callback) {
8-
var ids = element.val(),
9-
count = ids.split(",").length;
10-
11-
Spree.ajax({
12-
type: "GET",
13-
url: Spree.pathFor('api/taxons'),
14-
data: {
15-
ids: ids,
16-
per_page: count,
17-
without_children: true
18-
},
19-
success: function (data) {
20-
callback(data['taxons']);
21-
}
22-
});
15+
var ids = element.val();
16+
17+
if (options.multiple) {
18+
var count = ids.split(",").length;
19+
20+
Spree.ajax({
21+
type: "GET",
22+
url: Spree.pathFor('api/taxons'),
23+
data: {
24+
ids: ids,
25+
per_page: count,
26+
without_children: true
27+
},
28+
success: function (data) {
29+
callback(data['taxons']);
30+
}
31+
});
32+
} else {
33+
34+
Spree.ajax({
35+
type: "GET",
36+
url: Spree.pathFor('api/taxons'),
37+
data: {
38+
ids: ids,
39+
per_page: 1,
40+
without_children: true
41+
},
42+
success: function (data) {
43+
callback(data['taxons'][0]);
44+
}
45+
});
46+
}
2347
},
2448
ajax: {
2549
url: Spree.pathFor('api/taxons'),
@@ -53,5 +77,11 @@ $.fn.taxonAutocomplete = function () {
5377
};
5478

5579
Spree.ready(function () {
56-
$('#product_taxon_ids, .taxon_picker').taxonAutocomplete();
80+
$('#product_taxon_ids, .taxon_picker').taxonAutocomplete({
81+
multiple: true,
82+
});
83+
84+
$('#product_primary_taxon_id').taxonAutocomplete({
85+
multiple: false,
86+
});
5787
});

backend/app/views/spree/admin/products/_form.html.erb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@
3636
<% end %>
3737
</div>
3838

39+
<div data-hook="admin_product_form_primary_taxons">
40+
<%= f.field_container :primary_taxon do %>
41+
<%= f.label :primary_taxon_id %><br>
42+
<%= f.hidden_field :primary_taxon_id, value: @product.primary_taxon_id %>
43+
<% end %>
44+
</div>
45+
3946
<div data-hook="admin_product_form_option_types">
4047
<%= f.field_container :option_types do %>
4148
<%= f.label :option_type_ids, plural_resource_name(Spree::OptionType) %>

backend/spec/features/admin/products/edit/taxons_spec.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ def assert_selected_taxons(taxons)
2626
visit spree.edit_admin_product_path(product)
2727

2828
assert_selected_taxons([taxon_1])
29-
select2_search "Clothing", from: "Taxon"
29+
within("[data-hook='admin_product_form_taxons']") do
30+
select2_search "Clothing", from: "Taxon"
31+
end
32+
3033
assert_selected_taxons([taxon_1, taxon_2])
3134

3235
# Without this line we have a flaky spec probably due to select2 not

core/app/models/spree/product.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class Product < Spree::Base
3232

3333
belongs_to :tax_category, class_name: 'Spree::TaxCategory', optional: true
3434
belongs_to :shipping_category, class_name: 'Spree::ShippingCategory', inverse_of: :products, optional: true
35+
belongs_to :primary_taxon, class_name: 'Spree::Taxon', optional: true
3536

3637
has_one :master,
3738
-> { where(is_master: true).with_discarded },

core/config/locales/en.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,8 @@ en:
179179
name: Name
180180
on_hand: On Hand
181181
price: Master Price
182+
primary_taxon: Primary Taxon
183+
primary_taxon_id: Primary Taxon
182184
promotionable: Promotable
183185
shipping_category: Shipping Category
184186
sku: Master SKU
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class AddPrimaryTaxonToProducts < ActiveRecord::Migration[7.0]
2+
def change
3+
change_table :spree_products do |t|
4+
t.references :primary_taxon, { to_table: :spree_taxons }
5+
end
6+
end
7+
end

core/lib/spree/permitted_attributes.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ module PermittedAttributes
7878
:meta_keywords, :price, :sku, :deleted_at,
7979
:option_values_hash, :weight, :height, :width, :depth,
8080
:shipping_category_id, :tax_category_id,
81-
:taxon_ids, :option_type_ids, :cost_currency, :cost_price
81+
:taxon_ids, :option_type_ids, :cost_currency, :cost_price, :primary_taxon_id
8282
]
8383

8484
@@property_attributes = [:name, :presentation]

core/spec/models/spree/product_spec.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,22 @@ class Extension < Spree::Base
452452
end
453453
end
454454
end
455+
456+
describe "primary_taxon" do
457+
it 'should belong to primary_taxon' do
458+
expect(Spree::Product.reflect_on_association(:primary_taxon).macro).to eq(:belongs_to)
459+
end
460+
461+
it 'should be optional' do
462+
association = Spree::Product.reflect_on_association(:primary_taxon)
463+
expect(association.options[:optional]).to be(true)
464+
end
465+
466+
it 'should have a class_name of Spree::Taxon' do
467+
association = Spree::Product.reflect_on_association(:primary_taxon)
468+
expect(association.class_name).to eq('Spree::Taxon')
469+
end
470+
end
455471
end
456472
end
457473

@@ -709,4 +725,13 @@ class Extension < Spree::Base
709725
end
710726
end
711727
end
728+
729+
it 'is valid with or without a primary_taxon' do
730+
product_with_taxon = create(:product, primary_taxon: create(:taxon))
731+
732+
product_without_taxon = create(:product, primary_taxon: nil)
733+
734+
expect(product_with_taxon).to be_valid
735+
expect(product_without_taxon).to be_valid
736+
end
712737
end

0 commit comments

Comments
 (0)