Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unified Handling of Option Values and Product Properties List #3592

Merged
merged 6 commits into from
Jun 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 17 additions & 28 deletions backend/app/assets/javascripts/spree/backend/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ Spree.ready(function(){
$('.spree_add_fields').click(function() {
var target = $(this).data("target");
var new_table_row = $(target + ' tr:visible:last').clone();

// remove id
new_table_row.attr("id", "");

// Remove sort handle
new_table_row.find("td").first().empty();

// set unique form ids and names of new item
var new_id = new Date().getTime() + (uniqueId++);
new_table_row.find("input, select").each(function () {
var el = $(this);
Expand All @@ -66,14 +74,11 @@ Spree.ready(function(){
el.prop("id", el.prop("id").replace(/\d+(?=[^\d]*$)/, new_id))
el.prop("name", el.prop("name").replace(/\d+(?=[^\d]*$)/, new_id))
})
// When cloning a new row, set the href of all icons to be an empty "#"
// This is so that clicking on them does not perform the actions for the
// duplicated row
new_table_row.find("a").each(function () {
var el = $(this);
el.prop('href', '#');
})
$(target).prepend(new_table_row);

// Add a remove button to the actions column of the new row
new_table_row.find("td").last().empty().append('<a class="spree_remove_fields no-text with-tip fa fa-trash icon_link with-tip" data-action="remove" href="#" data-original-title="Remove"><span class="text"></span></a>');

$(target).append(new_table_row);
})

$('body').on('click', '.delete-resource', function() {
Expand Down Expand Up @@ -101,26 +106,10 @@ Spree.ready(function(){

$('body').on('click', 'a.spree_remove_fields', function() {
hefan marked this conversation as resolved.
Show resolved Hide resolved
var el = $(this);
el.prev("input[type=hidden]").val("1");
el.closest(".fields").hide();
if (el.prop("href").substr(-1) == '#') {
el.parents("tr").fadeOut('hide');
}else if (el.prop("href")) {
Spree.ajax({
type: 'POST',
url: el.prop("href"),
data: {
_method: 'delete',
},
success: function(response) {
el.parents("tr").fadeOut('hide');
},
error: function(response, textStatus, errorThrown) {
show_flash('error', response.responseText);
}

})
}
var table_row = el.parents("tr").first();
table_row.fadeOut("hide", function() {
table_row.remove();
});
return false;
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def find_properties
end

def setup_property
@product.product_properties.build
@product.product_properties.build if @product.product_properties.empty?
end

def setup_variant_property_rules
Expand Down
2 changes: 2 additions & 0 deletions backend/app/helpers/spree/admin/base_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ def link_to_remove_fields(name, form, options = {})
link_to_with_icon('trash', name, url, class: "spree_remove_fields #{options[:class]}", data: { action: 'remove' }, title: t('spree.actions.remove')) +
form.hidden_field(:_destroy)
end
deprecate link_to_remove_fields: "Please use link_to_delete instead, Example: \n" \
"link_to_delete \"form.object\"", deprecator: Spree::Deprecation

def spree_dom_id(record)
dom_id(record, 'spree')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,12 @@
</td>
<td class="name"><%= f.text_field :name %></td>
<td class="presentation"><%= f.text_field :presentation %></td>
<td class="actions"><%= link_to_remove_fields t('spree.actions.remove'), f, no_text: true %></td>
<td class="actions">
<% if f.object.persisted? %>
<%= link_to_delete f.object, no_text: true %>
<% else %>
<%= link_to_with_icon('trash', 'remove', '#', no_text: true, class: "spree_remove_fields",
data: { action: 'remove' }, title: t('spree.actions.remove')) %>
<% end %>
aldesantis marked this conversation as resolved.
Show resolved Hide resolved
</td>
</tr>
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@
<%= f.text_field :value %>
</td>
<td class="actions">
<% if f.object.persisted? && can?(:destroy, f.object) %>
hefan marked this conversation as resolved.
Show resolved Hide resolved
<%= link_to_delete f.object, no_text: true %>
<% if f.object.persisted? %>
<% if can?(:destroy, f.object) %>
<%= link_to_delete f.object, no_text: true %>
<% end %>
<% else %>
<%= link_to_with_icon('trash', 'remove', '#', no_text: true, class: "spree_remove_fields",
data: { action: 'remove' }, title: t('spree.actions.remove')) %>
<% end %>
</td>
</tr>
4 changes: 3 additions & 1 deletion backend/spec/features/admin/products/option_types_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@
expect(page).to have_title("#{option_value.option_type.name} - Option Types - Products")
expect(page).to have_css("tbody#option_values tr", count: 1)
within("tbody#option_values") do
find('.spree_remove_fields').click
accept_alert do
find('.fa-trash').click
end
end
# Assert that the field is hidden automatically
expect(page).to have_no_css("tbody#option_values tr")
Expand Down
18 changes: 10 additions & 8 deletions backend/spec/features/admin/products/properties_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,23 +88,25 @@
it "successfully create and then remove product property" do
fill_in_property

check_property_row_count(2)
check_persisted_property_row_count(1)

delete_product_property

check_property_row_count(1)
check_persisted_property_row_count(0)
end

# Regression test for https://github.com/spree/spree/issues/4466
it "successfully remove and create a product property at the same time" do
fill_in_property

expect(page).to have_css('tr.product_property', count: 2)
expect(page).to have_css('tr.product_property', count: 1)

within '#spree_new_product_property' do
click_button "Add Product Properties"
within '.product_property:first-child' do
find('[id$=_property_name]').set("New Property")
find('[id$=_value]').set("New Value")
end
expect(page).to have_css('tr.product_property', count: 2)

delete_product_property

Expand All @@ -115,7 +117,7 @@

expect(page).not_to have_content("Product is not found")

check_property_row_count(2)
check_persisted_property_row_count(0)
end

def fill_in_property
Expand All @@ -128,14 +130,14 @@ def fill_in_property

def delete_product_property
accept_alert do
click_icon :trash
within_row(1) { click_icon :trash }
end
expect(page).to have_content 'successfully removed'
end

def check_property_row_count(expected_row_count)
def check_persisted_property_row_count(expected_row_count)
click_link "Product Properties"
expect(page).to have_css("tbody#product_properties tr", count: expected_row_count)
expect(page).to have_css("tbody#product_properties tr:not(#spree_new_product_property)", count: expected_row_count)
end
end
end