Skip to content

Commit

Permalink
Merge pull request #5527 from nebulab/rainerd/admin/add-propery-types…
Browse files Browse the repository at this point in the history
…-index

 [Admin] Add `Properties` index component
  • Loading branch information
rainerdema committed Nov 29, 2023
2 parents 0bbb702 + 0b4af87 commit dba880d
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<%= page do %>
<%= page_header do %>
<%= page_header_title title %>
<%= page_header_actions do %>
<%= render component("ui/button").new(
tag: :a,
text: t('.add_property'),
href: spree.new_admin_property_path,
icon: "add-line",
) %>
<% end %>
<% end %>
<%= render component('ui/table').new(
id: 'property-list',
data: {
class: Spree::Property,
rows: @page.records,
url: ->(property) { solidus_admin.properties_path(property) },
prev: prev_page_path,
next: next_page_path,
columns: columns,
batch_actions: batch_actions,
},
search: {
name: :q,
value: params[:q],
url: solidus_admin.properties_path,
searchbar_key: :name_cont,
},
) %>
<% end %>
57 changes: 57 additions & 0 deletions admin/app/components/solidus_admin/properties/index/component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# frozen_string_literal: true

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

def initialize(page:)
@page = page
end

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

def prev_page_path
solidus_admin.url_for(**request.params, page: @page.number - 1, only_path: true) unless @page.first?
end

def next_page_path
solidus_admin.url_for(**request.params, page: @page.next_param, only_path: true) unless @page.last?
end

def batch_actions
[
{
display_name: t('.batch_actions.delete'),
action: solidus_admin.properties_path,
method: :delete,
icon: 'delete-bin-7-line',
},
]
end

def columns
[
name_column,
presentation_column,
]
end

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

def presentation_column
{
header: :presentation,
data: ->(property) do
content_tag :div, property.presentation
end
}
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
en:
add_property: 'Add Property'
batch_actions:
delete: 'Delete'
33 changes: 33 additions & 0 deletions admin/app/controllers/solidus_admin/properties_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# frozen_string_literal: true

module SolidusAdmin
class PropertiesController < SolidusAdmin::BaseController
include SolidusAdmin::ControllerHelpers::Search

def index
properties = apply_search_to(
Spree::Property.order(created_at: :desc, id: :desc),
param: :q,
)

set_page_and_extract_portion_from(
properties,
)

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

def destroy
@properties = Spree::Property.where(id: params[:id])

Spree::Property.transaction do
@properties.discard_all
end

flash[:notice] = t('.success')
redirect_to properties_path, status: :see_other
end
end
end
6 changes: 6 additions & 0 deletions admin/config/locales/properties.en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
en:
solidus_admin:
properties:
title: "Properties"
destroy:
success: "Properties were successfully removed."
6 changes: 6 additions & 0 deletions admin/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,10 @@
delete :destroy
end
end

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

require 'spec_helper'

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

it "lists properties and allows deleting them" do
create(:property, name: "Type", presentation: "Type")
create(:property, name: "Size", presentation: "Size")

visit "/admin/properties"
expect(page).to have_content("Type")
expect(page).to have_content("Size")

expect(page).to be_axe_clean

select_row("Type")
click_on "Delete"
expect(page).to have_content("Properties were successfully removed.")
expect(page).not_to have_content("Type")
expect(Spree::Property.count).to eq(1)
end
end

0 comments on commit dba880d

Please sign in to comment.