Skip to content

Commit

Permalink
Add option_types/index component with dedicated index action
Browse files Browse the repository at this point in the history
  • Loading branch information
rainerdema committed Nov 29, 2023
1 parent a3243ad commit b1f0db1
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<div class="px-4">
<header class="py-6 flex items-center">
<h1 class="body-title">
<%= title %>
</h1>

<div class="ml-auto flex gap-2 items-center">
<%= render component("ui/button").new(
tag: :a,
text: t('.create_option_type'),
href: spree.new_admin_option_type_path,
icon: "add-line",
) %>
</div>
</header>

<%= 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',
},
) %>
</div>
47 changes: 47 additions & 0 deletions admin/app/components/solidus_admin/option_types/index/component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# frozen_string_literal: true

class SolidusAdmin::OptionTypes::Index::Component < SolidusAdmin::BaseComponent
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
en:
batch_actions:
delete: 'Delete'
create_option_type: Create Option Type
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ 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)

Expand Down
2 changes: 1 addition & 1 deletion admin/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
end
end

resources :option_types do
resources :option_types, only: [:index] do
collection do
delete :destroy
end
Expand Down
24 changes: 24 additions & 0 deletions admin/spec/features/option_types_spec.rb
Original file line number Diff line number Diff line change
@@ -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: '[email protected]') }

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

0 comments on commit b1f0db1

Please sign in to comment.