-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
properties/index
component with dedicated controller actions
- Loading branch information
1 parent
9609689
commit 0b4af87
Showing
7 changed files
with
162 additions
and
0 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
admin/app/components/solidus_admin/properties/index/component.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
57
admin/app/components/solidus_admin/properties/index/component.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
4 changes: 4 additions & 0 deletions
4
admin/app/components/solidus_admin/properties/index/component.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
33
admin/app/controllers/solidus_admin/properties_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,4 +42,10 @@ | |
delete :destroy | ||
end | ||
end | ||
|
||
resources :properties, only: [:index] do | ||
collection do | ||
delete :destroy | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |