From 88d9f25a432c9aa8e68d6e3a871a739f5d571e4f Mon Sep 17 00:00:00 2001 From: Tamas Erdos Date: Sat, 29 Aug 2026 16:53:22 +0200 Subject: [PATCH 1/5] fix(editor): render nested blocks only once in the section blocks tree In the editor's section blocks index (tree presentation), a nested block was rendered twice: once at the root level and once under its parent. Root cause: Maglev::Content::SectionContent#root_blocks selected blocks via the root? flag of their block *type definition* (which defaults to true when unspecified) instead of looking at the block's own parent_id. So whenever a block type is both root-eligible AND accepted as a child of another block (e.g. a navbar menu_item accepting nested menu_item blocks, a very common pattern for navbars with submenus), every nested block of that type also showed up in the root list. Besides the duplicated entry in the UI, this also generated duplicated DOM ids and mixed sortable scopes (root vs. parent), confusing drag & drop. Fix: select root blocks by parent_id (blank = root), mirroring how the front-end builds the block tree in Maglev::SectionComponent #build_block_tree. Notes: - Maglev::Section#root_blocks (definition level) is untouched: the root flag still drives which block types can be *added* at the root. - Bonus: #root_blocks no longer raises when a block's type has been removed from the theme (its definition is nil). Tests: - model spec: #root_blocks returns only parentless blocks and #child_blocks_of returns the direct children (navbar factory with nested menu items). - request spec: the navbar blocks index renders a nested menu item exactly once, nested under its parent's sortable scope (red before the fix, green after). - the theme factory navbar now uses blocks_presentation: 'tree' and menu_item accepts menu_item children (mirroring the dummy theme YAML), so the tree presentation is actually exercised in specs. --- app/models/maglev/content/section_content.rb | 8 ++- spec/factories/maglev/themes.rb | 10 ++-- .../maglev/content/section_content_spec.rb | 23 ++++++++ .../maglev/editor/section_blocks_spec.rb | 52 +++++++++++++++++++ 4 files changed, 88 insertions(+), 5 deletions(-) diff --git a/app/models/maglev/content/section_content.rb b/app/models/maglev/content/section_content.rb index a8fe9db99..ef160f892 100644 --- a/app/models/maglev/content/section_content.rb +++ b/app/models/maglev/content/section_content.rb @@ -35,7 +35,13 @@ def block_definitions end def root_blocks - blocks.select(&:root?) + # NOTE: a block whose type is root-eligible (root: true, the default) can still be + # nested under another block when its type is also accepted as a child + # (e.g. a navbar menu_item inside another menu_item). Selecting blocks by their + # definition's root? flag would return those nested blocks as well, rendering them + # twice in the editor tree. So we filter on the parent_id instead, like the + # front-end block tree builder does. + blocks.select { |block| block.parent_id.blank? } end # return the definitions of root blocks that can be added diff --git a/spec/factories/maglev/themes.rb b/spec/factories/maglev/themes.rb index 96fdfe927..0d72450e0 100644 --- a/spec/factories/maglev/themes.rb +++ b/spec/factories/maglev/themes.rb @@ -66,11 +66,13 @@ site_scoped: true, singleton: true, insert_at: 'top', + blocks_presentation: 'tree', settings: [{ label: 'Logo', id: 'logo', type: 'image' }], blocks: [ { name: 'Menu item', type: 'menu_item', + accept: %w[menu_item], settings: [ { label: 'Label', id: 'label', type: 'text', default: 'Menu item' }, { label: 'Link', id: 'link', type: 'link', default: '/' } @@ -153,8 +155,8 @@ { type: 'jumbotron', settings: { - "title": "Let's create the product
your clients
will love.", - "body": '

NoCoffee, passionated developers,
creators of web applications, mobiles apps and
fancy R&D projects.

' + title: "Let's create the product
your clients
will love.", + body: '

NoCoffee, passionated developers,
creators of web applications, mobiles apps and
fancy R&D projects.

' }, blocks: [] }, @@ -190,8 +192,8 @@ { type: 'jumbotron', settings: { - "title": 'About our awesome team', - "body": '

NoCoffee, passionated developers,
creators of web applications, mobiles apps and
fancy R&D projects.

' + title: 'About our awesome team', + body: '

NoCoffee, passionated developers,
creators of web applications, mobiles apps and
fancy R&D projects.

' }, blocks: [] } diff --git a/spec/models/maglev/content/section_content_spec.rb b/spec/models/maglev/content/section_content_spec.rb index 7e554fa86..0f74e560a 100644 --- a/spec/models/maglev/content/section_content_spec.rb +++ b/spec/models/maglev/content/section_content_spec.rb @@ -52,4 +52,27 @@ end end end + + context '#root_blocks' do + let(:store) { build(:sections_content_store, :with_navbar, page: page) } + + it 'returns only the blocks without a parent' do + # the menu_item type is both root-eligible and accepted as a child of itself: + # nested menu items must not be returned here, otherwise the editor tree + # would render them twice (at the root and under their parent) + expect(section_content.root_blocks.map(&:id)).to eq %w[menu-item-0 menu-item-1] + end + end + + context '#child_blocks_of' do + let(:store) { build(:sections_content_store, :with_navbar, page: page) } + + it 'returns the direct children of the given block' do + expect(section_content.child_blocks_of('menu-item-1').map(&:id)).to eq %w[menu-item-1-1 menu-item-1-2] + end + + it 'returns an empty list for a block without children' do + expect(section_content.child_blocks_of('menu-item-0')).to eq [] + end + end end diff --git a/spec/requests/maglev/editor/section_blocks_spec.rb b/spec/requests/maglev/editor/section_blocks_spec.rb index 20fa0a055..534ba8565 100644 --- a/spec/requests/maglev/editor/section_blocks_spec.rb +++ b/spec/requests/maglev/editor/section_blocks_spec.rb @@ -19,6 +19,58 @@ get "/maglev/editor/en/#{home_page.id}/sections/#{section_id}/blocks" expect(response).to be_successful end + + context 'when the section presents its blocks as a tree' do + let(:site_store) { fetch_sections_store('_site') } + let(:navbar_section) { site_store.find_section_by_type('navbar') } + let(:section_id) { navbar_section['id'] } + + # rubocop:disable Style/StringHashKeys + before do + # two root menu items, one of them with a nested menu item + navbar_section['blocks'] = [ + { + 'id' => 'menu-item-0', + 'type' => 'menu_item', + 'settings' => [ + { 'id' => 'label', 'value' => 'Home' }, + { 'id' => 'link', 'value' => '/' } + ] + }, + { + 'id' => 'menu-item-1', + 'type' => 'menu_item', + 'settings' => [ + { 'id' => 'label', 'value' => 'About us' }, + { 'id' => 'link', 'value' => '/about-us' } + ] + }, + { + 'id' => 'nested-menu-item', + 'type' => 'menu_item', + 'parent_id' => 'menu-item-1', + 'settings' => [ + { 'id' => 'label', 'value' => 'Nested item' }, + { 'id' => 'link', 'value' => '/nested' } + ] + } + ] + site_store.sections_translations_will_change! + site_store.save! + end + # rubocop:enable Style/StringHashKeys + + it 'renders each block exactly once' do + get "/maglev/editor/en/#{home_page.id}/sections/#{section_id}/blocks" + expect(response).to be_successful + expect(response.body.scan('Nested item').count).to eq 1 + end + + it 'nests the child block under its parent' do + get "/maglev/editor/en/#{home_page.id}/sections/#{section_id}/blocks" + expect(response.body).to include('data-sortable-scope-value="menu-item-1"') + end + end end describe 'POST /maglev/editor/:context/sections/:id/blocks' do From c648d42976c850f4ffb2cfece27e8676c9a50b87 Mon Sep 17 00:00:00 2001 From: Tamas Erdos Date: Sat, 29 Aug 2026 18:16:57 +0200 Subject: [PATCH 2/5] fix(editor): stale lock version errors when saving site scoped sections MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Editing a site scoped section (e.g. a navbar with a menu) always ended up showing the "someone else updated the content in the meantime" modal, even with a single editor. The very first save went through (both stores shared the same lock version at that point), and every subsequent save failed forever. Root cause: a site scoped section is listed in a layout store (its "slot", e.g. the header/main store) while its content lives in the global site scoped store (_site), which is the store the content services actually write to. The editor forms however submitted the lock version of the *slot* store (LockVersionConcern#source_lock_version, keyed off SectionContent#store_handle). Since site scoped writes only bump the _site store, the two versions diverge after the first save and every following save raises ActiveRecord::StaleObjectError. Fixes: 1. Forms now submit the section's own lock version (LockVersionConcern#source_lock_version): the sections content fetcher already stamps each section with the lock version of the store its content comes from — the _site store for site scoped sections. This is a no-op for regular sections (their stamped version is the one of their own store). 2. The :persisted turbo stream event now dispatches the fresh lock version of the store which was actually written (UpdateSectionService / UpdateSectionBlockService return it), so the auto-saving settings forms keep working on subsequent changes. Previously the (stale) slot store version was dispatched. 3. The preview now uses the same counter: Maglev::SectionComponent #lock_version reads the lock version stamped on the section attributes instead of the unrelated site/page *model* lock versions. As a result, the section:checkLockVersion mechanism only refreshes genuinely stale preview sections instead of always refreshing site scoped ones (the two counters could never match). Tests: - request specs: with the slot and _site stores holding different lock versions, the section and block forms of a site scoped section render the _site lock version, updating with it succeeds and dispatches the bumped version, and a stale submission is still rejected with a 409. - component spec: SectionComponent#lock_version uses the stamped attribute, including for a site scoped definition. - UpdateSectionService specs now assert the returned store (the passed store for regular sections, the site scoped one for site scoped sections). --- app/components/maglev/section_component.rb | 12 +- .../maglev/editor/lock_version_concern.rb | 7 +- .../editor/section_blocks_controller.rb | 2 +- .../maglev/editor/sections_controller.rb | 2 +- .../content/update_section_block_service.rb | 5 + .../maglev/content/update_section_service.rb | 5 + .../section_blocks/update.turbo_stream.erb | 7 +- .../editor/sections/update.turbo_stream.erb | 7 +- .../maglev/section_component_spec.rb | 25 ++++ .../maglev/editor/section_blocks_spec.rb | 120 ++++++++++++------ spec/requests/maglev/editor/sections_spec.rb | 38 ++++++ .../content/update_section_service_spec.rb | 7 +- 12 files changed, 176 insertions(+), 61 deletions(-) diff --git a/app/components/maglev/section_component.rb b/app/components/maglev/section_component.rb index 3de31bc6d..830ec6e72 100644 --- a/app/components/maglev/section_component.rb +++ b/app/components/maglev/section_component.rb @@ -4,12 +4,12 @@ # we need to move it along with the PageComponent and BlockComponent # to a separate folder (content?) -# rubocop:disable Metrics/ClassLength module Maglev class SectionComponent < BaseComponent include TagHelper extend Forwardable + def_delegators :parent, :site, :page, :config attr_reader :parent, :id, :type, :settings, :attributes, :definition, :templates_root_path, @@ -35,8 +35,11 @@ def dom_id @dom_id ||= "section-#{id}" end + # the lock version of the store the section content comes from + # (stamped by the sections content fetcher, even for site scoped sections + # whose content lives in the global site scoped store) def lock_version - @lock_version ||= lock_source&.lock_version || '0' + attributes[:lock_version] || '0' end def dom_data @@ -68,10 +71,6 @@ def render private - def lock_source - definition&.site_scoped? ? site : page - end - def build_block_list build_blocks(attributes[:blocks]) end @@ -140,4 +139,3 @@ def inspect_fields end end end -# rubocop:enable Metrics/ClassLength diff --git a/app/controllers/concerns/maglev/editor/lock_version_concern.rb b/app/controllers/concerns/maglev/editor/lock_version_concern.rb index 3b93050c4..117ec293f 100644 --- a/app/controllers/concerns/maglev/editor/lock_version_concern.rb +++ b/app/controllers/concerns/maglev/editor/lock_version_concern.rb @@ -11,8 +11,13 @@ module LockVersionConcern private + # NOTE: we can't always rely on the lock version of the store the section was fetched + # from: a site scoped section is listed in a layout store (its "slot") but its content + # lives in the global site scoped store, which is what the content services update. + # Fortunately, the sections content fetcher stamps each section with the lock version + # of the store its content actually comes from, so we use it whenever possible. def source_lock_version - sections_store.lock_version || 0 + @section ? @section.lock_version : (sections_store.lock_version || 0) end end end diff --git a/app/controllers/maglev/editor/section_blocks_controller.rb b/app/controllers/maglev/editor/section_blocks_controller.rb index 6ac82ad0c..f413af0d1 100644 --- a/app/controllers/maglev/editor/section_blocks_controller.rb +++ b/app/controllers/maglev/editor/section_blocks_controller.rb @@ -74,7 +74,7 @@ def sections_store end def update_section_block - services.update_section_block.call( + @updated_store = services.update_section_block.call( store: sections_store, section_id: @section.id, block_id: @section_block.id, diff --git a/app/controllers/maglev/editor/sections_controller.rb b/app/controllers/maglev/editor/sections_controller.rb index c35ba4e33..d5fc20601 100644 --- a/app/controllers/maglev/editor/sections_controller.rb +++ b/app/controllers/maglev/editor/sections_controller.rb @@ -89,7 +89,7 @@ def create_section end def update_section - services.update_section.call( + @updated_store = services.update_section.call( store: sections_store, section_id: @section.id, content: params[:section].to_unsafe_h, diff --git a/app/services/maglev/content/update_section_block_service.rb b/app/services/maglev/content/update_section_block_service.rb index 347928673..26a9e9fbb 100644 --- a/app/services/maglev/content/update_section_block_service.rb +++ b/app/services/maglev/content/update_section_block_service.rb @@ -28,6 +28,9 @@ def call private def unsafe_call + # return the store which has been actually written into, so the caller + # can read its fresh lock version (a site scoped section is listed in + # a layout store but its content is written in the site scoped store) if site_scoped? update_section_block_content!(site_scoped_store) else @@ -41,6 +44,8 @@ def update_section_block_content!(source) source.sections_translations_will_change! update_section_block_content(source) source.save! + + source end def update_section_block_content(source) diff --git a/app/services/maglev/content/update_section_service.rb b/app/services/maglev/content/update_section_service.rb index a4bac8f5d..53c998e7c 100644 --- a/app/services/maglev/content/update_section_service.rb +++ b/app/services/maglev/content/update_section_service.rb @@ -26,6 +26,9 @@ def call private def unsafe_call + # return the store which has been actually written into, so the caller + # can read its fresh lock version (a site scoped section is listed in + # a layout store but its content is written in the site scoped store) if site_scoped? add_missing_site_scoped_section # make sure the section is also present in the site scoped store update_section_content!(site_scoped_store) @@ -40,6 +43,8 @@ def update_section_content!(source) source.sections_translations_will_change! update_section_content(source) source.save! + + source end def update_section_content(source) diff --git a/app/views/maglev/editor/section_blocks/update.turbo_stream.erb b/app/views/maglev/editor/section_blocks/update.turbo_stream.erb index 613844a6a..dae6ff104 100644 --- a/app/views/maglev/editor/section_blocks/update.turbo_stream.erb +++ b/app/views/maglev/editor/section_blocks/update.turbo_stream.erb @@ -1,13 +1,10 @@ -<%= turbo_stream.dispatch_event "section-block-#{@section_block.id}:persisted", payload: { lockVersion: source_lock_version }.to_json %> - +<%= turbo_stream.dispatch_event "section-block-#{@section_block.id}:persisted", payload: { lockVersion: @updated_store.lock_version }.to_json %> <%= turbo_stream.update 'page-layout-notification', method: 'morph' do %> <%= render Maglev::Uikit::BadgeComponent.new(color: :green, icon_name: 'checkbox_circle', disappear_after: 3.seconds).with_content(flash.now[:notice]) %> <% end %> - <%= turbo_stream.update dom_id(current_maglev_page, 'topbar-page-info'), method: 'morph' do %> <%= render 'layouts/maglev/editor/topbar/page_info' %> <% end %> - <%= turbo_stream.update dom_id(current_maglev_page, 'topbar-publish-button'), method: 'morph' do %> <%= render 'layouts/maglev/editor/topbar/publish_button' %> -<% end %> \ No newline at end of file +<% end %> diff --git a/app/views/maglev/editor/sections/update.turbo_stream.erb b/app/views/maglev/editor/sections/update.turbo_stream.erb index d1ad61bb7..f270a9d0c 100644 --- a/app/views/maglev/editor/sections/update.turbo_stream.erb +++ b/app/views/maglev/editor/sections/update.turbo_stream.erb @@ -1,13 +1,10 @@ -<%= turbo_stream.dispatch_event "section-#{@section.id}:persisted", payload: { lockVersion: source_lock_version }.to_json %> - +<%= turbo_stream.dispatch_event "section-#{@section.id}:persisted", payload: { lockVersion: @updated_store.lock_version }.to_json %> <%= turbo_stream.update 'page-layout-notification', method: 'morph' do %> <%= render Maglev::Uikit::BadgeComponent.new(color: :green, icon_name: 'checkbox_circle', disappear_after: 3.seconds).with_content(flash.now[:notice]) %> <% end %> - <%= turbo_stream.update dom_id(current_maglev_page, 'topbar-page-info'), method: 'morph' do %> <%= render 'layouts/maglev/editor/topbar/page_info' %> <% end %> - <%= turbo_stream.update dom_id(current_maglev_page, 'topbar-publish-button'), method: 'morph' do %> <%= render 'layouts/maglev/editor/topbar/publish_button' %> -<% end %> \ No newline at end of file +<% end %> diff --git a/spec/components/maglev/section_component_spec.rb b/spec/components/maglev/section_component_spec.rb index 1bacae5f7..0f5b3b301 100644 --- a/spec/components/maglev/section_component_spec.rb +++ b/spec/components/maglev/section_component_spec.rb @@ -36,6 +36,31 @@ end end + describe '#lock_version' do + subject { component.lock_version } + + context 'when the attributes carry no lock version' do + it { is_expected.to eq '0' } + end + + context 'when the attributes carry the lock version of the source store' do + before { attributes[:lock_version] = 42 } + + it { is_expected.to eq 42 } + end + + context 'when the section is site scoped' do + # the lock version stamped on the attributes is the one of the + # site scoped store (where the content lives), not the one of + # the layout store the section is listed in + let(:definition) { build(:section, category: 'headers', site_scoped: true) } + + before { attributes[:lock_version] = 7 } + + it { is_expected.to eq 7 } + end + end + describe '#render' do subject { pretty_html(component.render) } diff --git a/spec/requests/maglev/editor/section_blocks_spec.rb b/spec/requests/maglev/editor/section_blocks_spec.rb index 534ba8565..f3553b56b 100644 --- a/spec/requests/maglev/editor/section_blocks_spec.rb +++ b/spec/requests/maglev/editor/section_blocks_spec.rb @@ -14,6 +14,51 @@ allow(Maglev.local_themes).to receive(:first).and_return(theme) end + # the navbar is a site scoped section presenting its blocks as a tree: + # it is listed in the "header" layout store (its "slot") while its content + # lives in the global site scoped store + shared_context 'with a site scoped section with nested blocks' do + let(:slot_store) { fetch_sections_store('header') } + let(:site_store) { fetch_sections_store('_site') } + let(:navbar_section) { site_store.find_section_by_type('navbar') } + let(:section_id) { navbar_section['id'] } + + # rubocop:disable Style/StringHashKeys + before do + # two root menu items, one of them with a nested menu item + navbar_section['blocks'] = [ + { + 'id' => 'menu-item-0', + 'type' => 'menu_item', + 'settings' => [ + { 'id' => 'label', 'value' => 'Home' }, + { 'id' => 'link', 'value' => '/' } + ] + }, + { + 'id' => 'menu-item-1', + 'type' => 'menu_item', + 'settings' => [ + { 'id' => 'label', 'value' => 'About us' }, + { 'id' => 'link', 'value' => '/about-us' } + ] + }, + { + 'id' => 'nested-menu-item', + 'type' => 'menu_item', + 'parent_id' => 'menu-item-1', + 'settings' => [ + { 'id' => 'label', 'value' => 'Nested item' }, + { 'id' => 'link', 'value' => { 'link_type' => 'url', 'href' => '/nested', 'open_new_window' => false } } + ] + } + ] + site_store.sections_translations_will_change! + site_store.save! + end + # rubocop:enable Style/StringHashKeys + end + describe 'GET /maglev/editor/:context/sections/:id/blocks' do it 'returns a success response' do get "/maglev/editor/en/#{home_page.id}/sections/#{section_id}/blocks" @@ -21,44 +66,7 @@ end context 'when the section presents its blocks as a tree' do - let(:site_store) { fetch_sections_store('_site') } - let(:navbar_section) { site_store.find_section_by_type('navbar') } - let(:section_id) { navbar_section['id'] } - - # rubocop:disable Style/StringHashKeys - before do - # two root menu items, one of them with a nested menu item - navbar_section['blocks'] = [ - { - 'id' => 'menu-item-0', - 'type' => 'menu_item', - 'settings' => [ - { 'id' => 'label', 'value' => 'Home' }, - { 'id' => 'link', 'value' => '/' } - ] - }, - { - 'id' => 'menu-item-1', - 'type' => 'menu_item', - 'settings' => [ - { 'id' => 'label', 'value' => 'About us' }, - { 'id' => 'link', 'value' => '/about-us' } - ] - }, - { - 'id' => 'nested-menu-item', - 'type' => 'menu_item', - 'parent_id' => 'menu-item-1', - 'settings' => [ - { 'id' => 'label', 'value' => 'Nested item' }, - { 'id' => 'link', 'value' => '/nested' } - ] - } - ] - site_store.sections_translations_will_change! - site_store.save! - end - # rubocop:enable Style/StringHashKeys + include_context 'with a site scoped section with nested blocks' it 'renders each block exactly once' do get "/maglev/editor/en/#{home_page.id}/sections/#{section_id}/blocks" @@ -114,6 +122,42 @@ expect(response).to be_successful expect(main_store.reload.sections.dig(1, 'blocks', 0, 'settings', 0, 'value')).to eq 'My new title 🍔' end + + context 'when the section is site scoped' do + include_context 'with a site scoped section with nested blocks' + + let(:block_id) { 'nested-menu-item' } + + before do + # make sure the lock versions of the slot store and the site scoped store differ + # rubocop:disable Rails/SkipsModelValidations + slot_store.update_column(:lock_version, 0) + site_store.update_column(:lock_version, 7) + # rubocop:enable Rails/SkipsModelValidations + end + + it 'renders the form with the lock version of the site scoped store' do + get "/maglev/editor/en/#{home_page.id}/sections/#{section_id}/blocks/#{block_id}/edit" + expect(response.body).to have_selector("input[name='lock_version'][value='7']", visible: :hidden) + end + + it 'updates the site scoped store and dispatches its fresh lock version' do + put "/maglev/editor/en/#{home_page.id}/sections/#{section_id}/blocks/#{block_id}", + as: :turbo_stream, + params: { lock_version: 7, section_block: { label: 'Our dream team' } } + expect(response).to be_successful + expect(response.body).to include('"lockVersion":8') + expect(site_store.reload.lock_version).to eq 8 + expect(slot_store.reload.lock_version).to eq 0 + end + + it 'still rejects a stale lock version' do + put "/maglev/editor/en/#{home_page.id}/sections/#{section_id}/blocks/#{block_id}", + as: :turbo_stream, + params: { lock_version: 6, section_block: { label: 'Our dream team' } } + expect(response).to have_http_status(:conflict) + end + end end describe 'DELETE /maglev/editor/:context/sections/:id/blocks/:id' do diff --git a/spec/requests/maglev/editor/sections_spec.rb b/spec/requests/maglev/editor/sections_spec.rb index 348cc9833..c496addb2 100644 --- a/spec/requests/maglev/editor/sections_spec.rb +++ b/spec/requests/maglev/editor/sections_spec.rb @@ -78,6 +78,44 @@ expect(response.media_type).to eq Mime[:turbo_stream] expect(main_store.reload.sections.dig(0, 'settings', 0, 'value')).to eq('Hello world!') end + + context 'when the section is site scoped' do + # a site scoped section is listed in a layout store (its "slot") but its content + # lives in the global site scoped store: the lock version of the latter must be used + let(:slot_store) { fetch_sections_store('header') } + let(:site_store) { fetch_sections_store('_site') } + let(:section_id) { site_store.find_section_by_type('navbar')['id'] } + + before do + # make sure the lock versions of the two stores differ + # rubocop:disable Rails/SkipsModelValidations + slot_store.update_column(:lock_version, 0) + site_store.update_column(:lock_version, 7) + # rubocop:enable Rails/SkipsModelValidations + end + + it 'renders the form with the lock version of the site scoped store' do + get "/maglev/editor/en/#{home_page.id}/sections/#{section_id}/edit" + expect(response.body).to have_selector("input[name='lock_version'][value='7']", visible: :hidden) + end + + it 'updates the site scoped store and dispatches its fresh lock version' do + put "/maglev/editor/en/#{home_page.id}/sections/#{section_id}", + as: :turbo_stream, + params: { lock_version: 7, section: { logo: 'new-logo.png' } } + expect(response).to be_successful + expect(response.body).to include('"lockVersion":8') + expect(site_store.reload.lock_version).to eq 8 + expect(slot_store.reload.lock_version).to eq 0 + end + + it 'still rejects a stale lock version' do + put "/maglev/editor/en/#{home_page.id}/sections/#{section_id}", + as: :turbo_stream, + params: { lock_version: 6, section: { logo: 'new-logo.png' } } + expect(response).to have_http_status(:conflict) + end + end end describe 'DELETE /maglev/editor/:context/sections/:id' do diff --git a/spec/services/maglev/content/update_section_service_spec.rb b/spec/services/maglev/content/update_section_service_spec.rb index 053f0aaee..77512a7ae 100644 --- a/spec/services/maglev/content/update_section_service_spec.rb +++ b/spec/services/maglev/content/update_section_service_spec.rb @@ -22,7 +22,7 @@ let(:lock_version) { 0 } it 'updates the section' do - expect(subject).to eq(true) + expect(subject).to be(store) expect(store.reload.sections.dig(0, 'settings', 0, 'value')).to eq('Hello world!') expect(store.reload.lock_version).to eq(1) end @@ -51,7 +51,7 @@ context 'Given an existing page section with a version' do it 'updates the section' do - expect(subject).to eq(true) + expect(subject).to be(store) end context 'Given the store has been modified while updating the section' do @@ -79,7 +79,8 @@ end it 'updates the section content on the site' do - expect(subject).to eq(true) + # the written store is the site scoped one, not the passed (slot) store + expect(subject).to eq(site_scoped_store) # rubocop:disable Style/StringHashKeys expect(site_scoped_sections.dig(0, 'settings', 0, 'value')).to eq({ 'url' => '/awesome-logo.png' }) # rubocop:enable Style/StringHashKeys From 7d5fe1bcd1da84b6ebbcd0223995a0384f48ade0 Mon Sep 17 00:00:00 2001 From: Claude Agent Date: Tue, 1 Sep 2026 19:41:04 +0000 Subject: [PATCH 3/5] fix(editor): keep the other blocks in place when sorting section blocks Sorting the blocks of a section (drag & drop in the editor, or the sort endpoint) re-sorted the whole flat blocks list with Array#sort_by!, giving every block outside of the sorted scope the same key (-1). Two problems with that: - Array#sort_by! is not stable, so the blocks sharing the -1 key (the root blocks when sorting the children of a menu item, or all the children when sorting the root blocks) could be shuffled. Whether it actually happens depends on the platform's qsort_r, which is why the bug shows up on some machines only (macOS, glibc 2.37/2.38, ...). - even with a stable sort, all the non-sibling blocks were moved before the sorted siblings, so the stored order churned on every sort. Fix: only re-order the siblings (the blocks sharing the requested parent_id), in place, with a stable sort. The other blocks keep the exact position they had in the list. Co-Authored-By: Claude Fable 5.1 --- .../content/sort_section_blocks_service.rb | 22 ++++++- .../sort_section_blocks_service_spec.rb | 59 +++++++++++++++++++ 2 files changed, 79 insertions(+), 2 deletions(-) diff --git a/app/services/maglev/content/sort_section_blocks_service.rb b/app/services/maglev/content/sort_section_blocks_service.rb index 5e3647af8..940ac77aa 100644 --- a/app/services/maglev/content/sort_section_blocks_service.rb +++ b/app/services/maglev/content/sort_section_blocks_service.rb @@ -42,11 +42,29 @@ def sort_section_blocks!(source) source.save! end + # Only the siblings (the blocks sharing the same parent) are re-ordered, in place. + # The other blocks keep their position in the flat list: sorting the whole list + # with a non-stable sort (Array#sort_by!) used to shuffle the blocks outside of + # the sorted scope, like the children of the other blocks in a tree. def sort_section_blocks(source) - find_blocks(source)&.sort_by! do |block| - block['parent_id'] == parent_id ? block_ids.index(block['id']) || Float::INFINITY : -1 + blocks = find_blocks(source) + return if blocks.blank? + + indices = blocks.each_index.select { |index| sibling?(blocks[index]) } + sorted_siblings = sort_siblings(blocks.values_at(*indices)) + + indices.zip(sorted_siblings) { |index, block| blocks[index] = block } + end + + def sort_siblings(siblings) + siblings.sort_by.with_index do |block, position| + [block_ids.index(block['id']) || Float::INFINITY, position] end end + + def sibling?(block) + block['parent_id'].presence == parent_id.presence + end end end end diff --git a/spec/services/maglev/content/sort_section_blocks_service_spec.rb b/spec/services/maglev/content/sort_section_blocks_service_spec.rb index 5f24aae25..62dc9aee6 100644 --- a/spec/services/maglev/content/sort_section_blocks_service_spec.rb +++ b/spec/services/maglev/content/sort_section_blocks_service_spec.rb @@ -76,5 +76,64 @@ end }.to ['Home', 'About us', 'Our office', 'Our team'] end + + context 'when the sorted siblings are interleaved with other blocks' do + # blocks are appended to the flat list as the editor creates them, so the children + # of a menu item are rarely stored right after it + let!(:page) do + create(:page, header_sections: [{ + id: 'abc', + type: 'navbar', + settings: [{ id: :logo, value: 'logo.png' }], + blocks: [ + menu_item('home', 'Home'), + menu_item('about', 'About us'), + menu_item('about-team', 'Our team', parent_id: 'about'), + menu_item('contact', 'Contact'), + menu_item('about-office', 'Our office', parent_id: 'about'), + menu_item('contact-email', 'Email', parent_id: 'contact'), + menu_item('contact-phone', 'Phone', parent_id: 'contact'), + menu_item('blog', 'Blog') + ] + }]) + end + + def menu_item(id, label, parent_id: nil) + { + id: id, + type: 'menu_item', + parent_id: parent_id, + settings: [{ id: :label, value: label }, { id: :link, value: '/' }] + }.compact + end + + def block_ids_in_store + site_scoped_store.reload.sections.dig(0, 'blocks').map { |block| block['id'] } + end + + context 'sorting the root blocks' do + let(:parent_id) { nil } + let(:block_ids) { %w[blog contact about home] } + + it 'only moves the root blocks and leaves the nested blocks where they are' do + subject + expect(block_ids_in_store).to eq( + %w[blog contact about-team about about-office contact-email contact-phone home] + ) + end + end + + context 'sorting the children of a block' do + let(:parent_id) { 'about' } + let(:block_ids) { %w[about-office about-team] } + + it 'only moves the children and leaves the other blocks where they are' do + subject + expect(block_ids_in_store).to eq( + %w[home about about-office contact about-team contact-email contact-phone blog] + ) + end + end + end end end From 20ca650d90b7d74c980ae4336a2bc46f1b6302dd Mon Sep 17 00:00:00 2001 From: Tamas Erdos Date: Wed, 2 Sep 2026 00:00:37 +0200 Subject: [PATCH 4/5] fix(editor): keep the other blocks in place when sorting section blocks Sorting the blocks of a section (drag & drop in the editor, or the sort endpoint) re-sorted the whole flat blocks list with Array#sort_by!, giving every block outside of the sorted scope the same key (-1). Two problems with that: - Array#sort_by! is not stable, so the blocks sharing the -1 key (the root blocks when sorting the children of a menu item, or all the children when sorting the root blocks) could be shuffled. Whether it actually happens depends on the platform's qsort_r, which is why the bug shows up on some machines only (macOS, glibc 2.37/2.38, ...). - even with a stable sort, all the non-sibling blocks were moved before the sorted siblings, so the stored order churned on every sort. Fix: only re-order the siblings (the blocks sharing the requested parent_id), in place, with a stable sort. The other blocks keep the exact position they had in the list. Co-Authored-By: Claude Fable 5.1 --- app/components/maglev/section_component.rb | 12 ++- .../maglev/editor/lock_version_concern.rb | 7 +- .../editor/section_blocks_controller.rb | 2 +- .../maglev/editor/sections_controller.rb | 2 +- app/models/maglev/content/section_content.rb | 8 +- .../content/sort_section_blocks_service.rb | 22 +---- .../content/update_section_block_service.rb | 5 - .../maglev/content/update_section_service.rb | 5 - .../section_blocks/update.turbo_stream.erb | 7 +- .../editor/sections/update.turbo_stream.erb | 7 +- .../maglev/section_component_spec.rb | 25 ----- spec/factories/maglev/themes.rb | 10 +- .../maglev/content/section_content_spec.rb | 23 ----- .../maglev/editor/section_blocks_spec.rb | 96 ------------------- spec/requests/maglev/editor/sections_spec.rb | 38 -------- .../sort_section_blocks_service_spec.rb | 59 ------------ .../content/update_section_service_spec.rb | 7 +- 17 files changed, 30 insertions(+), 305 deletions(-) diff --git a/app/components/maglev/section_component.rb b/app/components/maglev/section_component.rb index 830ec6e72..3de31bc6d 100644 --- a/app/components/maglev/section_component.rb +++ b/app/components/maglev/section_component.rb @@ -4,12 +4,12 @@ # we need to move it along with the PageComponent and BlockComponent # to a separate folder (content?) +# rubocop:disable Metrics/ClassLength module Maglev class SectionComponent < BaseComponent include TagHelper extend Forwardable - def_delegators :parent, :site, :page, :config attr_reader :parent, :id, :type, :settings, :attributes, :definition, :templates_root_path, @@ -35,11 +35,8 @@ def dom_id @dom_id ||= "section-#{id}" end - # the lock version of the store the section content comes from - # (stamped by the sections content fetcher, even for site scoped sections - # whose content lives in the global site scoped store) def lock_version - attributes[:lock_version] || '0' + @lock_version ||= lock_source&.lock_version || '0' end def dom_data @@ -71,6 +68,10 @@ def render private + def lock_source + definition&.site_scoped? ? site : page + end + def build_block_list build_blocks(attributes[:blocks]) end @@ -139,3 +140,4 @@ def inspect_fields end end end +# rubocop:enable Metrics/ClassLength diff --git a/app/controllers/concerns/maglev/editor/lock_version_concern.rb b/app/controllers/concerns/maglev/editor/lock_version_concern.rb index 117ec293f..3b93050c4 100644 --- a/app/controllers/concerns/maglev/editor/lock_version_concern.rb +++ b/app/controllers/concerns/maglev/editor/lock_version_concern.rb @@ -11,13 +11,8 @@ module LockVersionConcern private - # NOTE: we can't always rely on the lock version of the store the section was fetched - # from: a site scoped section is listed in a layout store (its "slot") but its content - # lives in the global site scoped store, which is what the content services update. - # Fortunately, the sections content fetcher stamps each section with the lock version - # of the store its content actually comes from, so we use it whenever possible. def source_lock_version - @section ? @section.lock_version : (sections_store.lock_version || 0) + sections_store.lock_version || 0 end end end diff --git a/app/controllers/maglev/editor/section_blocks_controller.rb b/app/controllers/maglev/editor/section_blocks_controller.rb index f413af0d1..6ac82ad0c 100644 --- a/app/controllers/maglev/editor/section_blocks_controller.rb +++ b/app/controllers/maglev/editor/section_blocks_controller.rb @@ -74,7 +74,7 @@ def sections_store end def update_section_block - @updated_store = services.update_section_block.call( + services.update_section_block.call( store: sections_store, section_id: @section.id, block_id: @section_block.id, diff --git a/app/controllers/maglev/editor/sections_controller.rb b/app/controllers/maglev/editor/sections_controller.rb index d5fc20601..c35ba4e33 100644 --- a/app/controllers/maglev/editor/sections_controller.rb +++ b/app/controllers/maglev/editor/sections_controller.rb @@ -89,7 +89,7 @@ def create_section end def update_section - @updated_store = services.update_section.call( + services.update_section.call( store: sections_store, section_id: @section.id, content: params[:section].to_unsafe_h, diff --git a/app/models/maglev/content/section_content.rb b/app/models/maglev/content/section_content.rb index ef160f892..a8fe9db99 100644 --- a/app/models/maglev/content/section_content.rb +++ b/app/models/maglev/content/section_content.rb @@ -35,13 +35,7 @@ def block_definitions end def root_blocks - # NOTE: a block whose type is root-eligible (root: true, the default) can still be - # nested under another block when its type is also accepted as a child - # (e.g. a navbar menu_item inside another menu_item). Selecting blocks by their - # definition's root? flag would return those nested blocks as well, rendering them - # twice in the editor tree. So we filter on the parent_id instead, like the - # front-end block tree builder does. - blocks.select { |block| block.parent_id.blank? } + blocks.select(&:root?) end # return the definitions of root blocks that can be added diff --git a/app/services/maglev/content/sort_section_blocks_service.rb b/app/services/maglev/content/sort_section_blocks_service.rb index 940ac77aa..5e3647af8 100644 --- a/app/services/maglev/content/sort_section_blocks_service.rb +++ b/app/services/maglev/content/sort_section_blocks_service.rb @@ -42,29 +42,11 @@ def sort_section_blocks!(source) source.save! end - # Only the siblings (the blocks sharing the same parent) are re-ordered, in place. - # The other blocks keep their position in the flat list: sorting the whole list - # with a non-stable sort (Array#sort_by!) used to shuffle the blocks outside of - # the sorted scope, like the children of the other blocks in a tree. def sort_section_blocks(source) - blocks = find_blocks(source) - return if blocks.blank? - - indices = blocks.each_index.select { |index| sibling?(blocks[index]) } - sorted_siblings = sort_siblings(blocks.values_at(*indices)) - - indices.zip(sorted_siblings) { |index, block| blocks[index] = block } - end - - def sort_siblings(siblings) - siblings.sort_by.with_index do |block, position| - [block_ids.index(block['id']) || Float::INFINITY, position] + find_blocks(source)&.sort_by! do |block| + block['parent_id'] == parent_id ? block_ids.index(block['id']) || Float::INFINITY : -1 end end - - def sibling?(block) - block['parent_id'].presence == parent_id.presence - end end end end diff --git a/app/services/maglev/content/update_section_block_service.rb b/app/services/maglev/content/update_section_block_service.rb index 26a9e9fbb..347928673 100644 --- a/app/services/maglev/content/update_section_block_service.rb +++ b/app/services/maglev/content/update_section_block_service.rb @@ -28,9 +28,6 @@ def call private def unsafe_call - # return the store which has been actually written into, so the caller - # can read its fresh lock version (a site scoped section is listed in - # a layout store but its content is written in the site scoped store) if site_scoped? update_section_block_content!(site_scoped_store) else @@ -44,8 +41,6 @@ def update_section_block_content!(source) source.sections_translations_will_change! update_section_block_content(source) source.save! - - source end def update_section_block_content(source) diff --git a/app/services/maglev/content/update_section_service.rb b/app/services/maglev/content/update_section_service.rb index 53c998e7c..a4bac8f5d 100644 --- a/app/services/maglev/content/update_section_service.rb +++ b/app/services/maglev/content/update_section_service.rb @@ -26,9 +26,6 @@ def call private def unsafe_call - # return the store which has been actually written into, so the caller - # can read its fresh lock version (a site scoped section is listed in - # a layout store but its content is written in the site scoped store) if site_scoped? add_missing_site_scoped_section # make sure the section is also present in the site scoped store update_section_content!(site_scoped_store) @@ -43,8 +40,6 @@ def update_section_content!(source) source.sections_translations_will_change! update_section_content(source) source.save! - - source end def update_section_content(source) diff --git a/app/views/maglev/editor/section_blocks/update.turbo_stream.erb b/app/views/maglev/editor/section_blocks/update.turbo_stream.erb index dae6ff104..613844a6a 100644 --- a/app/views/maglev/editor/section_blocks/update.turbo_stream.erb +++ b/app/views/maglev/editor/section_blocks/update.turbo_stream.erb @@ -1,10 +1,13 @@ -<%= turbo_stream.dispatch_event "section-block-#{@section_block.id}:persisted", payload: { lockVersion: @updated_store.lock_version }.to_json %> +<%= turbo_stream.dispatch_event "section-block-#{@section_block.id}:persisted", payload: { lockVersion: source_lock_version }.to_json %> + <%= turbo_stream.update 'page-layout-notification', method: 'morph' do %> <%= render Maglev::Uikit::BadgeComponent.new(color: :green, icon_name: 'checkbox_circle', disappear_after: 3.seconds).with_content(flash.now[:notice]) %> <% end %> + <%= turbo_stream.update dom_id(current_maglev_page, 'topbar-page-info'), method: 'morph' do %> <%= render 'layouts/maglev/editor/topbar/page_info' %> <% end %> + <%= turbo_stream.update dom_id(current_maglev_page, 'topbar-publish-button'), method: 'morph' do %> <%= render 'layouts/maglev/editor/topbar/publish_button' %> -<% end %> +<% end %> \ No newline at end of file diff --git a/app/views/maglev/editor/sections/update.turbo_stream.erb b/app/views/maglev/editor/sections/update.turbo_stream.erb index f270a9d0c..d1ad61bb7 100644 --- a/app/views/maglev/editor/sections/update.turbo_stream.erb +++ b/app/views/maglev/editor/sections/update.turbo_stream.erb @@ -1,10 +1,13 @@ -<%= turbo_stream.dispatch_event "section-#{@section.id}:persisted", payload: { lockVersion: @updated_store.lock_version }.to_json %> +<%= turbo_stream.dispatch_event "section-#{@section.id}:persisted", payload: { lockVersion: source_lock_version }.to_json %> + <%= turbo_stream.update 'page-layout-notification', method: 'morph' do %> <%= render Maglev::Uikit::BadgeComponent.new(color: :green, icon_name: 'checkbox_circle', disappear_after: 3.seconds).with_content(flash.now[:notice]) %> <% end %> + <%= turbo_stream.update dom_id(current_maglev_page, 'topbar-page-info'), method: 'morph' do %> <%= render 'layouts/maglev/editor/topbar/page_info' %> <% end %> + <%= turbo_stream.update dom_id(current_maglev_page, 'topbar-publish-button'), method: 'morph' do %> <%= render 'layouts/maglev/editor/topbar/publish_button' %> -<% end %> +<% end %> \ No newline at end of file diff --git a/spec/components/maglev/section_component_spec.rb b/spec/components/maglev/section_component_spec.rb index 0f5b3b301..1bacae5f7 100644 --- a/spec/components/maglev/section_component_spec.rb +++ b/spec/components/maglev/section_component_spec.rb @@ -36,31 +36,6 @@ end end - describe '#lock_version' do - subject { component.lock_version } - - context 'when the attributes carry no lock version' do - it { is_expected.to eq '0' } - end - - context 'when the attributes carry the lock version of the source store' do - before { attributes[:lock_version] = 42 } - - it { is_expected.to eq 42 } - end - - context 'when the section is site scoped' do - # the lock version stamped on the attributes is the one of the - # site scoped store (where the content lives), not the one of - # the layout store the section is listed in - let(:definition) { build(:section, category: 'headers', site_scoped: true) } - - before { attributes[:lock_version] = 7 } - - it { is_expected.to eq 7 } - end - end - describe '#render' do subject { pretty_html(component.render) } diff --git a/spec/factories/maglev/themes.rb b/spec/factories/maglev/themes.rb index 0d72450e0..96fdfe927 100644 --- a/spec/factories/maglev/themes.rb +++ b/spec/factories/maglev/themes.rb @@ -66,13 +66,11 @@ site_scoped: true, singleton: true, insert_at: 'top', - blocks_presentation: 'tree', settings: [{ label: 'Logo', id: 'logo', type: 'image' }], blocks: [ { name: 'Menu item', type: 'menu_item', - accept: %w[menu_item], settings: [ { label: 'Label', id: 'label', type: 'text', default: 'Menu item' }, { label: 'Link', id: 'link', type: 'link', default: '/' } @@ -155,8 +153,8 @@ { type: 'jumbotron', settings: { - title: "Let's create the product
your clients
will love.", - body: '

NoCoffee, passionated developers,
creators of web applications, mobiles apps and
fancy R&D projects.

' + "title": "Let's create the product
your clients
will love.", + "body": '

NoCoffee, passionated developers,
creators of web applications, mobiles apps and
fancy R&D projects.

' }, blocks: [] }, @@ -192,8 +190,8 @@ { type: 'jumbotron', settings: { - title: 'About our awesome team', - body: '

NoCoffee, passionated developers,
creators of web applications, mobiles apps and
fancy R&D projects.

' + "title": 'About our awesome team', + "body": '

NoCoffee, passionated developers,
creators of web applications, mobiles apps and
fancy R&D projects.

' }, blocks: [] } diff --git a/spec/models/maglev/content/section_content_spec.rb b/spec/models/maglev/content/section_content_spec.rb index 0f74e560a..7e554fa86 100644 --- a/spec/models/maglev/content/section_content_spec.rb +++ b/spec/models/maglev/content/section_content_spec.rb @@ -52,27 +52,4 @@ end end end - - context '#root_blocks' do - let(:store) { build(:sections_content_store, :with_navbar, page: page) } - - it 'returns only the blocks without a parent' do - # the menu_item type is both root-eligible and accepted as a child of itself: - # nested menu items must not be returned here, otherwise the editor tree - # would render them twice (at the root and under their parent) - expect(section_content.root_blocks.map(&:id)).to eq %w[menu-item-0 menu-item-1] - end - end - - context '#child_blocks_of' do - let(:store) { build(:sections_content_store, :with_navbar, page: page) } - - it 'returns the direct children of the given block' do - expect(section_content.child_blocks_of('menu-item-1').map(&:id)).to eq %w[menu-item-1-1 menu-item-1-2] - end - - it 'returns an empty list for a block without children' do - expect(section_content.child_blocks_of('menu-item-0')).to eq [] - end - end end diff --git a/spec/requests/maglev/editor/section_blocks_spec.rb b/spec/requests/maglev/editor/section_blocks_spec.rb index f3553b56b..20fa0a055 100644 --- a/spec/requests/maglev/editor/section_blocks_spec.rb +++ b/spec/requests/maglev/editor/section_blocks_spec.rb @@ -14,71 +14,11 @@ allow(Maglev.local_themes).to receive(:first).and_return(theme) end - # the navbar is a site scoped section presenting its blocks as a tree: - # it is listed in the "header" layout store (its "slot") while its content - # lives in the global site scoped store - shared_context 'with a site scoped section with nested blocks' do - let(:slot_store) { fetch_sections_store('header') } - let(:site_store) { fetch_sections_store('_site') } - let(:navbar_section) { site_store.find_section_by_type('navbar') } - let(:section_id) { navbar_section['id'] } - - # rubocop:disable Style/StringHashKeys - before do - # two root menu items, one of them with a nested menu item - navbar_section['blocks'] = [ - { - 'id' => 'menu-item-0', - 'type' => 'menu_item', - 'settings' => [ - { 'id' => 'label', 'value' => 'Home' }, - { 'id' => 'link', 'value' => '/' } - ] - }, - { - 'id' => 'menu-item-1', - 'type' => 'menu_item', - 'settings' => [ - { 'id' => 'label', 'value' => 'About us' }, - { 'id' => 'link', 'value' => '/about-us' } - ] - }, - { - 'id' => 'nested-menu-item', - 'type' => 'menu_item', - 'parent_id' => 'menu-item-1', - 'settings' => [ - { 'id' => 'label', 'value' => 'Nested item' }, - { 'id' => 'link', 'value' => { 'link_type' => 'url', 'href' => '/nested', 'open_new_window' => false } } - ] - } - ] - site_store.sections_translations_will_change! - site_store.save! - end - # rubocop:enable Style/StringHashKeys - end - describe 'GET /maglev/editor/:context/sections/:id/blocks' do it 'returns a success response' do get "/maglev/editor/en/#{home_page.id}/sections/#{section_id}/blocks" expect(response).to be_successful end - - context 'when the section presents its blocks as a tree' do - include_context 'with a site scoped section with nested blocks' - - it 'renders each block exactly once' do - get "/maglev/editor/en/#{home_page.id}/sections/#{section_id}/blocks" - expect(response).to be_successful - expect(response.body.scan('Nested item').count).to eq 1 - end - - it 'nests the child block under its parent' do - get "/maglev/editor/en/#{home_page.id}/sections/#{section_id}/blocks" - expect(response.body).to include('data-sortable-scope-value="menu-item-1"') - end - end end describe 'POST /maglev/editor/:context/sections/:id/blocks' do @@ -122,42 +62,6 @@ expect(response).to be_successful expect(main_store.reload.sections.dig(1, 'blocks', 0, 'settings', 0, 'value')).to eq 'My new title 🍔' end - - context 'when the section is site scoped' do - include_context 'with a site scoped section with nested blocks' - - let(:block_id) { 'nested-menu-item' } - - before do - # make sure the lock versions of the slot store and the site scoped store differ - # rubocop:disable Rails/SkipsModelValidations - slot_store.update_column(:lock_version, 0) - site_store.update_column(:lock_version, 7) - # rubocop:enable Rails/SkipsModelValidations - end - - it 'renders the form with the lock version of the site scoped store' do - get "/maglev/editor/en/#{home_page.id}/sections/#{section_id}/blocks/#{block_id}/edit" - expect(response.body).to have_selector("input[name='lock_version'][value='7']", visible: :hidden) - end - - it 'updates the site scoped store and dispatches its fresh lock version' do - put "/maglev/editor/en/#{home_page.id}/sections/#{section_id}/blocks/#{block_id}", - as: :turbo_stream, - params: { lock_version: 7, section_block: { label: 'Our dream team' } } - expect(response).to be_successful - expect(response.body).to include('"lockVersion":8') - expect(site_store.reload.lock_version).to eq 8 - expect(slot_store.reload.lock_version).to eq 0 - end - - it 'still rejects a stale lock version' do - put "/maglev/editor/en/#{home_page.id}/sections/#{section_id}/blocks/#{block_id}", - as: :turbo_stream, - params: { lock_version: 6, section_block: { label: 'Our dream team' } } - expect(response).to have_http_status(:conflict) - end - end end describe 'DELETE /maglev/editor/:context/sections/:id/blocks/:id' do diff --git a/spec/requests/maglev/editor/sections_spec.rb b/spec/requests/maglev/editor/sections_spec.rb index c496addb2..348cc9833 100644 --- a/spec/requests/maglev/editor/sections_spec.rb +++ b/spec/requests/maglev/editor/sections_spec.rb @@ -78,44 +78,6 @@ expect(response.media_type).to eq Mime[:turbo_stream] expect(main_store.reload.sections.dig(0, 'settings', 0, 'value')).to eq('Hello world!') end - - context 'when the section is site scoped' do - # a site scoped section is listed in a layout store (its "slot") but its content - # lives in the global site scoped store: the lock version of the latter must be used - let(:slot_store) { fetch_sections_store('header') } - let(:site_store) { fetch_sections_store('_site') } - let(:section_id) { site_store.find_section_by_type('navbar')['id'] } - - before do - # make sure the lock versions of the two stores differ - # rubocop:disable Rails/SkipsModelValidations - slot_store.update_column(:lock_version, 0) - site_store.update_column(:lock_version, 7) - # rubocop:enable Rails/SkipsModelValidations - end - - it 'renders the form with the lock version of the site scoped store' do - get "/maglev/editor/en/#{home_page.id}/sections/#{section_id}/edit" - expect(response.body).to have_selector("input[name='lock_version'][value='7']", visible: :hidden) - end - - it 'updates the site scoped store and dispatches its fresh lock version' do - put "/maglev/editor/en/#{home_page.id}/sections/#{section_id}", - as: :turbo_stream, - params: { lock_version: 7, section: { logo: 'new-logo.png' } } - expect(response).to be_successful - expect(response.body).to include('"lockVersion":8') - expect(site_store.reload.lock_version).to eq 8 - expect(slot_store.reload.lock_version).to eq 0 - end - - it 'still rejects a stale lock version' do - put "/maglev/editor/en/#{home_page.id}/sections/#{section_id}", - as: :turbo_stream, - params: { lock_version: 6, section: { logo: 'new-logo.png' } } - expect(response).to have_http_status(:conflict) - end - end end describe 'DELETE /maglev/editor/:context/sections/:id' do diff --git a/spec/services/maglev/content/sort_section_blocks_service_spec.rb b/spec/services/maglev/content/sort_section_blocks_service_spec.rb index 62dc9aee6..5f24aae25 100644 --- a/spec/services/maglev/content/sort_section_blocks_service_spec.rb +++ b/spec/services/maglev/content/sort_section_blocks_service_spec.rb @@ -76,64 +76,5 @@ end }.to ['Home', 'About us', 'Our office', 'Our team'] end - - context 'when the sorted siblings are interleaved with other blocks' do - # blocks are appended to the flat list as the editor creates them, so the children - # of a menu item are rarely stored right after it - let!(:page) do - create(:page, header_sections: [{ - id: 'abc', - type: 'navbar', - settings: [{ id: :logo, value: 'logo.png' }], - blocks: [ - menu_item('home', 'Home'), - menu_item('about', 'About us'), - menu_item('about-team', 'Our team', parent_id: 'about'), - menu_item('contact', 'Contact'), - menu_item('about-office', 'Our office', parent_id: 'about'), - menu_item('contact-email', 'Email', parent_id: 'contact'), - menu_item('contact-phone', 'Phone', parent_id: 'contact'), - menu_item('blog', 'Blog') - ] - }]) - end - - def menu_item(id, label, parent_id: nil) - { - id: id, - type: 'menu_item', - parent_id: parent_id, - settings: [{ id: :label, value: label }, { id: :link, value: '/' }] - }.compact - end - - def block_ids_in_store - site_scoped_store.reload.sections.dig(0, 'blocks').map { |block| block['id'] } - end - - context 'sorting the root blocks' do - let(:parent_id) { nil } - let(:block_ids) { %w[blog contact about home] } - - it 'only moves the root blocks and leaves the nested blocks where they are' do - subject - expect(block_ids_in_store).to eq( - %w[blog contact about-team about about-office contact-email contact-phone home] - ) - end - end - - context 'sorting the children of a block' do - let(:parent_id) { 'about' } - let(:block_ids) { %w[about-office about-team] } - - it 'only moves the children and leaves the other blocks where they are' do - subject - expect(block_ids_in_store).to eq( - %w[home about about-office contact about-team contact-email contact-phone blog] - ) - end - end - end end end diff --git a/spec/services/maglev/content/update_section_service_spec.rb b/spec/services/maglev/content/update_section_service_spec.rb index 77512a7ae..053f0aaee 100644 --- a/spec/services/maglev/content/update_section_service_spec.rb +++ b/spec/services/maglev/content/update_section_service_spec.rb @@ -22,7 +22,7 @@ let(:lock_version) { 0 } it 'updates the section' do - expect(subject).to be(store) + expect(subject).to eq(true) expect(store.reload.sections.dig(0, 'settings', 0, 'value')).to eq('Hello world!') expect(store.reload.lock_version).to eq(1) end @@ -51,7 +51,7 @@ context 'Given an existing page section with a version' do it 'updates the section' do - expect(subject).to be(store) + expect(subject).to eq(true) end context 'Given the store has been modified while updating the section' do @@ -79,8 +79,7 @@ end it 'updates the section content on the site' do - # the written store is the site scoped one, not the passed (slot) store - expect(subject).to eq(site_scoped_store) + expect(subject).to eq(true) # rubocop:disable Style/StringHashKeys expect(site_scoped_sections.dig(0, 'settings', 0, 'value')).to eq({ 'url' => '/awesome-logo.png' }) # rubocop:enable Style/StringHashKeys From 9edf930be2e2cececffaf51d27cea42f7b0c5780 Mon Sep 17 00:00:00 2001 From: Tamas Erdos Date: Mon, 14 Sep 2026 18:42:34 +0200 Subject: [PATCH 5/5] fix(editor): restore nested sorting, tree rendering, and site-scoped locking Restore the three fixes and their regression coverage removed by 20ca650d. The resulting tree matches 968a334c and retains all current upstream main code. Validation: full RSpec suite on SQLite (483 examples, 0 failures), RuboCop on all 15 changed Ruby files (no offenses), and git diff --check. --- app/components/maglev/section_component.rb | 12 +-- .../maglev/editor/lock_version_concern.rb | 7 +- .../editor/section_blocks_controller.rb | 2 +- .../maglev/editor/sections_controller.rb | 2 +- app/models/maglev/content/section_content.rb | 8 +- .../content/sort_section_blocks_service.rb | 22 ++++- .../content/update_section_block_service.rb | 5 + .../maglev/content/update_section_service.rb | 5 + .../section_blocks/update.turbo_stream.erb | 7 +- .../editor/sections/update.turbo_stream.erb | 7 +- .../maglev/section_component_spec.rb | 25 +++++ spec/factories/maglev/themes.rb | 10 +- .../maglev/content/section_content_spec.rb | 23 +++++ .../maglev/editor/section_blocks_spec.rb | 96 +++++++++++++++++++ spec/requests/maglev/editor/sections_spec.rb | 38 ++++++++ .../sort_section_blocks_service_spec.rb | 59 ++++++++++++ .../content/update_section_service_spec.rb | 7 +- 17 files changed, 305 insertions(+), 30 deletions(-) diff --git a/app/components/maglev/section_component.rb b/app/components/maglev/section_component.rb index 3de31bc6d..830ec6e72 100644 --- a/app/components/maglev/section_component.rb +++ b/app/components/maglev/section_component.rb @@ -4,12 +4,12 @@ # we need to move it along with the PageComponent and BlockComponent # to a separate folder (content?) -# rubocop:disable Metrics/ClassLength module Maglev class SectionComponent < BaseComponent include TagHelper extend Forwardable + def_delegators :parent, :site, :page, :config attr_reader :parent, :id, :type, :settings, :attributes, :definition, :templates_root_path, @@ -35,8 +35,11 @@ def dom_id @dom_id ||= "section-#{id}" end + # the lock version of the store the section content comes from + # (stamped by the sections content fetcher, even for site scoped sections + # whose content lives in the global site scoped store) def lock_version - @lock_version ||= lock_source&.lock_version || '0' + attributes[:lock_version] || '0' end def dom_data @@ -68,10 +71,6 @@ def render private - def lock_source - definition&.site_scoped? ? site : page - end - def build_block_list build_blocks(attributes[:blocks]) end @@ -140,4 +139,3 @@ def inspect_fields end end end -# rubocop:enable Metrics/ClassLength diff --git a/app/controllers/concerns/maglev/editor/lock_version_concern.rb b/app/controllers/concerns/maglev/editor/lock_version_concern.rb index 3b93050c4..117ec293f 100644 --- a/app/controllers/concerns/maglev/editor/lock_version_concern.rb +++ b/app/controllers/concerns/maglev/editor/lock_version_concern.rb @@ -11,8 +11,13 @@ module LockVersionConcern private + # NOTE: we can't always rely on the lock version of the store the section was fetched + # from: a site scoped section is listed in a layout store (its "slot") but its content + # lives in the global site scoped store, which is what the content services update. + # Fortunately, the sections content fetcher stamps each section with the lock version + # of the store its content actually comes from, so we use it whenever possible. def source_lock_version - sections_store.lock_version || 0 + @section ? @section.lock_version : (sections_store.lock_version || 0) end end end diff --git a/app/controllers/maglev/editor/section_blocks_controller.rb b/app/controllers/maglev/editor/section_blocks_controller.rb index 6ac82ad0c..f413af0d1 100644 --- a/app/controllers/maglev/editor/section_blocks_controller.rb +++ b/app/controllers/maglev/editor/section_blocks_controller.rb @@ -74,7 +74,7 @@ def sections_store end def update_section_block - services.update_section_block.call( + @updated_store = services.update_section_block.call( store: sections_store, section_id: @section.id, block_id: @section_block.id, diff --git a/app/controllers/maglev/editor/sections_controller.rb b/app/controllers/maglev/editor/sections_controller.rb index c35ba4e33..d5fc20601 100644 --- a/app/controllers/maglev/editor/sections_controller.rb +++ b/app/controllers/maglev/editor/sections_controller.rb @@ -89,7 +89,7 @@ def create_section end def update_section - services.update_section.call( + @updated_store = services.update_section.call( store: sections_store, section_id: @section.id, content: params[:section].to_unsafe_h, diff --git a/app/models/maglev/content/section_content.rb b/app/models/maglev/content/section_content.rb index a8fe9db99..ef160f892 100644 --- a/app/models/maglev/content/section_content.rb +++ b/app/models/maglev/content/section_content.rb @@ -35,7 +35,13 @@ def block_definitions end def root_blocks - blocks.select(&:root?) + # NOTE: a block whose type is root-eligible (root: true, the default) can still be + # nested under another block when its type is also accepted as a child + # (e.g. a navbar menu_item inside another menu_item). Selecting blocks by their + # definition's root? flag would return those nested blocks as well, rendering them + # twice in the editor tree. So we filter on the parent_id instead, like the + # front-end block tree builder does. + blocks.select { |block| block.parent_id.blank? } end # return the definitions of root blocks that can be added diff --git a/app/services/maglev/content/sort_section_blocks_service.rb b/app/services/maglev/content/sort_section_blocks_service.rb index 5e3647af8..940ac77aa 100644 --- a/app/services/maglev/content/sort_section_blocks_service.rb +++ b/app/services/maglev/content/sort_section_blocks_service.rb @@ -42,11 +42,29 @@ def sort_section_blocks!(source) source.save! end + # Only the siblings (the blocks sharing the same parent) are re-ordered, in place. + # The other blocks keep their position in the flat list: sorting the whole list + # with a non-stable sort (Array#sort_by!) used to shuffle the blocks outside of + # the sorted scope, like the children of the other blocks in a tree. def sort_section_blocks(source) - find_blocks(source)&.sort_by! do |block| - block['parent_id'] == parent_id ? block_ids.index(block['id']) || Float::INFINITY : -1 + blocks = find_blocks(source) + return if blocks.blank? + + indices = blocks.each_index.select { |index| sibling?(blocks[index]) } + sorted_siblings = sort_siblings(blocks.values_at(*indices)) + + indices.zip(sorted_siblings) { |index, block| blocks[index] = block } + end + + def sort_siblings(siblings) + siblings.sort_by.with_index do |block, position| + [block_ids.index(block['id']) || Float::INFINITY, position] end end + + def sibling?(block) + block['parent_id'].presence == parent_id.presence + end end end end diff --git a/app/services/maglev/content/update_section_block_service.rb b/app/services/maglev/content/update_section_block_service.rb index 347928673..26a9e9fbb 100644 --- a/app/services/maglev/content/update_section_block_service.rb +++ b/app/services/maglev/content/update_section_block_service.rb @@ -28,6 +28,9 @@ def call private def unsafe_call + # return the store which has been actually written into, so the caller + # can read its fresh lock version (a site scoped section is listed in + # a layout store but its content is written in the site scoped store) if site_scoped? update_section_block_content!(site_scoped_store) else @@ -41,6 +44,8 @@ def update_section_block_content!(source) source.sections_translations_will_change! update_section_block_content(source) source.save! + + source end def update_section_block_content(source) diff --git a/app/services/maglev/content/update_section_service.rb b/app/services/maglev/content/update_section_service.rb index a4bac8f5d..53c998e7c 100644 --- a/app/services/maglev/content/update_section_service.rb +++ b/app/services/maglev/content/update_section_service.rb @@ -26,6 +26,9 @@ def call private def unsafe_call + # return the store which has been actually written into, so the caller + # can read its fresh lock version (a site scoped section is listed in + # a layout store but its content is written in the site scoped store) if site_scoped? add_missing_site_scoped_section # make sure the section is also present in the site scoped store update_section_content!(site_scoped_store) @@ -40,6 +43,8 @@ def update_section_content!(source) source.sections_translations_will_change! update_section_content(source) source.save! + + source end def update_section_content(source) diff --git a/app/views/maglev/editor/section_blocks/update.turbo_stream.erb b/app/views/maglev/editor/section_blocks/update.turbo_stream.erb index 613844a6a..dae6ff104 100644 --- a/app/views/maglev/editor/section_blocks/update.turbo_stream.erb +++ b/app/views/maglev/editor/section_blocks/update.turbo_stream.erb @@ -1,13 +1,10 @@ -<%= turbo_stream.dispatch_event "section-block-#{@section_block.id}:persisted", payload: { lockVersion: source_lock_version }.to_json %> - +<%= turbo_stream.dispatch_event "section-block-#{@section_block.id}:persisted", payload: { lockVersion: @updated_store.lock_version }.to_json %> <%= turbo_stream.update 'page-layout-notification', method: 'morph' do %> <%= render Maglev::Uikit::BadgeComponent.new(color: :green, icon_name: 'checkbox_circle', disappear_after: 3.seconds).with_content(flash.now[:notice]) %> <% end %> - <%= turbo_stream.update dom_id(current_maglev_page, 'topbar-page-info'), method: 'morph' do %> <%= render 'layouts/maglev/editor/topbar/page_info' %> <% end %> - <%= turbo_stream.update dom_id(current_maglev_page, 'topbar-publish-button'), method: 'morph' do %> <%= render 'layouts/maglev/editor/topbar/publish_button' %> -<% end %> \ No newline at end of file +<% end %> diff --git a/app/views/maglev/editor/sections/update.turbo_stream.erb b/app/views/maglev/editor/sections/update.turbo_stream.erb index d1ad61bb7..f270a9d0c 100644 --- a/app/views/maglev/editor/sections/update.turbo_stream.erb +++ b/app/views/maglev/editor/sections/update.turbo_stream.erb @@ -1,13 +1,10 @@ -<%= turbo_stream.dispatch_event "section-#{@section.id}:persisted", payload: { lockVersion: source_lock_version }.to_json %> - +<%= turbo_stream.dispatch_event "section-#{@section.id}:persisted", payload: { lockVersion: @updated_store.lock_version }.to_json %> <%= turbo_stream.update 'page-layout-notification', method: 'morph' do %> <%= render Maglev::Uikit::BadgeComponent.new(color: :green, icon_name: 'checkbox_circle', disappear_after: 3.seconds).with_content(flash.now[:notice]) %> <% end %> - <%= turbo_stream.update dom_id(current_maglev_page, 'topbar-page-info'), method: 'morph' do %> <%= render 'layouts/maglev/editor/topbar/page_info' %> <% end %> - <%= turbo_stream.update dom_id(current_maglev_page, 'topbar-publish-button'), method: 'morph' do %> <%= render 'layouts/maglev/editor/topbar/publish_button' %> -<% end %> \ No newline at end of file +<% end %> diff --git a/spec/components/maglev/section_component_spec.rb b/spec/components/maglev/section_component_spec.rb index 1bacae5f7..0f5b3b301 100644 --- a/spec/components/maglev/section_component_spec.rb +++ b/spec/components/maglev/section_component_spec.rb @@ -36,6 +36,31 @@ end end + describe '#lock_version' do + subject { component.lock_version } + + context 'when the attributes carry no lock version' do + it { is_expected.to eq '0' } + end + + context 'when the attributes carry the lock version of the source store' do + before { attributes[:lock_version] = 42 } + + it { is_expected.to eq 42 } + end + + context 'when the section is site scoped' do + # the lock version stamped on the attributes is the one of the + # site scoped store (where the content lives), not the one of + # the layout store the section is listed in + let(:definition) { build(:section, category: 'headers', site_scoped: true) } + + before { attributes[:lock_version] = 7 } + + it { is_expected.to eq 7 } + end + end + describe '#render' do subject { pretty_html(component.render) } diff --git a/spec/factories/maglev/themes.rb b/spec/factories/maglev/themes.rb index 96fdfe927..0d72450e0 100644 --- a/spec/factories/maglev/themes.rb +++ b/spec/factories/maglev/themes.rb @@ -66,11 +66,13 @@ site_scoped: true, singleton: true, insert_at: 'top', + blocks_presentation: 'tree', settings: [{ label: 'Logo', id: 'logo', type: 'image' }], blocks: [ { name: 'Menu item', type: 'menu_item', + accept: %w[menu_item], settings: [ { label: 'Label', id: 'label', type: 'text', default: 'Menu item' }, { label: 'Link', id: 'link', type: 'link', default: '/' } @@ -153,8 +155,8 @@ { type: 'jumbotron', settings: { - "title": "Let's create the product
your clients
will love.", - "body": '

NoCoffee, passionated developers,
creators of web applications, mobiles apps and
fancy R&D projects.

' + title: "Let's create the product
your clients
will love.", + body: '

NoCoffee, passionated developers,
creators of web applications, mobiles apps and
fancy R&D projects.

' }, blocks: [] }, @@ -190,8 +192,8 @@ { type: 'jumbotron', settings: { - "title": 'About our awesome team', - "body": '

NoCoffee, passionated developers,
creators of web applications, mobiles apps and
fancy R&D projects.

' + title: 'About our awesome team', + body: '

NoCoffee, passionated developers,
creators of web applications, mobiles apps and
fancy R&D projects.

' }, blocks: [] } diff --git a/spec/models/maglev/content/section_content_spec.rb b/spec/models/maglev/content/section_content_spec.rb index 7e554fa86..0f74e560a 100644 --- a/spec/models/maglev/content/section_content_spec.rb +++ b/spec/models/maglev/content/section_content_spec.rb @@ -52,4 +52,27 @@ end end end + + context '#root_blocks' do + let(:store) { build(:sections_content_store, :with_navbar, page: page) } + + it 'returns only the blocks without a parent' do + # the menu_item type is both root-eligible and accepted as a child of itself: + # nested menu items must not be returned here, otherwise the editor tree + # would render them twice (at the root and under their parent) + expect(section_content.root_blocks.map(&:id)).to eq %w[menu-item-0 menu-item-1] + end + end + + context '#child_blocks_of' do + let(:store) { build(:sections_content_store, :with_navbar, page: page) } + + it 'returns the direct children of the given block' do + expect(section_content.child_blocks_of('menu-item-1').map(&:id)).to eq %w[menu-item-1-1 menu-item-1-2] + end + + it 'returns an empty list for a block without children' do + expect(section_content.child_blocks_of('menu-item-0')).to eq [] + end + end end diff --git a/spec/requests/maglev/editor/section_blocks_spec.rb b/spec/requests/maglev/editor/section_blocks_spec.rb index 20fa0a055..f3553b56b 100644 --- a/spec/requests/maglev/editor/section_blocks_spec.rb +++ b/spec/requests/maglev/editor/section_blocks_spec.rb @@ -14,11 +14,71 @@ allow(Maglev.local_themes).to receive(:first).and_return(theme) end + # the navbar is a site scoped section presenting its blocks as a tree: + # it is listed in the "header" layout store (its "slot") while its content + # lives in the global site scoped store + shared_context 'with a site scoped section with nested blocks' do + let(:slot_store) { fetch_sections_store('header') } + let(:site_store) { fetch_sections_store('_site') } + let(:navbar_section) { site_store.find_section_by_type('navbar') } + let(:section_id) { navbar_section['id'] } + + # rubocop:disable Style/StringHashKeys + before do + # two root menu items, one of them with a nested menu item + navbar_section['blocks'] = [ + { + 'id' => 'menu-item-0', + 'type' => 'menu_item', + 'settings' => [ + { 'id' => 'label', 'value' => 'Home' }, + { 'id' => 'link', 'value' => '/' } + ] + }, + { + 'id' => 'menu-item-1', + 'type' => 'menu_item', + 'settings' => [ + { 'id' => 'label', 'value' => 'About us' }, + { 'id' => 'link', 'value' => '/about-us' } + ] + }, + { + 'id' => 'nested-menu-item', + 'type' => 'menu_item', + 'parent_id' => 'menu-item-1', + 'settings' => [ + { 'id' => 'label', 'value' => 'Nested item' }, + { 'id' => 'link', 'value' => { 'link_type' => 'url', 'href' => '/nested', 'open_new_window' => false } } + ] + } + ] + site_store.sections_translations_will_change! + site_store.save! + end + # rubocop:enable Style/StringHashKeys + end + describe 'GET /maglev/editor/:context/sections/:id/blocks' do it 'returns a success response' do get "/maglev/editor/en/#{home_page.id}/sections/#{section_id}/blocks" expect(response).to be_successful end + + context 'when the section presents its blocks as a tree' do + include_context 'with a site scoped section with nested blocks' + + it 'renders each block exactly once' do + get "/maglev/editor/en/#{home_page.id}/sections/#{section_id}/blocks" + expect(response).to be_successful + expect(response.body.scan('Nested item').count).to eq 1 + end + + it 'nests the child block under its parent' do + get "/maglev/editor/en/#{home_page.id}/sections/#{section_id}/blocks" + expect(response.body).to include('data-sortable-scope-value="menu-item-1"') + end + end end describe 'POST /maglev/editor/:context/sections/:id/blocks' do @@ -62,6 +122,42 @@ expect(response).to be_successful expect(main_store.reload.sections.dig(1, 'blocks', 0, 'settings', 0, 'value')).to eq 'My new title 🍔' end + + context 'when the section is site scoped' do + include_context 'with a site scoped section with nested blocks' + + let(:block_id) { 'nested-menu-item' } + + before do + # make sure the lock versions of the slot store and the site scoped store differ + # rubocop:disable Rails/SkipsModelValidations + slot_store.update_column(:lock_version, 0) + site_store.update_column(:lock_version, 7) + # rubocop:enable Rails/SkipsModelValidations + end + + it 'renders the form with the lock version of the site scoped store' do + get "/maglev/editor/en/#{home_page.id}/sections/#{section_id}/blocks/#{block_id}/edit" + expect(response.body).to have_selector("input[name='lock_version'][value='7']", visible: :hidden) + end + + it 'updates the site scoped store and dispatches its fresh lock version' do + put "/maglev/editor/en/#{home_page.id}/sections/#{section_id}/blocks/#{block_id}", + as: :turbo_stream, + params: { lock_version: 7, section_block: { label: 'Our dream team' } } + expect(response).to be_successful + expect(response.body).to include('"lockVersion":8') + expect(site_store.reload.lock_version).to eq 8 + expect(slot_store.reload.lock_version).to eq 0 + end + + it 'still rejects a stale lock version' do + put "/maglev/editor/en/#{home_page.id}/sections/#{section_id}/blocks/#{block_id}", + as: :turbo_stream, + params: { lock_version: 6, section_block: { label: 'Our dream team' } } + expect(response).to have_http_status(:conflict) + end + end end describe 'DELETE /maglev/editor/:context/sections/:id/blocks/:id' do diff --git a/spec/requests/maglev/editor/sections_spec.rb b/spec/requests/maglev/editor/sections_spec.rb index 348cc9833..c496addb2 100644 --- a/spec/requests/maglev/editor/sections_spec.rb +++ b/spec/requests/maglev/editor/sections_spec.rb @@ -78,6 +78,44 @@ expect(response.media_type).to eq Mime[:turbo_stream] expect(main_store.reload.sections.dig(0, 'settings', 0, 'value')).to eq('Hello world!') end + + context 'when the section is site scoped' do + # a site scoped section is listed in a layout store (its "slot") but its content + # lives in the global site scoped store: the lock version of the latter must be used + let(:slot_store) { fetch_sections_store('header') } + let(:site_store) { fetch_sections_store('_site') } + let(:section_id) { site_store.find_section_by_type('navbar')['id'] } + + before do + # make sure the lock versions of the two stores differ + # rubocop:disable Rails/SkipsModelValidations + slot_store.update_column(:lock_version, 0) + site_store.update_column(:lock_version, 7) + # rubocop:enable Rails/SkipsModelValidations + end + + it 'renders the form with the lock version of the site scoped store' do + get "/maglev/editor/en/#{home_page.id}/sections/#{section_id}/edit" + expect(response.body).to have_selector("input[name='lock_version'][value='7']", visible: :hidden) + end + + it 'updates the site scoped store and dispatches its fresh lock version' do + put "/maglev/editor/en/#{home_page.id}/sections/#{section_id}", + as: :turbo_stream, + params: { lock_version: 7, section: { logo: 'new-logo.png' } } + expect(response).to be_successful + expect(response.body).to include('"lockVersion":8') + expect(site_store.reload.lock_version).to eq 8 + expect(slot_store.reload.lock_version).to eq 0 + end + + it 'still rejects a stale lock version' do + put "/maglev/editor/en/#{home_page.id}/sections/#{section_id}", + as: :turbo_stream, + params: { lock_version: 6, section: { logo: 'new-logo.png' } } + expect(response).to have_http_status(:conflict) + end + end end describe 'DELETE /maglev/editor/:context/sections/:id' do diff --git a/spec/services/maglev/content/sort_section_blocks_service_spec.rb b/spec/services/maglev/content/sort_section_blocks_service_spec.rb index 5f24aae25..62dc9aee6 100644 --- a/spec/services/maglev/content/sort_section_blocks_service_spec.rb +++ b/spec/services/maglev/content/sort_section_blocks_service_spec.rb @@ -76,5 +76,64 @@ end }.to ['Home', 'About us', 'Our office', 'Our team'] end + + context 'when the sorted siblings are interleaved with other blocks' do + # blocks are appended to the flat list as the editor creates them, so the children + # of a menu item are rarely stored right after it + let!(:page) do + create(:page, header_sections: [{ + id: 'abc', + type: 'navbar', + settings: [{ id: :logo, value: 'logo.png' }], + blocks: [ + menu_item('home', 'Home'), + menu_item('about', 'About us'), + menu_item('about-team', 'Our team', parent_id: 'about'), + menu_item('contact', 'Contact'), + menu_item('about-office', 'Our office', parent_id: 'about'), + menu_item('contact-email', 'Email', parent_id: 'contact'), + menu_item('contact-phone', 'Phone', parent_id: 'contact'), + menu_item('blog', 'Blog') + ] + }]) + end + + def menu_item(id, label, parent_id: nil) + { + id: id, + type: 'menu_item', + parent_id: parent_id, + settings: [{ id: :label, value: label }, { id: :link, value: '/' }] + }.compact + end + + def block_ids_in_store + site_scoped_store.reload.sections.dig(0, 'blocks').map { |block| block['id'] } + end + + context 'sorting the root blocks' do + let(:parent_id) { nil } + let(:block_ids) { %w[blog contact about home] } + + it 'only moves the root blocks and leaves the nested blocks where they are' do + subject + expect(block_ids_in_store).to eq( + %w[blog contact about-team about about-office contact-email contact-phone home] + ) + end + end + + context 'sorting the children of a block' do + let(:parent_id) { 'about' } + let(:block_ids) { %w[about-office about-team] } + + it 'only moves the children and leaves the other blocks where they are' do + subject + expect(block_ids_in_store).to eq( + %w[home about about-office contact about-team contact-email contact-phone blog] + ) + end + end + end end end diff --git a/spec/services/maglev/content/update_section_service_spec.rb b/spec/services/maglev/content/update_section_service_spec.rb index 053f0aaee..77512a7ae 100644 --- a/spec/services/maglev/content/update_section_service_spec.rb +++ b/spec/services/maglev/content/update_section_service_spec.rb @@ -22,7 +22,7 @@ let(:lock_version) { 0 } it 'updates the section' do - expect(subject).to eq(true) + expect(subject).to be(store) expect(store.reload.sections.dig(0, 'settings', 0, 'value')).to eq('Hello world!') expect(store.reload.lock_version).to eq(1) end @@ -51,7 +51,7 @@ context 'Given an existing page section with a version' do it 'updates the section' do - expect(subject).to eq(true) + expect(subject).to be(store) end context 'Given the store has been modified while updating the section' do @@ -79,7 +79,8 @@ end it 'updates the section content on the site' do - expect(subject).to eq(true) + # the written store is the site scoped one, not the passed (slot) store + expect(subject).to eq(site_scoped_store) # rubocop:disable Style/StringHashKeys expect(site_scoped_sections.dig(0, 'settings', 0, 'value')).to eq({ 'url' => '/awesome-logo.png' }) # rubocop:enable Style/StringHashKeys