From f33df883a060669298dd6be8069a57513fa679c1 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Fri, 10 Jun 2016 11:24:23 +1000 Subject: [PATCH] Fix link_to_remove_fields - does not immediately delete the field --- app/assets/javascripts/admin/util.js.erb | 16 ++++++++++++---- app/controllers/admin/enterprises_controller.rb | 7 ++++--- app/helpers/spree/admin/base_helper_decorator.rb | 16 ---------------- spec/features/admin/enterprises_spec.rb | 13 +++++++------ 4 files changed, 23 insertions(+), 29 deletions(-) diff --git a/app/assets/javascripts/admin/util.js.erb b/app/assets/javascripts/admin/util.js.erb index bc963d4612..4a8c526414 100644 --- a/app/assets/javascripts/admin/util.js.erb +++ b/app/assets/javascripts/admin/util.js.erb @@ -30,8 +30,16 @@ show_flash_error = function(message) { } $(document).ready(function(){ - $('a.close').click(function(event){ - event.preventDefault(); - $(this).parent().slideUp(250); - }); + $('a.close').click(function(event){ + event.preventDefault(); + $(this).parent().slideUp(250); + }); + + // Spree locates hidden with prev(), which with our current version of jQuery + // does not locate the hidden field, resulting in the delete failing. This + // handler updates the hidden field, fixing the problem. + $('body').on('click', 'a.remove_fields', function() { + $(this).next("input[type=hidden]").val("1"); + return false; + }); }); diff --git a/app/controllers/admin/enterprises_controller.rb b/app/controllers/admin/enterprises_controller.rb index 8b7efa9e1f..f2c9552c4a 100644 --- a/app/controllers/admin/enterprises_controller.rb +++ b/app/controllers/admin/enterprises_controller.rb @@ -271,9 +271,10 @@ module Admin # Overriding method on Spree's resource controller def location_after_save referer_path = OpenFoodNetwork::RefererParser::path(request.referer) - refered_from_edit = referer_path =~ /\/edit$/ - if params[:enterprise].key?(:producer_properties_attributes) && !refered_from_edit - main_app.admin_enterprises_path + refered_from_producer_properties = referer_path =~ /\/producer_properties$/ + + if refered_from_producer_properties + main_app.admin_enterprise_producer_properties_path(@enterprise) else main_app.edit_admin_enterprise_path(@enterprise) end diff --git a/app/helpers/spree/admin/base_helper_decorator.rb b/app/helpers/spree/admin/base_helper_decorator.rb index 86e77431ba..ff9e2b76a7 100644 --- a/app/helpers/spree/admin/base_helper_decorator.rb +++ b/app/helpers/spree/admin/base_helper_decorator.rb @@ -1,22 +1,6 @@ module Spree module Admin module BaseHelper - # Add url option to pass in link URL - def link_to_remove_fields(name, f, options = {}) - name = '' if options[:no_text] - options[:class] = '' unless options[:class] - options[:class] += 'no-text' if options[:no_text] - - url = if f.object.persisted? - options[:url] || [:admin, f.object] - else - '#' - end - - link_to_with_icon('icon-trash', name, url, :class => "remove_fields #{options[:class]}", :data => {:action => 'remove'}, :title => t(:remove)) + f.hidden_field(:_destroy) - end - - def preference_field_tag_with_files(name, value, options) if options[:type] == :file file_field_tag name, preference_field_options(options) diff --git a/spec/features/admin/enterprises_spec.rb b/spec/features/admin/enterprises_spec.rb index 865ce15b57..8b5a08a7cc 100644 --- a/spec/features/admin/enterprises_spec.rb +++ b/spec/features/admin/enterprises_spec.rb @@ -210,8 +210,8 @@ feature %q{ fill_in 'enterprise_producer_properties_attributes_0_value', with: "NASAA 12345" click_button 'Update' - # Then I should be returned to the enterprises page - page.should have_selector '#listing_enterprises a', text: s.name + # Then I should remain on the producer properties page + expect(current_path).to eq main_app.admin_enterprise_producer_properties_path(s) # And the producer should have the property s.producer_properties(true).count.should == 1 @@ -233,8 +233,8 @@ feature %q{ fill_in 'enterprise_producer_properties_attributes_0_value', with: "Shininess" click_button 'Update' - # Then I should be returned to the enterprises - page.should have_selector '#listing_enterprises a', text: s.name + # Then I should remain on the producer properties page + expect(current_path).to eq main_app.admin_enterprise_producer_properties_path(s) # And the property should be updated s.producer_properties(true).count.should == 1 @@ -254,9 +254,10 @@ feature %q{ # And I remove the property page.should have_field 'enterprise_producer_properties_attributes_0_property_name', with: 'Certified Organic' within("#spree_producer_property_#{pp.id}") { page.find('a.remove_fields').click } + click_button 'Update' # Then the property should have been removed - page.should_not have_selector '#progress' + expect(current_path).to eq main_app.admin_enterprise_producer_properties_path(s) page.should_not have_field 'enterprise_producer_properties_attributes_0_property_name', with: 'Certified Organic' s.producer_properties(true).should be_empty end @@ -438,7 +439,7 @@ feature %q{ end within("#spree_producer_property_#{pp.id}") { page.find('a.remove_fields').click } - page.should_not have_selector '#progress' + click_button 'Update' supplier1.producer_properties(true).should be_empty end end