Skip to content

Commit f35f325

Browse files
committed
Admin form support for primary taxon
Updated the product form for primary taxon and modified the related jQuery to display and select a specific taxon.
1 parent 066850f commit f35f325

File tree

3 files changed

+60
-20
lines changed

3 files changed

+60
-20
lines changed

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

0 commit comments

Comments
 (0)