Skip to content

fix: 2 sections can't have the same id #153

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
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
1 change: 1 addition & 0 deletions lib/maglev/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module Maglev
module Errors
class NotAuthorized < StandardError; end
class UnknownSection < StandardError; end
class DuplicateSectionDefinition < StandardError; end

class UnknownSetting < StandardError
def initialize(section_id, block_id, setting_id)
Expand Down
7 changes: 7 additions & 0 deletions lib/maglev/theme_filesystem_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def initialize(fetch_section_screenshot_path)
def call(path)
theme = add(YAML.safe_load(File.read(path.join('theme.yml')), aliases: true))
sections = load_sections(theme, Pathname.new(path).join('sections/**/*.yml'))
detect_duplicate_sections(sections)
theme.sections = Maglev::Section::Store.new(sections)
theme
rescue Errno::ENOENT
Expand Down Expand Up @@ -52,6 +53,12 @@ def find_section_screenshot_timestamp(theme, section)
File.exist?(path) ? File.mtime(path).to_i : nil
end

def detect_duplicate_sections(sections)
sections.group_by(&:id).each do |id, list|
raise Maglev::Errors::DuplicateSectionDefinition, "Duplicate section definition: #{id}" if list.size > 1
end
end

def log_missing_theme_file(path)
# don't log the error if the ruby code is not executed inside
# the Rails console or when the Rails server is running
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Name of the section displayed in the editor UI
name: Section A

category: category_1

# Definition of the settings:
# A setting type can be one of the following values: text, image, checkbox, link and color.
# Please visit: https://docs.maglev.dev/concepts/setting for more explanation.
settings:
- label: "Title"
id: title
type: text
default: "Title"
# html: true
# line_break: true

- label: "Body"
id: body
type: text
html: true
line_break: true
default: "Body"

# Definition of the blocks.
# You can define as many types of blocks as you want.
blocks: []

# By default, in the editor UI, blocks will be listed below the "Content" title.
# The title can be changed with the following property:
# blocks_label: "My list of items"
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Name of the section displayed in the editor UI
name: Section A

category: category_2

# Definition of the settings:
# A setting type can be one of the following values: text, image, checkbox, link and color.
# Please visit: https://docs.maglev.dev/concepts/setting for more explanation.
settings:
- label: "Title"
id: title
type: text
default: "Title"
# html: true
# line_break: true

# Definition of the blocks.
# You can define as many types of blocks as you want.
blocks: []

# By default, in the editor UI, blocks will be listed below the "Content" title.
# The title can be changed with the following property:
# blocks_label: "My list of items"
10 changes: 10 additions & 0 deletions spec/fixtures/themes/theme_with_sections_with_same_id/theme.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
id: default
name: Default

section_categories:
- id: category_1
name: "Category #1"
- id: category_2
name: "Category #2"

style_settings: []
21 changes: 21 additions & 0 deletions spec/lib/maglev/theme_filesystem_loader_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

require 'rails_helper'

describe Maglev::ThemeFilesystemLoader do
describe '#call' do
# rubocop:disable Lint/UnusedBlockArgument
let(:fetch_section_screenshot_path) { ->(theme:, section:, absolute:) { path } }
# rubocop:enable Lint/UnusedBlockArgument

subject { described_class.new(fetch_section_screenshot_path).call(Pathname.new(path)) }

context 'There are 2 sections with the same id in different categories' do
let(:path) { File.expand_path('./spec/fixtures/themes/theme_with_sections_with_same_id') }

it 'raises a DuplicateSectionDefinition error' do
expect { subject }.to raise_error(Maglev::Errors::DuplicateSectionDefinition)
end
end
end
end