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 Properties index component #5527

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,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 %>
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
Loading