Skip to content

Commit 83b122b

Browse files
committed
feat: allow to pass a lambda to set the title of the editor window
1 parent 4c80774 commit 83b122b

File tree

6 files changed

+18
-41
lines changed

6 files changed

+18
-41
lines changed

app/helpers/maglev/editor_helper.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,14 @@ def api_base_path
2020
end
2121

2222
def editor_window_title
23-
maglev_config.title.presence || 'Maglev - EDITOR'
23+
case maglev_config.title
24+
when nil
25+
'Maglev - EDITOR'
26+
when String
27+
maglev_config.title
28+
when Proc
29+
instance_exec(maglev_site, &maglev_config.title)
30+
end
2431
end
2532

2633
def editor_primary_hex_color

app/services/maglev/persist_page.rb

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ def call
1616
ActiveRecord::Base.transaction do
1717
persist_page!
1818
persist_site!
19-
persist_repositories!
2019
persist_style!
2120
page
2221
end
@@ -46,26 +45,6 @@ def persist_site!
4645
site.save!
4746
end
4847

49-
def persist_repositories!
50-
scoped_sections = page_attributes[:sections].each do |section_attributes|
51-
type = section_attributes[:type]
52-
scope = theme.find_scope(type)
53-
next if scope.blank?
54-
55-
# section 1 (scope = dogs)
56-
# section 2
57-
# section 3 (scope = dogs)
58-
# section 4 (scope = cats)
59-
60-
repository = Maglev::SectionRepository.find_or_initialize_by(name: scope)
61-
repository.attributes = { sections: [section_attributes] }
62-
63-
repository.prepare_sections(theme)
64-
65-
repository.save!
66-
end
67-
end
68-
6948
def persist_style!
7049
return if site_attributes&.dig(:style).blank?
7150

lib/generators/maglev/templates/install/config/initializers/maglev.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
Maglev.configure do |config|
44
# Title of the Editor window
55
# config.title = 'Maglev - Editor'
6+
# config.title = ->(site) { "#{site.name} 👋" }
67

78
# Logo of the Editor (top left corner).
89
# Put your custom logo in the app/assets/images folder of your Rails application.

spec/factories/maglev/themes.rb

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,6 @@
8686
site_scoped: true,
8787
settings: [{ label: 'Copyright', id: 'copyright', type: 'text' }],
8888
blocks: []
89-
}.with_indifferent_access),
90-
Maglev::Section.build({
91-
theme: theme,
92-
name: 'Ads',
93-
id: 'ads',
94-
category: 'features',
95-
scope: 'shoes',
96-
settings: [{ id: 'title', type: 'text' }],
97-
blocks: []
9889
}.with_indifferent_access)
9990
])
10091
end

spec/helpers/maglev/editor_helper_spec.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,21 @@ def maglev_site; end
3434
expect(subject).to eq 'Maglev - EDITOR'
3535
end
3636

37-
context 'the developer has set a custom title' do
37+
context 'the developer has set a custom title (String)' do
3838
let(:title) { 'My awesome CMS' }
3939

4040
it 'returns the new title' do
4141
expect(subject).to eq 'My awesome CMS'
4242
end
4343
end
44+
45+
context 'the developer has set a custom title (Proc)' do
46+
let(:title) { ->(site) { "#{site.name}!" } }
47+
48+
it 'returns the new title' do
49+
expect(subject).to eq 'My awesome site!'
50+
end
51+
end
4452
end
4553

4654
describe '#editor_primary_hex_color' do

spec/services/maglev/persist_page_spec.rb

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,6 @@
3030
end
3131
end
3232

33-
context 'Given one of the sections is tied to a repository' do
34-
let!(:page) { create(:page) }
35-
let(:page_attributes) { attributes_for(:page, :with_ads) }
36-
37-
it 'creates a section repository in the DB (since the ads section belongs to a scope)' do
38-
expect { subject }.to change(Maglev::Page, :count).by(0).and change(Maglev::SectionRepository, :count).by(1)
39-
end
40-
end
41-
4233
context 'Given the site attributes are not empty' do
4334
let(:page) { create(:page) }
4435
let(:page_attributes) { attributes_for(:page, :with_navbar) }

0 commit comments

Comments
 (0)