Skip to content

armgit / spree

Public Imported from GitHub
0

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.

Convert to belons_to relationship.

Matthew Kennedyc3e70532038c
diff --git a/api/app/serializers/spree/api/v2/platform/menu_item_serializer.rb b/api/app/serializers/spree/api/v2/platform/menu_item_serializer.rb
index bb18f7836b..d2a820d479 100644
--- a/api/app/serializers/spree/api/v2/platform/menu_item_serializer.rb
+++ b/api/app/serializers/spree/api/v2/platform/menu_item_serializer.rb
@@ -5,6 +5,20 @@ module Spree
class MenuItemSerializer < BaseSerializer
include ::Spree::Api::V2::ResourceSerializerConcern
+ attributes :item_type, :code, :name, :subtitle, :destination, :new_window, :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: :menu_item_image,
id_method_name: :menu_item_image_id,
diff --git a/api/app/serializers/spree/api/v2/platform/menu_serializer.rb b/api/app/serializers/spree/api/v2/platform/menu_serializer.rb
index 2ec221508b..cfe3a87031 100644
--- a/api/app/serializers/spree/api/v2/platform/menu_serializer.rb
+++ b/api/app/serializers/spree/api/v2/platform/menu_serializer.rb
@@ -5,6 +5,9 @@ module Spree
class MenuSerializer < BaseSerializer
include ::Spree::Api::V2::ResourceSerializerConcern
+ attributes :name, :unique_code
+
+ belongs_to :store
has_many :menu_items
end
end
diff --git a/api/app/serializers/spree/api/v2/platform/store_serializer.rb b/api/app/serializers/spree/api/v2/platform/store_serializer.rb
new file mode 100644
index 0000000000..204a9c6e5d
--- /dev/null
+++ b/api/app/serializers/spree/api/v2/platform/store_serializer.rb
@@ -0,0 +1,18 @@
+module Spree
+ module Api
+ module V2
+ module Platform
+ class StoreSerializer < BaseSerializer
+ include ::Spree::Api::V2::ResourceSerializerConcern
+
+ attributes :name, :url, :meta_description, :meta_keywords, :seo_title, :default_currency, :default, :supported_currencies, :facebook,
+ :twitter, :instagram, :default_locale, :customer_support_email, :default_country_id, :description,
+ :address, :contact_phone, :supported_locales
+
+ has_many :menus
+ has_one :default_country, serializer: :country, record_type: :country, id_method_name: :default_country_id
+ end
+ end
+ end
+ end
+end
diff --git a/api/app/serializers/spree/v2/storefront/menu_serializer.rb b/api/app/serializers/spree/v2/storefront/menu_serializer.rb
index c2a8fc61b0..f747434278 100644
--- a/api/app/serializers/spree/v2/storefront/menu_serializer.rb
+++ b/api/app/serializers/spree/v2/storefront/menu_serializer.rb
@@ -6,6 +6,7 @@ module Spree
attributes :name, :unique_code
+ belongs_to :store
has_many :menu_items
end
end
diff --git a/api/app/serializers/spree/v2/storefront/store_serializer.rb b/api/app/serializers/spree/v2/storefront/store_serializer.rb
index 9fbcd42bee..8bd8683a89 100644
--- a/api/app/serializers/spree/v2/storefront/store_serializer.rb
+++ b/api/app/serializers/spree/v2/storefront/store_serializer.rb
@@ -8,8 +8,7 @@ module Spree
:twitter, :instagram, :default_locale, :customer_support_email, :default_country_id, :description,
:address, :contact_phone, :supported_locales
- has_many :menus, record_type: :menu
-
+ has_many :menus
has_one :default_country, serializer: :country, record_type: :country, id_method_name: :default_country_id
end
end
diff --git a/backend/app/views/spree/admin/menus/_form.html.erb b/backend/app/views/spree/admin/menus/_form.html.erb
index 8e025697c5..0130184277 100644
--- a/backend/app/views/spree/admin/menus/_form.html.erb
+++ b/backend/app/views/spree/admin/menus/_form.html.erb
@@ -16,7 +16,7 @@
<div class="col-12 col-md-6">
<div class="form-group">
<%= label_tag :menu_stores, Spree.t(:stores) %>
- <%= collection_select(:menu, :store_ids, @stores, :id, :unique_name, {}, { multiple: true, class: 'select2' }) %>
+ <%= collection_select(:menu, :store_id, @stores, :id, :unique_name, { include_blank: true }, { class: 'select2-clear' }) %>
<%= f.error_message_on :menu_stores %>
</div>
</div>
diff --git a/backend/app/views/spree/admin/menus/index.html.erb b/backend/app/views/spree/admin/menus/index.html.erb
index fb9afdcbd8..e51b4a4d27 100644
--- a/backend/app/views/spree/admin/menus/index.html.erb
+++ b/backend/app/views/spree/admin/menus/index.html.erb
@@ -24,8 +24,8 @@
</small>
</td>
<td>
- <% menu.store_ids.each do |store| %>
- <span class="badge badge-secondary"><%= @stores.find(store).unique_name %></span>
+ <% if menu.store_id %>
+ <span class="badge badge-secondary"><%= @stores.find(menu.store_id).unique_name %></span>
<% end %>
</td>
<td class="actions" data-hook="admin_menus_index_row_actions">
@@ -44,5 +44,3 @@
<%= Spree.t('admin.navigation.you_have_no_menus') %>
</div>
<% end %>
-
-
diff --git a/core/app/models/spree/menu.rb b/core/app/models/spree/menu.rb
index 93ec638a22..043db881c9 100644
--- a/core/app/models/spree/menu.rb
+++ b/core/app/models/spree/menu.rb
@@ -1,17 +1,16 @@
module Spree
class Menu < Spree::Base
has_many :menu_items, dependent: :destroy
- has_many :navigations, inverse_of: :menu
- has_many :stores, through: :navigations
+ belongs_to :store
before_validation :paremeterize_unique_code
after_create :set_root
- validate :uniqueness_of_code_within_stores
validates :name, presence: true
+ validates :unique_code, uniqueness: { scope: :store_id }
- scope :by_store, ->(store) { joins(:stores).where('store_id = ?', store) }
scope :by_unique_code, ->(menu_code) { where unique_code: menu_code }
+ scope :by_store, ->(store) { where store_id: store }
has_one :root, -> { where parent_id: nil }, class_name: 'Spree::MenuItem', dependent: :destroy
@@ -24,22 +23,5 @@ module Spree
def set_root
self.root ||= MenuItem.create!(menu_id: id, name: name, item_type: 'Container', code: "#{name}-root")
end
-
- def uniqueness_of_code_within_stores
- menus = []
-
- store_ids.map do |store_id|
- Menu.by_store(store_id).
- by_unique_code(unique_code).
- each { |match| menus << match.name unless id == match.id }
- end
-
- unless menus.empty?
- errors.add(:unique_code,
- Spree.t('admin.navigation.unique_code_store_error',
- code: unique_code,
- menus: menus[0]))
- end
- end
end
end
diff --git a/core/app/models/spree/navigation.rb b/core/app/models/spree/navigation.rb
deleted file mode 100644
index 4c53216578..0000000000
--- a/core/app/models/spree/navigation.rb
+++ /dev/null
@@ -1,8 +0,0 @@
-module Spree
- class Navigation < Spree::Base
- self.table_name = 'spree_menus_stores'
-
- belongs_to :menu, class_name: 'Spree::Menu'
- belongs_to :store, class_name: 'Spree::Store'
- end
-end
diff --git a/core/app/models/spree/store.rb b/core/app/models/spree/store.rb
index 7f686a4015..a49e63b320 100644
--- a/core/app/models/spree/store.rb
+++ b/core/app/models/spree/store.rb
@@ -3,8 +3,7 @@ module Spree
has_many :orders, class_name: 'Spree::Order'
has_many :payment_methods, class_name: 'Spree::PaymentMethod'
- has_many :navigations, inverse_of: :store
- has_many :menus, through: :navigations
+ has_many :menus
belongs_to :default_country, class_name: 'Spree::Country'
belongs_to :checkout_zone, class_name: 'Spree::Zone'
diff --git a/core/db/migrate/20210407200948_create_spree_menus.rb b/core/db/migrate/20210407200948_create_spree_menus.rb
index 116f257d3d..7ec057fda0 100644
--- a/core/db/migrate/20210407200948_create_spree_menus.rb
+++ b/core/db/migrate/20210407200948_create_spree_menus.rb
@@ -4,6 +4,8 @@ class CreateSpreeMenus < ActiveRecord::Migration[6.0]
t.column :name, :string
t.column :unique_code, :string, null: false
+ t.belongs_to :store
+
t.timestamps
end
diff --git a/core/db/migrate/20210409172956_create_spree_menus_stores.rb b/core/db/migrate/20210409172956_create_spree_menus_stores.rb
deleted file mode 100644
index 046291b18d..0000000000
--- a/core/db/migrate/20210409172956_create_spree_menus_stores.rb
+++ /dev/null
@@ -1,10 +0,0 @@
-class CreateSpreeMenusStores < ActiveRecord::Migration[6.0]
- def change
- create_table :spree_menus_stores, id: false do |t|
- t.belongs_to :menu
- t.belongs_to :store
- end
-
- add_index :spree_menus_stores, [:menu_id, :store_id], unique: true, name: 'menu_id_store_id_unique_index'
- end
-end
diff --git a/core/spec/models/spree/menu_spec.rb b/core/spec/models/spree/menu_spec.rb
index cd16068f4b..b3c288cc06 100644
--- a/core/spec/models/spree/menu_spec.rb
+++ b/core/spec/models/spree/menu_spec.rb
@@ -4,12 +4,10 @@ describe Spree::Menu, type: :model do
describe '.by_store' do
let!(:store_1) { create(:store) }
let!(:store_2) { create(:store) }
- let!(:store_3) { create(:store) }
-
- let!(:menu) { create(:menu, store_ids: [store_1.id, store_3.id]) }
+ let!(:menu) { create(:menu, store_id: store_1.id) }
it 'returns menu if they are avalable to the store' do
- expect(described_class.by_store(store_1.id)).to include(menu)
+ expect(described_class.by_store(store_1)).to include(menu)
expect(described_class.by_store(store_2.id)).not_to include(menu)
end
end
@@ -27,17 +25,16 @@ describe Spree::Menu, type: :model do
describe 'creating new menu' do
let!(:store_1) { create(:store) }
let!(:store_2) { create(:store) }
- let!(:store_3) { create(:store) }
- let!(:menu_a) { create(:menu, name: 'Footer', unique_code: 'ABC123', store_ids: [store_1.id, store_3.id]) }
+ let!(:menu_a) { create(:menu, name: 'Footer', unique_code: 'ABC123', store_id: store_1.id) }
let!(:menu_param) { create(:menu, name: 'Footer', unique_code: 'ABC 123 XyZ') }
it 'validates uniqueness of unique_code to be valid if not associated with a store with the same code' do
- expect(described_class.new(name: 'Footer', unique_code: 'ABC123', store_ids: [store_2.id])).to be_valid
+ expect(described_class.new(name: 'Footer', unique_code: 'ABC123', store_id: store_2.id)).to be_valid
end
it 'validates uniqueness of unique_code to not be valid if it is associated with a store with the same code' do
- expect(described_class.new(name: 'Footer', unique_code: 'ABC123', store_ids: [store_1.id])).not_to be_valid
+ expect(described_class.new(name: 'Footer', unique_code: 'ABC123', store_id: store_1.id)).not_to be_valid
end
it 'validates presence of name' do
diff --git a/frontend/spec/features/locale_spec.rb b/frontend/spec/features/locale_spec.rb
index 1795213956..4eab2fb8fa 100644
--- a/frontend/spec/features/locale_spec.rb
+++ b/frontend/spec/features/locale_spec.rb
@@ -4,7 +4,7 @@ describe 'setting locale', type: :feature, js: true do
let!(:store) { Spree::Store.default }
let!(:locale) { :fr }
- let!(:main_menu) { create(:menu, name: 'Main Menu', unique_code: 'spree-all-main', store_ids: [store.id]) }
+ let!(:main_menu) { create(:menu, name: 'Main Menu', unique_code: 'spree-all-main', store_id: store.id) }
let!(:mi_root) { create(:menu_item, name: 'URL', menu_id: main_menu.id, parent_id: main_menu.root.id, destination: 'https://spree.com') }
let!(:mi_home) { create(:menu_item, name: 'Home', menu_id: main_menu.id, parent_id: main_menu.root.id, linked_resource_type: 'Home Page') }
diff --git a/sample/db/samples/menu_items.rb b/sample/db/samples/menu_items.rb
index e16714a0c6..24711f0b93 100644
--- a/sample/db/samples/menu_items.rb
+++ b/sample/db/samples/menu_items.rb
@@ -1,7 +1,8 @@
-main_menu = Spree::Menu.find_by!(name: 'Main Menu')
-footer_menu = Spree::Menu.find_by!(name: 'Footer Menu')
+MENUS = []
-MENUS = [main_menu, footer_menu]
+Spree::Menu.all.each do |menu|
+ MENUS << menu
+end
MENUS.each do |menu|
##############
diff --git a/sample/db/samples/menus.rb b/sample/db/samples/menus.rb
index b75b37a774..24b90072aa 100644
--- a/sample/db/samples/menus.rb
+++ b/sample/db/samples/menus.rb
@@ -1,15 +1,13 @@
-main_menu = Spree::Menu.where(
- name: 'Main Menu',
- unique_code: 'spree-all-main'
-).first_or_create!
+Spree::Store.ids.each do |store|
+ Spree::Menu.where(
+ name: 'Main Menu',
+ unique_code: 'spree-all-main',
+ store_id: store
+ ).first_or_create!
-main_menu.store_ids = Spree::Store.ids
-main_menu.save!
-
-footer_menu = Spree::Menu.where(
- name: 'Footer Menu',
- unique_code: 'spree-all-footer'
-).first_or_create!
-
-footer_menu.store_ids = Spree::Store.ids
-footer_menu.save!
+ Spree::Menu.where(
+ name: 'Footer Menu',
+ unique_code: 'spree-all-footer',
+ store_id: store
+ ).first_or_create!
+end