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.

Fix broken test for touch after create, add tests for level of newly added items.

Matthew Kennedy46a3bacc7ef9
diff --git a/backend/app/views/spree/admin/menu_items/_form.html.erb b/backend/app/views/spree/admin/menu_items/_form.html.erb
index 7a5fff1ccf..456100ae34 100644
--- a/backend/app/views/spree/admin/menu_items/_form.html.erb
+++ b/backend/app/views/spree/admin/menu_items/_form.html.erb
@@ -21,7 +21,7 @@
</small>
<% end %>
- <%= f.field_container :parent_id, class: ['form-group'] do %>
+ <%= f.field_container :parent_id, class: ['form-group'] do %>
<%= f.label :parent_id, Spree.t('admin.navigation.nested_under') %>
<%= f.select :parent_id, nested_set_options(@menu.menu_items, @menu_item) {|i| "#{'-' * i.level} #{i.name}" }, {include_blank: false}, {class: 'select2'} %>
<%= f.error_message_on :parent_id %>
diff --git a/core/app/models/spree/menu_item.rb b/core/app/models/spree/menu_item.rb
index e6c12a80c3..9c1fdba13d 100644
--- a/core/app/models/spree/menu_item.rb
+++ b/core/app/models/spree/menu_item.rb
@@ -5,7 +5,7 @@ module Spree
belongs_to :menu, touch: true
belongs_to :linked_resource, polymorphic: true
- around_create :ensure_item_belongs_to_root
+ before_create :ensure_item_belongs_to_root
before_save :reset_link_attributes, :build_path, :paremeterize_code
after_save :touch_ancestors_and_menu
@@ -75,11 +75,9 @@ module Spree
def ensure_item_belongs_to_root
if menu.try(:root).present? && parent_id.nil?
self.parent = menu.root
- end
-
- yield
- move_to_child_of(menu.root) unless root
+ store_new_parent
+ end
end
def touch_ancestors_and_menu
diff --git a/core/spec/models/spree/menu_item_spec.rb b/core/spec/models/spree/menu_item_spec.rb
index d7539fde6e..79d74898c4 100644
--- a/core/spec/models/spree/menu_item_spec.rb
+++ b/core/spec/models/spree/menu_item_spec.rb
@@ -103,7 +103,7 @@ describe Spree::MenuItem, type: :model do
item_taxon.update(linked_resource: taxon)
item_taxon.update(linked_resource_id: nil)
- expect(item_taxon.destination).to eql nil
+ expect(item_taxon.destination).to be nil
end
it 'returns product path' do
@@ -116,7 +116,7 @@ describe Spree::MenuItem, type: :model do
item_product.update(linked_resource: product)
item_product.update(linked_resource_id: nil)
- expect(item_product.destination).to eql nil
+ expect(item_product.destination).to be nil
end
it 'returns root path' do
@@ -137,11 +137,19 @@ describe Spree::MenuItem, type: :model do
end
describe '.ensure_item_belongs_to_root' do
- let(:item_x) { create(:menu_item, name: 'URL', item_type: 'Link', menu: menu, parent: menu.root, linked_resource_type: 'URL', code: 'My Fantastic Code') }
+ let(:item_x) { create(:menu_item, name: 'URL', item_type: 'Link', menu: menu, linked_resource_type: 'URL', code: 'My Fantastic Code') }
it 'Sets new items parent_id to root.id' do
expect(item_x.parent_id).to eql menu.root.id
end
+
+ it 'is not a root' do
+ expect(item_x.root?).to be false
+ end
+
+ it 'is level 1' do
+ expect(item_x.level).to eql 1
+ end
end
describe 'touch menu and store' do