diff --git a/admin/app/components/solidus_admin/option_types/index/component.html.erb b/admin/app/components/solidus_admin/option_types/index/component.html.erb new file mode 100644 index 00000000000..b7f24c6b50c --- /dev/null +++ b/admin/app/components/solidus_admin/option_types/index/component.html.erb @@ -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_option_type'), + href: spree.new_admin_option_type_path, + icon: "add-line", + ) %> + <% end %> + <% end %> + + <%= render component('ui/table').new( + id: 'option-types-list', + data: { + class: Spree::OptionType, + rows: @option_types, + url: ->(option_type) { spree.edit_admin_option_type_path(option_type) }, + columns: columns, + batch_actions: batch_actions, + }, + sortable: { + url: ->(option_type) { solidus_admin.move_option_type_path(option_type) }, + param: 'position', + }, + ) %> +<% end %> diff --git a/admin/app/components/solidus_admin/option_types/index/component.rb b/admin/app/components/solidus_admin/option_types/index/component.rb new file mode 100644 index 00000000000..b46f909edc6 --- /dev/null +++ b/admin/app/components/solidus_admin/option_types/index/component.rb @@ -0,0 +1,49 @@ +# frozen_string_literal: true + +class SolidusAdmin::OptionTypes::Index::Component < SolidusAdmin::BaseComponent + include SolidusAdmin::Layout::PageHelpers + + def initialize(option_types:) + @option_types = option_types + end + + def title + Spree::OptionType.model_name.human.pluralize + end + + def columns + [ + name_column, + presentation_column, + ] + end + + def name_column + { + header: :name, + data: ->(option_type) do + content_tag :div, option_type.name + end + } + end + + def presentation_column + { + header: :presentation, + data: ->(option_type) do + content_tag :div, option_type.presentation + end + } + end + + def batch_actions + [ + { + display_name: t('.batch_actions.delete'), + action: solidus_admin.option_types_path, + method: :delete, + icon: 'delete-bin-7-line', + }, + ] + end +end diff --git a/admin/app/components/solidus_admin/option_types/index/component.yml b/admin/app/components/solidus_admin/option_types/index/component.yml new file mode 100644 index 00000000000..4d61e58803a --- /dev/null +++ b/admin/app/components/solidus_admin/option_types/index/component.yml @@ -0,0 +1,4 @@ +en: + batch_actions: + delete: 'Delete' + create_option_type: Create Option Type diff --git a/admin/app/controllers/solidus_admin/option_types_controller.rb b/admin/app/controllers/solidus_admin/option_types_controller.rb new file mode 100644 index 00000000000..338a65d003d --- /dev/null +++ b/admin/app/controllers/solidus_admin/option_types_controller.rb @@ -0,0 +1,39 @@ +# frozen_string_literal: true + +module SolidusAdmin + class OptionTypesController < SolidusAdmin::BaseController + before_action :load_option_type, only: [:move] + + def index + @option_types = Spree::OptionType.all + + respond_to do |format| + format.html { render component('option_types/index').new(option_types: @option_types) } + end + end + + def move + @option_type.insert_at(params[:position].to_i) + + respond_to do |format| + format.js { head :no_content } + end + end + + def destroy + @option_types = Spree::OptionType.where(id: params[:id]) + + Spree::OptionType.transaction { @option_types.destroy_all } + + flash[:notice] = t('.success') + redirect_back_or_to option_types_path, status: :see_other + end + + private + + def load_option_type + @option_type = Spree::OptionType.find(params[:id]) + authorize! action_name, @option_type + end + end +end diff --git a/admin/config/locales/option_types.en.yml b/admin/config/locales/option_types.en.yml new file mode 100644 index 00000000000..fb439e8d07b --- /dev/null +++ b/admin/config/locales/option_types.en.yml @@ -0,0 +1,6 @@ +en: + solidus_admin: + option_types: + title: "Option Types" + destroy: + success: "Option Types were successfully removed." diff --git a/admin/config/routes.rb b/admin/config/routes.rb index 9cd0b6d6822..bbe9af8f286 100644 --- a/admin/config/routes.rb +++ b/admin/config/routes.rb @@ -48,4 +48,13 @@ delete :destroy end end + + resources :option_types, only: [:index] do + collection do + delete :destroy + end + member do + patch :move + end + end end diff --git a/admin/spec/features/option_types_spec.rb b/admin/spec/features/option_types_spec.rb new file mode 100644 index 00000000000..10181e7d009 --- /dev/null +++ b/admin/spec/features/option_types_spec.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe "Option Types", :js, type: :feature do + before { sign_in create(:admin_user, email: 'admin@example.com') } + + it "lists option types and allows deleting them" do + create(:option_type, name: "color", presentation: "Color") + create(:option_type, name: "size", presentation: "Size") + + visit "/admin/option_types" + expect(page).to have_content("color") + expect(page).to have_content("size") + + expect(page).to be_axe_clean + + select_row("color") + click_on "Delete" + expect(page).to have_content("Option Types were successfully removed.") + expect(page).not_to have_content("color") + expect(Spree::OptionType.count).to eq(1) + end +end