Skip to content

Commit e01cb55

Browse files
committed
fix: raise an exception if 2 sections in different categories share the same id
1 parent 90f4fab commit e01cb55

File tree

6 files changed

+92
-0
lines changed

6 files changed

+92
-0
lines changed

lib/maglev/errors.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module Maglev
44
module Errors
55
class NotAuthorized < StandardError; end
66
class UnknownSection < StandardError; end
7+
class DuplicateSectionDefinition < StandardError; end
78

89
class UnknownSetting < StandardError
910
def initialize(section_id, block_id, setting_id)

lib/maglev/theme_filesystem_loader.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ def initialize(fetch_section_screenshot_path)
1111
def call(path)
1212
theme = add(YAML.safe_load(File.read(path.join('theme.yml')), aliases: true))
1313
sections = load_sections(theme, Pathname.new(path).join('sections/**/*.yml'))
14+
detect_duplicate_sections(sections)
1415
theme.sections = Maglev::Section::Store.new(sections)
1516
theme
1617
rescue Errno::ENOENT
@@ -52,6 +53,12 @@ def find_section_screenshot_timestamp(theme, section)
5253
File.exist?(path) ? File.mtime(path).to_i : nil
5354
end
5455

56+
def detect_duplicate_sections(sections)
57+
sections.group_by(&:id).each do |id, list|
58+
raise Maglev::Errors::DuplicateSectionDefinition, "Duplicate section definition: #{id}" if list.size > 1
59+
end
60+
end
61+
5562
def log_missing_theme_file(path)
5663
# don't log the error if the ruby code is not executed inside
5764
# the Rails console or when the Rails server is running
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Name of the section displayed in the editor UI
2+
name: Section A
3+
4+
category: category_1
5+
6+
# Definition of the settings:
7+
# A setting type can be one of the following values: text, image, checkbox, link and color.
8+
# Please visit: https://docs.maglev.dev/concepts/setting for more explanation.
9+
settings:
10+
- label: "Title"
11+
id: title
12+
type: text
13+
default: "Title"
14+
# html: true
15+
# line_break: true
16+
17+
- label: "Body"
18+
id: body
19+
type: text
20+
html: true
21+
line_break: true
22+
default: "Body"
23+
24+
# Definition of the blocks.
25+
# You can define as many types of blocks as you want.
26+
blocks: []
27+
28+
# By default, in the editor UI, blocks will be listed below the "Content" title.
29+
# The title can be changed with the following property:
30+
# blocks_label: "My list of items"
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Name of the section displayed in the editor UI
2+
name: Section A
3+
4+
category: category_2
5+
6+
# Definition of the settings:
7+
# A setting type can be one of the following values: text, image, checkbox, link and color.
8+
# Please visit: https://docs.maglev.dev/concepts/setting for more explanation.
9+
settings:
10+
- label: "Title"
11+
id: title
12+
type: text
13+
default: "Title"
14+
# html: true
15+
# line_break: true
16+
17+
# Definition of the blocks.
18+
# You can define as many types of blocks as you want.
19+
blocks: []
20+
21+
# By default, in the editor UI, blocks will be listed below the "Content" title.
22+
# The title can be changed with the following property:
23+
# blocks_label: "My list of items"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
id: default
2+
name: Default
3+
4+
section_categories:
5+
- id: category_1
6+
name: "Category #1"
7+
- id: category_2
8+
name: "Category #2"
9+
10+
style_settings: []
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# frozen_string_literal: true
2+
3+
require 'rails_helper'
4+
5+
describe Maglev::ThemeFilesystemLoader do
6+
describe '#call' do
7+
# rubocop:disable Lint/UnusedBlockArgument
8+
let(:fetch_section_screenshot_path) { ->(theme:, section:, absolute:) { path } }
9+
# rubocop:enable Lint/UnusedBlockArgument
10+
11+
subject { described_class.new(fetch_section_screenshot_path).call(Pathname.new(path)) }
12+
13+
context 'There are 2 sections with the same id in different categories' do
14+
let(:path) { File.expand_path('./spec/fixtures/themes/theme_with_sections_with_same_id') }
15+
16+
it 'raises a DuplicateSectionDefinition error' do
17+
expect { subject }.to raise_error(Maglev::Errors::DuplicateSectionDefinition)
18+
end
19+
end
20+
end
21+
end

0 commit comments

Comments
 (0)