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 Stores index component #5537

Merged
merged 2 commits into from
Dec 6, 2023
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,34 @@
<%= page do %>
<%= page_header do %>
<%= page_header_title title %>
<%= page_header_actions do %>
<%= render component("ui/button").new(
tag: :a,
text: t('.add'),
href: spree.new_admin_store_path,
icon: "add-line",
) %>
<% end %>
<% end %>

<%= render component('ui/table').new(
id: stimulus_id,
data: {
class: Spree::Store,
rows: @page.records,
url: ->(store) { spree.edit_admin_store_path(store) },
prev: prev_page_path,
next: next_page_path,
columns: columns,
batch_actions: batch_actions,
},
search: {
name: :q,
value: params[:q],
url: solidus_admin.stores_path,
searchbar_key: :name_or_url_or_code_cont,
filters: filters,
scopes: scopes,
},
) %>
<% end %>
59 changes: 59 additions & 0 deletions admin/app/components/solidus_admin/stores/index/component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# frozen_string_literal: true

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

def initialize(page:)
@page = page
end

def title
Spree::Store.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.stores_path,
method: :delete,
icon: 'delete-bin-7-line',
},
]
end

def filters
[]
end

def scopes
[]
end

def columns
[
:name,
:url,
{
header: :slug,
data: ->(store) do
content_tag :div, store.code
end
},
{
header: :default,
data: ->(store) do
store.default? ? component('ui/badge').yes : component('ui/badge').no
end
},
]
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
en:
add: 'Add new'
batch_actions:
delete: 'Delete'
40 changes: 40 additions & 0 deletions admin/app/controllers/solidus_admin/stores_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# frozen_string_literal: true

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

def index
stores = apply_search_to(
Spree::Store.order(id: :desc),
param: :q
)

set_page_and_extract_portion_from(stores)

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

def destroy
@stores = Spree::Store.where(id: params[:id])

Spree::Store.transaction { @stores.destroy_all }

flash[:notice] = t('.success')
redirect_back_or_to stores_path, status: :see_other
end

private

def load_store
@store = Spree::Store.find_by!(number: params[:id])
authorize! action_name, @store
end

def store_params
params.require(:store).permit(:store_id, permitted_store_attributes)
end
end
end
6 changes: 6 additions & 0 deletions admin/config/locales/stores.en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
en:
solidus_admin:
stores:
title: "Stores"
destroy:
success: "Stores were successfully removed."
1 change: 1 addition & 0 deletions admin/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,5 @@
admin_resources :shipping_methods, only: [:index, :destroy]
admin_resources :shipping_categories, only: [:index, :destroy]
admin_resources :stock_locations, only: [:index, :destroy]
admin_resources :stores, only: [:index, :destroy]
end
24 changes: 24 additions & 0 deletions admin/spec/features/stores_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

require 'spec_helper'

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

it "lists stores and allows deleting them" do
create(:store, name: "B2C Store")
create(:store, name: "B2B Store")

visit "/admin/stores"
expect(page).to have_content("B2C Store")
expect(page).to have_content("B2B Store")
expect(page).to be_axe_clean

select_row("B2C Store")
click_on "Delete"
expect(page).to have_content("Stores were successfully removed.")
expect(page).not_to have_content("B2C Store")
expect(Spree::Store.count).to eq(1)
expect(page).to be_axe_clean
end
end
2 changes: 2 additions & 0 deletions core/app/models/spree/store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class Store < Spree::Base
validates :url, presence: true
validates :mail_from_address, presence: true

self.allowed_ransackable_attributes = %w[name url code]

before_save :ensure_default_exists_and_is_unique
before_destroy :validate_not_default

Expand Down
Loading