Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions app/components/maglev/section_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -140,4 +139,3 @@ def inspect_fields
end
end
end
# rubocop:enable Metrics/ClassLength
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/maglev/editor/section_blocks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/maglev/editor/sections_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions app/services/maglev/content/update_section_block_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions app/services/maglev/content/update_section_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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 %>
<% end %>
7 changes: 2 additions & 5 deletions app/views/maglev/editor/sections/update.turbo_stream.erb
Original file line number Diff line number Diff line change
@@ -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 %>
<% end %>
25 changes: 25 additions & 0 deletions spec/components/maglev/section_component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }

Expand Down
139 changes: 110 additions & 29 deletions spec/requests/maglev/editor/section_blocks_spec.rb
Original file line number Diff line number Diff line change
@@ -1,84 +1,165 @@
# frozen_string_literal: true

require 'rails_helper'
require "rails_helper"

describe 'Maglev::Editor::SectionBlocks', type: :request do
describe "Maglev::Editor::SectionBlocks", type: :request do
let(:theme) { build(:theme, :predefined_pages) }
let!(:site) { Maglev::GenerateSite.call(theme: theme) }
let!(:home_page) { Maglev::Page.home.first }
let(:main_store) { fetch_sections_store('main', home_page.id) }
let(:section) { main_store.find_section_by_type('showcase') }
let(:section_id) { section['id'] }
let(:main_store) { fetch_sections_store("main", home_page.id) }
let(:section) { main_store.find_section_by_type("showcase") }
let(:section_id) { section["id"] }

before do
allow(Maglev.local_themes).to receive(:first).and_return(theme)
end

describe 'GET /maglev/editor/:context/sections/:id/blocks' do
it 'returns a success response' do
# 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
end

describe 'POST /maglev/editor/:context/sections/:id/blocks' do
it 'returns a redirect response' do
describe "POST /maglev/editor/:context/sections/:id/blocks" do
it "returns a redirect response" do
expect do
post "/maglev/editor/en/#{home_page.id}/sections/#{section_id}/blocks", params: { block_type: 'item' }
post "/maglev/editor/en/#{home_page.id}/sections/#{section_id}/blocks", params: { block_type: "item" }
expect(response).to redirect_to("/maglev/editor/en/#{home_page.id}/sections/#{section_id}/blocks")
end.to change { main_store.reload.sections.dig(1, 'blocks').count }.by(1)
end.to change { main_store.reload.sections.dig(1, "blocks").count }.by(1)
end
end

describe 'GET /maglev/editor/:context/sections/:id/blocks/:id/edit' do
let(:block_id) { section.dig('blocks', 0, 'id') }
it 'returns a success response' do
describe "GET /maglev/editor/:context/sections/:id/blocks/:id/edit" do
let(:block_id) { section.dig("blocks", 0, "id") }
it "returns a success response" do
get "/maglev/editor/en/#{home_page.id}/sections/#{section_id}/blocks/#{block_id}/edit"
expect(response).to be_successful
end

describe "when the section doesn't exist anymore" do
it 'returns a 404 response' do
it "returns a 404 response" do
get "/maglev/editor/en/#{home_page.id}/sections/not_existing/blocks/not_existing/edit"
expect(response).to redirect_to("/maglev/editor/en/#{home_page.id}/sections")
end
end

describe "when the block doesn't exist anymore" do
it 'returns a 404 response' do
it "returns a 404 response" do
get "/maglev/editor/en/#{home_page.id}/sections/#{section_id}/blocks/not_existing/edit"
expect(response).to redirect_to("/maglev/editor/en/#{home_page.id}/sections")
end
end
end

describe 'PUT /maglev/editor/:context/sections/:id/blocks/:id' do
let(:block_id) { section.dig('blocks', 0, 'id') }
it 'returns a success response' do
params = { section_block: { title: 'My new title 🍔' } }
describe "PUT /maglev/editor/:context/sections/:id/blocks/:id" do
let(:block_id) { section.dig("blocks", 0, "id") }
it "returns a success response" do
params = { section_block: { title: "My new title 🍔" } }
put "/maglev/editor/en/#{home_page.id}/sections/#{section_id}/blocks/#{block_id}",
as: :turbo_stream,
params: params
expect(response).to be_successful
expect(main_store.reload.sections.dig(1, 'blocks', 0, 'settings', 0, 'value')).to eq 'My new title 🍔'
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("&quot;lockVersion&quot;: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
let(:block_id) { section.dig('blocks', 0, 'id') }
it 'returns a redirect response' do
describe "DELETE /maglev/editor/:context/sections/:id/blocks/:id" do
let(:block_id) { section.dig("blocks", 0, "id") }
it "returns a redirect response" do
expect do
delete "/maglev/editor/en/#{home_page.id}/sections/#{section_id}/blocks/#{block_id}"
expect(response).to redirect_to("/maglev/editor/en/#{home_page.id}/sections/#{section_id}/blocks")
end.to change { main_store.reload.sections.dig(1, 'blocks').count }.by(-1)
end.to change { main_store.reload.sections.dig(1, "blocks").count }.by(-1)
end
end

describe 'PUT /maglev/editor/:context/sections/:id/blocks/sort' do
it 'returns a success response' do
original_block_ids = section['blocks'].map { |block| block['id'] }
describe "PUT /maglev/editor/:context/sections/:id/blocks/sort" do
it "returns a success response" do
original_block_ids = section["blocks"].map { |block| block["id"] }
put "/maglev/editor/en/#{home_page.id}/sections/#{section_id}/blocks/sort",
params: { item_ids: original_block_ids.reverse, lock_version: section['lock_version'] }
params: { item_ids: original_block_ids.reverse, lock_version: section["lock_version"] }
expect(response).to redirect_to("/maglev/editor/en/#{home_page.id}/sections/#{section_id}/blocks")
expect(main_store.reload.section_block_ids(section_id)).to eq(original_block_ids.reverse)
end
Expand Down
Loading
Loading