diff --git a/app/assets/javascripts/imageable.js b/app/assets/javascripts/imageable.js index c2ee3e882..a6e658796 100644 --- a/app/assets/javascripts/imageable.js +++ b/app/assets/javascripts/imageable.js @@ -8,6 +8,9 @@ $("#nested-image").on("cocoon:after-remove", function() { $("#new_image_link").removeClass("hide"); }); + $("#nested-image").on("cocoon:before-insert", function() { + $(".js-image-attachment").closest(".image").remove(); + }); $("#nested-image").on("cocoon:after-insert", function(e, nested_image) { var input; $("#new_image_link").addClass("hide"); diff --git a/app/assets/stylesheets/mixins/uploads.scss b/app/assets/stylesheets/mixins/uploads.scss index 570b65326..692ec9b05 100644 --- a/app/assets/stylesheets/mixins/uploads.scss +++ b/app/assets/stylesheets/mixins/uploads.scss @@ -28,8 +28,7 @@ .attachment-errors { - > .js-image-attachment, - > .js-document-attachment { + > [type=file] { @include element-invisible; ~ .error { diff --git a/spec/shared/system/nested_imageable.rb b/spec/shared/system/nested_imageable.rb index 1560db273..9de787867 100644 --- a/spec/shared/system/nested_imageable.rb +++ b/spec/shared/system/nested_imageable.rb @@ -207,38 +207,36 @@ shared_examples "nested imageable" do |imageable_factory_name, path, imageable_p end if path.include? "edit" - scenario "Should show persisted image" do + scenario "show persisted image" do create(:image, imageable: imageable) do_login_for user + visit send(path, arguments) expect(page).to have_css ".image", count: 1 - end - - scenario "Should not show add image button when image already exists" do - create(:image, imageable: imageable) - do_login_for user - visit send(path, arguments) - expect(page).not_to have_css "a#new_image_link" end - scenario "Should remove nested field after remove image" do + scenario "remove nested field after removing the image" do create(:image, imageable: imageable) do_login_for user + visit send(path, arguments) - click_on "Remove image" + click_link "Remove image" expect(page).not_to have_css ".image" + expect(page).to have_css "a#new_image_link" end - scenario "Should show add image button after remove image" do + scenario "don't duplicate fields after removing and adding an image" do create(:image, imageable: imageable) do_login_for user - visit send(path, arguments) - click_on "Remove image" - expect(page).to have_css "a#new_image_link" + visit send(path, arguments) + click_link "Remove image" + click_link "Add image" + + expect(page).to have_css ".image", count: 1, visible: :all end end end