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

[Admin] Add Taxonomies index component #5526

Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<%= page do %>
<%= page_header do %>
<%= page_header_title title %>
<%= page_header_actions do %>
<%= render component("ui/button").new(
tag: :a,
text: t('.create_taxonomy'),
href: spree.new_admin_taxonomy_path,
icon: "add-line",
) %>
<% end %>
<% end %>

<%= render component('ui/table').new(
id: 'taxonomies-list',
data: {
class: Spree::Taxonomy,
rows: @taxonomies,
url: ->(taxonomy) { spree.edit_admin_taxonomy_path(taxonomy) },
columns: columns,
batch_actions: batch_actions,
},
sortable: {
url: ->(taxonomy) { solidus_admin.move_taxonomy_path(taxonomy) },
param: 'position',
},
) %>
<% end %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# frozen_string_literal: true

class SolidusAdmin::Taxonomies::Index::Component < SolidusAdmin::BaseComponent
include SolidusAdmin::Layout::PageHelpers

def initialize(taxonomies:)
@taxonomies = taxonomies
end

def title
Spree::Taxonomy.model_name.human.pluralize
end

def columns
[
name_column,
]
end

def name_column
{
header: :name,
data: ->(taxonomy) do
content_tag :div, taxonomy.name
end
}
end

def batch_actions
[
{
display_name: t('.batch_actions.delete'),
action: solidus_admin.taxonomies_path,
method: :delete,
icon: 'delete-bin-7-line',
},
]
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
en:
batch_actions:
delete: 'Delete'
create_taxonomy: Create Taxonomy
39 changes: 39 additions & 0 deletions admin/app/controllers/solidus_admin/taxonomies_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# frozen_string_literal: true

module SolidusAdmin
class TaxonomiesController < SolidusAdmin::BaseController
before_action :load_taxonomy, only: [:move]

def index
@taxonomies = Spree::Taxonomy.all

respond_to do |format|
format.html { render component('taxonomies/index').new(taxonomies: @taxonomies) }
end
end

def move
@taxonomy.insert_at(params[:position].to_i)

respond_to do |format|
format.js { head :no_content }
end
end

def destroy
@taxonomies = Spree::Taxonomy.where(id: params[:id])

Spree::Taxonomy.transaction { @taxonomies.destroy_all }

flash[:notice] = t('.success')
redirect_back_or_to taxonomies_path, status: :see_other
end

private

def load_taxonomy
@taxonomy = Spree::Taxonomy.find(params[:id])
authorize! action_name, @taxonomy
end
end
end
6 changes: 6 additions & 0 deletions admin/config/locales/taxonomies.en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
en:
solidus_admin:
taxonomies:
title: "Taxonomies"
destroy:
success: "Taxonomies were successfully removed."
9 changes: 9 additions & 0 deletions admin/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@
end
end

resources :taxonomies, only: [:index] do
collection do
delete :destroy
end
member do
patch :move
end
end

resources :promotion_categories, only: [:index] do
collection do
delete :destroy
Expand Down
24 changes: 24 additions & 0 deletions admin/spec/features/taxonomies_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

require 'spec_helper'

describe "Taxonomies", :js, type: :feature do
before { sign_in create(:admin_user, email: '[email protected]') }

it "lists taxonomies and allows deleting them" do
create(:taxonomy, name: "Categories")
create(:taxonomy, name: "Brand")

visit "/admin/taxonomies"
expect(page).to have_content("Categories")
expect(page).to have_content("Brand")

expect(page).to be_axe_clean

select_row("Categories")
click_on "Delete"
expect(page).to have_content("Taxonomies were successfully removed.")
expect(page).not_to have_content("Categories")
expect(Spree::Taxonomy.count).to eq(1)
end
end
Loading