Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Admin Scaffold Generator v2 (#13488)
Damian Legawiecd5c115aaf562
diff --git a/admin/lib/generators/spree/admin/scaffold/scaffold_generator.rb b/admin/lib/generators/spree/admin/scaffold/scaffold_generator.rbindex 9f43904a99..47ade508ba 100644--- a/admin/lib/generators/spree/admin/scaffold/scaffold_generator.rb+++ b/admin/lib/generators/spree/admin/scaffold/scaffold_generator.rb@@ -31,11 +31,12 @@ module Spreetemplate 'views/edit.html.erb', "app/views/spree/admin/#{@plural_name}/edit.html.erb"# partials- template 'views/_table_header.html.erb', "app/views/spree/admin/#{@plural_name}/_table_header.html.erb"- template 'views/_table_row.html.erb', "app/views/spree/admin/#{@plural_name}/_table_row.html.erb"- template 'views/_filters.html.erb', "app/views/spree/admin/#{@plural_name}/_filters.html.erb"template 'views/_form.html.erb', "app/views/spree/admin/#{@plural_name}/_form.html.erb"+ # initializers+ template 'table_initializer.rb', "config/initializers/spree_admin_#{@plural_name}_table.rb"+ template 'navigation_initializer.rb', "config/initializers/spree_admin_#{@plural_name}_navigation.rb"+# routesinsert_into_file 'config/routes.rb', after: "Spree::Core::Engine.add_routes do\n" do<<-ROUTES.strip_heredoc.indent!(4)diff --git a/admin/lib/generators/spree/admin/scaffold/templates/navigation_initializer.rb.tt b/admin/lib/generators/spree/admin/scaffold/templates/navigation_initializer.rb.ttnew file mode 100644index 0000000000..70deb45c0f--- /dev/null+++ b/admin/lib/generators/spree/admin/scaffold/templates/navigation_initializer.rb.tt@@ -0,0 +1,9 @@+Rails.application.config.after_initialize do+ Spree.admin.navigation.sidebar.add :<%= @plural_name %>,+ label: :<%= @plural_name %>,+ url: :admin_<%= @plural_name %>_path,+ icon: 'list',+ position: 55,+ active: -> { controller_name == '<%= @plural_name %>' },+ if: -> { can?(:manage, <%= @resource_name %>) }+enddiff --git a/admin/lib/generators/spree/admin/scaffold/templates/table_initializer.rb.tt b/admin/lib/generators/spree/admin/scaffold/templates/table_initializer.rb.ttnew file mode 100644index 0000000000..751b8f4700--- /dev/null+++ b/admin/lib/generators/spree/admin/scaffold/templates/table_initializer.rb.tt@@ -0,0 +1,27 @@+Rails.application.config.after_initialize do+ Spree.admin.tables.register(:<%= @plural_name %>, model_class: <%= @resource_name %>, search_param: :name_cont)++ Spree.admin.tables.<%= @plural_name %>.add :name,+ label: :name,+ type: :link,+ sortable: true,+ filterable: true,+ default: true,+ position: 10++ Spree.admin.tables.<%= @plural_name %>.add :created_at,+ label: :created_at,+ type: :datetime,+ sortable: true,+ filterable: true,+ default: true,+ position: 20++ Spree.admin.tables.<%= @plural_name %>.add :updated_at,+ label: :updated_at,+ type: :datetime,+ sortable: true,+ filterable: true,+ default: false,+ position: 30+enddiff --git a/admin/lib/generators/spree/admin/scaffold/templates/views/_filters.html.erb.tt b/admin/lib/generators/spree/admin/scaffold/templates/views/_filters.html.erb.ttdeleted file mode 100644index c0046fc470..0000000000--- a/admin/lib/generators/spree/admin/scaffold/templates/views/_filters.html.erb.tt+++ /dev/null@@ -1,13 +0,0 @@-<%%= search_form_for [:admin, @search], class: "filter-wrap", data: { controller: "filters dropdown" } do |f| %>- <div class="flex flex-column flex-lg-row gap-2">- <%%= render 'spree/admin/shared/filters_search_bar', param: :name_i_cont, label: Spree.t(:name) %>- <%%= render 'spree/admin/shared/filters_button' %>- </div>- <div data-dropdown-target="menu" id="table-filter" class="hidden">- <!-- Add additional filters here -->- <%%= render 'spree/admin/shared/filter_submit' %>- </div>-- <%%= render "spree/admin/shared/filter_badge_template" %>- <div data-filters-target="badgesContainer" class="filter-badges-container"></div>-<%% end %>diff --git a/admin/lib/generators/spree/admin/scaffold/templates/views/_table_header.html.erb.tt b/admin/lib/generators/spree/admin/scaffold/templates/views/_table_header.html.erb.ttdeleted file mode 100644index 014a0b4e50..0000000000--- a/admin/lib/generators/spree/admin/scaffold/templates/views/_table_header.html.erb.tt+++ /dev/null@@ -1,5 +0,0 @@-<tr>- <th scope="col"><%%= sort_link @search, :name, Spree.t(:name) %></th>- <th scope="col"><%%= sort_link @search, :created_at, Spree.t(:created_at) %></th>- <th scope="col"></th>-</tr>\ No newline at end of filediff --git a/admin/lib/generators/spree/admin/scaffold/templates/views/_table_row.html.erb.tt b/admin/lib/generators/spree/admin/scaffold/templates/views/_table_row.html.erb.ttdeleted file mode 100644index b3b0e18932..0000000000--- a/admin/lib/generators/spree/admin/scaffold/templates/views/_table_row.html.erb.tt+++ /dev/null@@ -1,11 +0,0 @@-<tr id="<%%= spree_dom_id <%= @singular_name %> %>" class="cursor-pointer" data-controller="row-link">- <td data-action="click->row-link#openLink" class="w-70">- <%%= <%= @singular_name %>.name %>- </td>- <td data-action="click->row-link#openLink" class="w-20">- <%%= spree_date(<%= @singular_name %>.created_at) %>- </td>- <td class="actions w-10">- <%%= link_to_edit(<%= @singular_name %>, data: { row_link_target: :link, turbo_frame: '_top' }) if can? :edit, <%= @singular_name %> %>- </td>-</tr>\ No newline at end of filediff --git a/admin/lib/generators/spree/admin/scaffold/templates/views/index.html.erb.tt b/admin/lib/generators/spree/admin/scaffold/templates/views/index.html.erb.ttindex 914e595d74..bedd5ed803 100644--- a/admin/lib/generators/spree/admin/scaffold/templates/views/index.html.erb.tt+++ b/admin/lib/generators/spree/admin/scaffold/templates/views/index.html.erb.tt@@ -6,4 +6,4 @@<%%= link_to_with_icon 'plus', Spree.t(:new_<%= @singular_name %>), new_object_url, class: "btn btn-primary" if can? :create, <%= @resource_name %> %><%% end %>-<%%= render 'spree/admin/shared/index_table' %>\ No newline at end of file+<%%= render_table @collection, :<%= @plural_name %> %>\ No newline at end of filediff --git a/docs/developer/tutorial/admin.mdx b/docs/developer/tutorial/admin.mdxindex 13e9f9e63e..91eff6c747 100644--- a/docs/developer/tutorial/admin.mdx+++ b/docs/developer/tutorial/admin.mdx@@ -18,15 +18,14 @@ bin/rails g spree:admin:scaffold Spree::BrandThis will create the following files:| File Type | Path | Description |-|-----------|------|------|+|-----------|------|-------------|| Controller | `app/controllers/spree/admin/brands_controller.rb` | Handles the logic for the brands resource. |-| View | `app/views/spree/admin/brands/index.html.erb` | Handles displaying the list of brands in the Admin Dashboard. |-| View | `app/views/spree/admin/brands/new.html.erb` | Handles displaying the new brand form in the Admin Dashboard. |-| View | `app/views/spree/admin/brands/edit.html.erb` | Handles displaying the edit brand form in the Admin Dashboard. |-| Partial | `app/views/spree/admin/brands/_table_header.html.erb` | Handles displaying the table header in the brands index view. |-| Partial | `app/views/spree/admin/brands/_table_row.html.erb` | Handles displaying the table row in the brands index view. |-| Partial | `app/views/spree/admin/brands/_filters.html.erb` | Handles displaying the filters in the brands index view. |-| Partial | `app/views/spree/admin/brands/_form.html.erb` | Handles displaying the form in the new and edit brand views. |+| View | `app/views/spree/admin/brands/index.html.erb` | Displays the list of brands using the new tables system. |+| View | `app/views/spree/admin/brands/new.html.erb` | Displays the new brand form. |+| View | `app/views/spree/admin/brands/edit.html.erb` | Displays the edit brand form. |+| Partial | `app/views/spree/admin/brands/_form.html.erb` | The form partial used in new and edit views. |+| Initializer | `config/initializers/spree_admin_brands_table.rb` | Registers the table with columns for the index view. |+| Initializer | `config/initializers/spree_admin_brands_navigation.rb` | Adds navigation to the admin sidebar. |It will also automatically add the following routes to your `config/routes.rb` file:@@ -38,39 +37,91 @@ endYou will now be able to access the brands resource at `http://localhost:3000/admin/brands` in your browser. To reference this route in your code, you can use the `spree.admin_brands_path` helper.-## Step 2: Add Navigation Menu Item+## Step 2: Customize the Table Columns-Now that we've created a complete Admin Dashboard UI for the brand resource, let's add a navigation menu item to the main sidebar so we can access it from the admin dashboard.+The scaffold generator creates a table initializer at `config/initializers/spree_admin_brands_table.rb` with default columns:-Add the following to your `config/initializers/spree.rb` file:--```ruby config/initializers/spree.rb+```ruby config/initializers/spree_admin_brands_table.rbRails.application.config.after_initialize do- # Add Brands to the admin sidebar- sidebar_nav = Spree.admin.navigation.sidebar+ Spree.admin.tables.register(:brands, model_class: Spree::Brand, search_param: :name_cont)++ Spree.admin.tables.brands.add :name,+ label: :name,+ type: :link,+ sortable: true,+ filterable: true,+ default: true,+ position: 10++ Spree.admin.tables.brands.add :created_at,+ label: :created_at,+ type: :datetime,+ sortable: true,+ filterable: true,+ default: true,+ position: 20++ Spree.admin.tables.brands.add :updated_at,+ label: :updated_at,+ type: :datetime,+ sortable: true,+ filterable: true,+ default: false,+ position: 30+end+```++You can customize this file to add more columns based on your model's attributes. For example, to add a description column:- sidebar_nav.add :brands,+```ruby+Spree.admin.tables.brands.add :description,+ label: :description,+ type: :string,+ sortable: false,+ filterable: true,+ default: true,+ position: 15+```++<Info>+For complete table customization options including column types, filtering, sorting, and bulk actions, see the [Admin Tables](/developer/admin/tables) guide.+</Info>++## Step 3: Customize Navigation++The scaffold generator also creates a navigation initializer at `config/initializers/spree_admin_brands_navigation.rb`:++```ruby config/initializers/spree_admin_brands_navigation.rb+Rails.application.config.after_initialize do+ Spree.admin.navigation.sidebar.add :brands,label: :brands,url: :admin_brands_path,- icon: 'award',- position: 35,+ icon: 'list',+ position: 55,active: -> { controller_name == 'brands' },if: -> { can?(:manage, Spree::Brand) }end```-This will add a "Brands" link to the admin sidebar between Products (30) and Customers (40).+You can customize the icon and position to fit your needs. For example, to use the "award" icon and place it between Products (30) and Customers (40):++```ruby+Spree.admin.navigation.sidebar.add :brands,+ label: :brands,+ url: :admin_brands_path,+ icon: 'award',+ position: 35,+ active: -> { controller_name == 'brands' },+ if: -> { can?(:manage, Spree::Brand) }+```### Adding to an Existing Submenu-To add "Brands" to the Products submenu instead add a new submenu item:+To add "Brands" to the Products submenu instead, use the `parent` option:-```ruby config/initializers/spree.rb+```ruby config/initializers/spree_admin_brands_navigation.rbRails.application.config.after_initialize do- sidebar_nav = Spree.admin.navigation.sidebar-- # Add brands as a child of products- sidebar_nav.add :brands,+ Spree.admin.navigation.sidebar.add :brands,label: :brands,url: :admin_brands_path,position: 50,