Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

[SD-1065] Add remove icon button to admin edit taxon page (#10646)

Robert Nowakowskia6461ddd87a0
diff --git a/backend/app/controllers/spree/admin/taxons_controller.rb b/backend/app/controllers/spree/admin/taxons_controller.rb
index e83c55d83b..7c92f47351 100644
--- a/backend/app/controllers/spree/admin/taxons_controller.rb
+++ b/backend/app/controllers/spree/admin/taxons_controller.rb
@@ -1,8 +1,8 @@
module Spree
module Admin
class TaxonsController < Spree::Admin::BaseController
- before_action :load_taxonomy, only: [:create, :edit, :update]
- before_action :load_taxon, only: [:edit, :update]
+ before_action :load_taxonomy, only: [:create, :edit, :update, :remove_icon]
+ before_action :load_taxon, only: [:edit, :update, :remove_icon]
before_action :set_permalink_part, only: [:edit, :update]
respond_to :html, :js
@@ -62,6 +62,16 @@ module Spree
end
end
+ def remove_icon
+ if @taxon.icon.destroy
+ flash[:success] = Spree.t('notice_messages.icon_removed')
+ redirect_to edit_admin_taxonomy_taxon_url(@taxonomy.id, @taxon.id)
+ else
+ flash[:error] = Spree.t('errors.messages.cannot_remove_icon')
+ render :edit
+ end
+ end
+
private
def set_permalink_part
diff --git a/backend/app/views/spree/admin/taxons/_form.html.erb b/backend/app/views/spree/admin/taxons/_form.html.erb
index 5cbeb77479..d9b25ba049 100644
--- a/backend/app/views/spree/admin/taxons/_form.html.erb
+++ b/backend/app/views/spree/admin/taxons/_form.html.erb
@@ -28,6 +28,12 @@
<%= f.label :icon, Spree.t(:header_banner) %>
<%= image_tag(main_app.url_for(@taxon.icon.try(:attachment)), class: 'w-100') if @taxon.icon %>
<%= f.file_field :icon %>
+ <% if @taxon.icon.present? %>
+ <%= link_to Spree.t(:remove_image),
+ remove_icon_admin_taxonomy_taxon_url(@taxonomy.id, @taxon.id),
+ method: :delete
+ %>
+ <% end %>
<% end %>
</div>
diff --git a/backend/config/routes.rb b/backend/config/routes.rb
index cbc1346e84..928fa9a8c0 100644
--- a/backend/config/routes.rb
+++ b/backend/config/routes.rb
@@ -136,7 +136,11 @@ Spree::Core::Engine.add_routes do
collection do
post :update_positions
end
- resources :taxons
+ resources :taxons do
+ member do
+ delete :remove_icon
+ end
+ end
end
resources :taxons, only: [:index, :show]
diff --git a/backend/spec/controllers/spree/admin/taxons_controller_spec.rb b/backend/spec/controllers/spree/admin/taxons_controller_spec.rb
new file mode 100644
index 0000000000..b3882b39a3
--- /dev/null
+++ b/backend/spec/controllers/spree/admin/taxons_controller_spec.rb
@@ -0,0 +1,28 @@
+require 'spec_helper'
+
+describe Spree::Admin::TaxonsController, type: :controller do
+ stub_authorization!
+
+ describe '#remove_icon' do
+ let!(:taxonomy) { create(:taxonomy) }
+ let!(:taxon) { create(:taxon, taxonomy: taxonomy) }
+ let!(:taxon_image) { Spree::TaxonImage.new }
+ let!(:image_file) { File.open(Spree::Core::Engine.root + 'spec/fixtures' + 'thinking-cat.jpg') }
+
+ before do
+ taxon_image.attachment.attach(io: image_file, filename: 'thinking-cat.jpg', content_type: 'image/jpeg')
+ taxon.icon = taxon_image
+ taxon.save!
+ end
+
+ it 'removes taxon icon' do
+ expect(taxon.icon).to eq(taxon_image)
+
+ allow(taxon).to receive :remove_icon
+ post :remove_icon, params: { taxonomy_id: taxonomy.id, id: taxon.id }
+ taxon.reload
+
+ expect(taxon.icon).to eq(nil)
+ end
+ end
+end
diff --git a/backend/spec/features/admin/taxons_spec.rb b/backend/spec/features/admin/taxons_spec.rb
index 429557d304..39c4cd94d0 100644
--- a/backend/spec/features/admin/taxons_spec.rb
+++ b/backend/spec/features/admin/taxons_spec.rb
@@ -1,9 +1,10 @@
require 'spec_helper'
-describe 'Taxonomies and taxons', type: :feature do
+describe 'Taxonomies and taxons', type: :feature, js: true do
stub_authorization!
let(:taxonomy) { create(:taxonomy, name: 'Hello') }
+ let(:file_path) { Rails.root + '../../spec/support/ror_ringer.jpeg' }
it 'admin should be able to edit taxon' do
visit spree.edit_admin_taxonomy_taxon_path(taxonomy, taxonomy.root.id)
@@ -48,9 +49,38 @@ describe 'Taxonomies and taxons', type: :feature do
expect(page).to have_content('No results')
end
+ it 'admin should be able to add taxon icon' do
+ visit spree.edit_admin_taxonomy_taxon_path(taxonomy, taxonomy.root.id)
+
+ attach_file('taxon_icon', file_path)
+ click_button 'Update'
+
+ expect(page).to have_content('successfully updated!')
+
+ visit spree.edit_admin_taxonomy_taxon_path(taxonomy, taxonomy.root.id)
+
+ expect(page).to have_css('#taxon_icon_field img')
+ end
+
+ it 'admin should be able to remove taxon icon' do
+ add_icon_to_root_taxon
+
+ visit spree.edit_admin_taxonomy_taxon_path(taxonomy, taxonomy.root.id)
+
+ click_link 'Remove Image'
+
+ expect(page).to have_content('Image has been successfully removed')
+ end
+
def select_clothing_from_select2
select2_open css: '.taxon-products-view'
select2_search 'Clothing', css: '.taxon-products-view'
select2_select Spree::Product.first.taxons.first&.pretty_name, css: '.taxon-products-view'
end
+
+ def add_icon_to_root_taxon
+ visit spree.edit_admin_taxonomy_taxon_path(taxonomy, taxonomy.root.id)
+ attach_file('taxon_icon', file_path)
+ click_button 'Update'
+ end
end
diff --git a/core/config/locales/en.yml b/core/config/locales/en.yml
index e03110cd68..de8f6f0ad9 100644
--- a/core/config/locales/en.yml
+++ b/core/config/locales/en.yml
@@ -851,6 +851,7 @@ en:
blank: can't be blank
could_not_create_taxon: Could not create taxon
no_shipping_methods_available: No shipping methods available for selected location, please change your address and try again.
+ cannot_remove_icon: Cannot remove image
services:
get_shipping_rates:
no_shipping_address: To generate Shipping Rates Order needs to have a Shipping Address
@@ -1154,6 +1155,7 @@ en:
not_found: ! '%{resource} is not found'
note: Note
notice_messages:
+ icon_removed: Image has been successfully removed
prices_saved: Prices successfully saved
product_cloned: Product has been cloned
product_deleted: Product has been deleted
@@ -1438,6 +1440,7 @@ en:
rejected: Rejected
remember_me: Remember me
remove: Remove
+ remove_image: Remove Image
rename: Rename
report: Report
reports: Reports