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
Add menu API.
Matthew Kennedy7c484424ae56
diff --git a/api/app/controllers/spree/api/v2/platform/menu_items_controller.rb b/api/app/controllers/spree/api/v2/platform/menu_items_controller.rbindex 2fcaf81720..c2445d3541 100644--- a/api/app/controllers/spree/api/v2/platform/menu_items_controller.rb+++ b/api/app/controllers/spree/api/v2/platform/menu_items_controller.rb@@ -4,11 +4,11 @@ module Spreemodule Platformclass MenuItemsController < ResourceControllerdef reposition- @moved_item = Spree::MenuItem.find(params[:moved_item_id])+ @moved_item = scope.find(params[:moved_item_id])@new_parent = if params[:new_parent_id].nil?nilelse- Spree::MenuItem.find(params[:new_parent_id])+ scope.find(params[:new_parent_id])end@moved_item.move_with_index(params[:new_position_idx].to_i, @new_parent)diff --git a/api/app/controllers/spree/api/v2/storefront/menus_controller.rb b/api/app/controllers/spree/api/v2/storefront/menus_controller.rbnew file mode 100644index 0000000000..6e5d4e751f--- /dev/null+++ b/api/app/controllers/spree/api/v2/storefront/menus_controller.rb@@ -0,0 +1,55 @@+module Spree+ module Api+ module V2+ module Storefront+ class MenusController < ::Spree::Api::V2::ResourceController+ private++ def serialize_collection(collection)+ collection_serializer.new(+ collection,+ collection_options(collection).merge(params: collection_serializer_params)+ ).serializable_hash+ end++ def collection_serializer_params+ {+ currency: current_currency,+ store: current_store,+ user: spree_current_user,+ }+ end++ def serializer_params+ {+ currency: current_currency,+ store: current_store,+ user: spree_current_user,+ include_menu_items: true,+ }+ end++ def resource+ scope.find_by(unique_code: params[:unique_code])+ end++ def resource_serializer+ Spree::Api::Dependencies.storefront_menu_serializer.constantize+ end++ def collection_serializer+ Spree::Api::Dependencies.storefront_menu_serializer.constantize+ end++ def collection_finder+ Spree::Api::Dependencies.storefront_menu_finder.constantize+ end++ def model_class+ Spree::Menu+ end+ end+ end+ end+ end+enddiff --git a/api/app/helpers/spree/api/api_helpers.rb b/api/app/helpers/spree/api/api_helpers.rbindex c3d384ca62..8ace7d1107 100644--- a/api/app/helpers/spree/api/api_helpers.rb+++ b/api/app/helpers/spree/api/api_helpers.rb@@ -9,6 +9,7 @@ module Spree:option_value_attributes,:order_attributes,:line_item_attributes,+ :menu_attributes,:option_type_attributes,:payment_attributes,:payment_method_attributes,@@ -85,6 +86,8 @@ module Spree@@line_item_attributes = [:id, :quantity, :price, :variant_id]+ @@menu_attributes = [:id, :name, :unique_code]+@@option_type_attributes = [:id, :name, :presentation, :position]@@payment_attributes = [diff --git a/api/app/models/spree/api_dependencies.rb b/api/app/models/spree/api_dependencies.rbindex e8d3c220e0..5d57e85d9f 100644--- a/api/app/models/spree/api_dependencies.rb+++ b/api/app/models/spree/api_dependencies.rb@@ -10,7 +10,7 @@ module Spree:storefront_checkout_remove_store_credit_service, :storefront_checkout_get_shipping_rates_service,:storefront_cart_compare_line_items_service, :storefront_cart_serializer, :storefront_credit_card_serializer,:storefront_credit_card_finder, :storefront_shipment_serializer, :storefront_payment_method_serializer, :storefront_country_finder,- :storefront_country_serializer, :storefront_current_order_finder, :storefront_completed_order_finder, :storefront_order_sorter,+ :storefront_country_serializer, :storefront_menu_serializer, :storefront_menu_finder, :storefront_current_order_finder, :storefront_completed_order_finder, :storefront_order_sorter,:storefront_collection_paginator, :storefront_user_serializer, :storefront_products_sorter, :storefront_products_finder,:storefront_product_serializer, :storefront_taxon_serializer, :storefront_taxon_finder, :storefront_find_by_variant_finder,:storefront_cart_update_service, :storefront_cart_estimate_shipping_rates_service, :storefront_estimated_shipment_serializer,@@ -60,6 +60,7 @@ module Spree@storefront_cart_serializer = 'Spree::V2::Storefront::CartSerializer'@storefront_credit_card_serializer = 'Spree::V2::Storefront::CreditCardSerializer'@storefront_country_serializer = 'Spree::V2::Storefront::CountrySerializer'+ @storefront_menu_serializer = 'Spree::V2::Storefront::MenuSerializer'@storefront_user_serializer = 'Spree::V2::Storefront::UserSerializer'@storefront_shipment_serializer = 'Spree::V2::Storefront::ShipmentSerializer'@storefront_taxon_serializer = 'Spree::V2::Storefront::TaxonSerializer'@@ -80,6 +81,7 @@ module Spree# finders@storefront_address_finder = Spree::Dependencies.address_finder@storefront_country_finder = Spree::Dependencies.country_finder+ @storefront_menu_finder = Spree::Dependencies.menu_finder@storefront_current_order_finder = Spree::Dependencies.current_order_finder@storefront_completed_order_finder = Spree::Dependencies.completed_order_finder@storefront_credit_card_finder = Spree::Dependencies.credit_card_finderdiff --git a/api/app/serializers/spree/v2/storefront/menu_item_serializer.rb b/api/app/serializers/spree/v2/storefront/menu_item_serializer.rbnew file mode 100644index 0000000000..a03dd61c93--- /dev/null+++ b/api/app/serializers/spree/v2/storefront/menu_item_serializer.rb@@ -0,0 +1,31 @@+module Spree+ module V2+ module Storefront+ class MenuItemSerializer < BaseSerializer+ set_type :menu_item++ attributes :name, :subtitle, :destination, :code, :new_window, :parent_id, :lft, :rgt, :depth++ attribute :is_root do |menu_item|+ menu_item.root?+ end++ attribute :is_child do |menu_item|+ menu_item.child?+ end++ attribute :is_leaf do |menu_item|+ menu_item.leaf?+ end++ has_one :image,+ object_method_name: :image_asset,+ id_method_name: :image_asset,+ record_type: :image,+ serializer: :image++ belongs_to :menu+ end+ end+ end+enddiff --git a/api/app/serializers/spree/v2/storefront/menu_serializer.rb b/api/app/serializers/spree/v2/storefront/menu_serializer.rbnew file mode 100644index 0000000000..8b55d9dd8c--- /dev/null+++ b/api/app/serializers/spree/v2/storefront/menu_serializer.rb@@ -0,0 +1,13 @@+module Spree+ module V2+ module Storefront+ class MenuSerializer < BaseSerializer+ set_type :menu++ attributes :name, :unique_code++ has_many :menu_items, if: proc { |_record, params| params && params[:include_menu_items] }+ end+ end+ end+enddiff --git a/api/config/routes.rb b/api/config/routes.rbindex 4de3fa3980..cfcaef2e47 100644--- a/api/config/routes.rb+++ b/api/config/routes.rb@@ -163,6 +163,9 @@ Spree::Core::Engine.add_routes doresources :products, only: %i[index show]resources :taxons, only: %i[index show], id: /.+/get '/stores/:code', to: 'stores#show', as: :store++ resources :menus, only: %i[index]+ get '/menus/:unique_code', to: 'menus#show', as: :menuendnamespace :platform dodiff --git a/core/app/finders/spree/menus/find.rb b/core/app/finders/spree/menus/find.rbnew file mode 100644index 0000000000..2090312ea8--- /dev/null+++ b/core/app/finders/spree/menus/find.rb@@ -0,0 +1,11 @@+module Spree+ module Menus+ class Find < ::Spree::BaseFinder+ def execute+ return scope.where(unique_code: params[:filter]['unique_code']) if params[:filter].present?++ scope+ end+ end+ end+enddiff --git a/core/app/models/spree/ability.rb b/core/app/models/spree/ability.rbindex 2d2d614781..f2d68da8af 100644--- a/core/app/models/spree/ability.rb+++ b/core/app/models/spree/ability.rb@@ -61,6 +61,7 @@ module Spreedef apply_user_permissions(user)can :read, ::Spree::Country+ can :read, ::Spree::Menucan :read, ::Spree::OptionTypecan :read, ::Spree::OptionValuecan :create, ::Spree::Orderdiff --git a/core/app/models/spree/app_dependencies.rb b/core/app/models/spree/app_dependencies.rbindex 9db4ef3d6f..0eac27e09d 100644--- a/core/app/models/spree/app_dependencies.rb+++ b/core/app/models/spree/app_dependencies.rb@@ -9,7 +9,7 @@ module Spree:cart_update_service, :checkout_next_service, :checkout_advance_service, :checkout_update_service,:checkout_complete_service, :checkout_add_store_credit_service,:checkout_remove_store_credit_service, :checkout_get_shipping_rates_service,- :coupon_handler, :country_finder, :current_order_finder, :credit_card_finder,+ :coupon_handler, :menu_finder, :country_finder, :current_order_finder, :credit_card_finder,:completed_order_finder, :order_sorter, :cart_compare_line_items_service, :collection_paginator, :products_sorter,:products_finder, :taxon_finder, :line_item_by_variant_finder, :cart_estimate_shipping_rates_service,:account_create_address_service, :account_update_address_service, :address_finder,@@ -73,6 +73,7 @@ module Spreedef set_default_finders@address_finder = 'Spree::Addresses::Find'@country_finder = 'Spree::Countries::Find'+ @menu_finder = 'Spree::Menus::Find'@current_order_finder = 'Spree::Orders::FindCurrent'@completed_order_finder = 'Spree::Orders::FindComplete'@credit_card_finder = 'Spree::CreditCards::Find'diff --git a/core/lib/spree/permitted_attributes.rb b/core/lib/spree/permitted_attributes.rbindex b6afcd1141..7ddda8b448 100644--- a/core/lib/spree/permitted_attributes.rb+++ b/core/lib/spree/permitted_attributes.rb@@ -7,6 +7,7 @@ module Spree:image_attributes,:inventory_unit_attributes,:line_item_attributes,+ :menu_attributes,:menu_item_attributes,:option_type_attributes,:option_value_attributes,@@ -52,9 +53,9 @@ module Spree@@line_item_attributes = [:id, :variant_id, :quantity]- @@menu_attributes = [:name, stores: []]+ @@menu_attributes = [:id, :name, :unique_code]- @@menu_item_attributes = [:name, :url, :image_asset]+ @@menu_item_attributes = [:id, :name, :destination, :image_asset, :menu_id]@@option_type_attributes = [:name, :presentation, :option_values_attributes]