Skip to content

Commit c63e192

Browse files
committed
chore: the notion of site or page sections don't exist anymore, adapt the rename section type + remove section type services
1 parent d754ac7 commit c63e192

File tree

8 files changed

+77
-170
lines changed

8 files changed

+77
-170
lines changed

app/services/maglev/remove_section_type.rb

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@ def call
2020

2121
def remove_sections
2222
ActiveRecord::Base.transaction do
23-
remove_resource_sections(site)
24-
site_pages.find_each do |page|
25-
remove_resource_sections(page)
23+
resources.find_each do |resource|
24+
remove_resource_sections(resource)
2625
end
2726
end
2827
end
@@ -43,8 +42,8 @@ def remove_sections_of_type(sections, type)
4342
original_size - sections.size
4443
end
4544

46-
def site_pages
47-
Maglev::Page
45+
def resources
46+
Maglev::SectionsContentStore.all
4847
end
4948
end
5049
end

app/services/maglev/rename_section_type.rb

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@ def call
1414
validate_section_types!
1515

1616
ActiveRecord::Base.transaction do
17-
rename_resource_sections(site)
18-
site_pages.find_each do |page|
19-
rename_resource_sections(page)
17+
resources.find_each do |resource|
18+
rename_resource_sections(resource)
2019
end
2120
end
2221

@@ -50,8 +49,8 @@ def change_section_type(sections, old_type, new_type)
5049
end
5150
end
5251

53-
def site_pages
54-
Maglev::Page
52+
def resources
53+
Maglev::SectionsContentStore.all
5554
end
5655
end
5756
end

spec/factories/maglev/sections_content_stores.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
FactoryBot.define do
44
factory :sections_content_store, class: 'Maglev::SectionsContentStore' do
5-
transient do
6-
page { nil }
7-
end
5+
page { nil }
86

97
handle { "main#{page ? "-#{page.id}" : ''}" }
108

spec/factories/maglev/sites.rb

Lines changed: 0 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -5,54 +5,6 @@
55
name { 'My awesome site' }
66
locales { [{ label: 'English', prefix: 'en' }, { label: 'French', prefix: 'fr' }] }
77

8-
# trait :empty do
9-
# sections { [] }
10-
# end
11-
12-
# trait :with_navbar do
13-
# sections do
14-
# [
15-
# {
16-
# type: 'navbar',
17-
# id: 'yyy',
18-
# settings: [
19-
# { id: :logo, value: 'mynewlogo.png' }
20-
# ],
21-
# blocks: [
22-
# {
23-
# type: 'menu_item',
24-
# id: 'zzz',
25-
# settings: [
26-
# { id: 'label', value: 'Home' },
27-
# {
28-
# id: 'link',
29-
# value: {
30-
# link_type: 'url', open_new_window: true, href: 'https://www.nocoffee.fr'
31-
# }
32-
# }
33-
# ]
34-
# }
35-
# ]
36-
# }
37-
# ]
38-
# end
39-
# end
40-
41-
# trait :with_footer do
42-
# sections do
43-
# [
44-
# {
45-
# type: 'footer',
46-
# id: 'footer',
47-
# settings: [
48-
# { id: :copyright, value: '(c) 2022 NoCoffee SARL' }
49-
# ],
50-
# blocks: []
51-
# }
52-
# ]
53-
# end
54-
# end
55-
568
trait :with_style do
579
style do
5810
[
@@ -61,50 +13,6 @@
6113
]
6214
end
6315
end
64-
65-
# trait :page_links do
66-
# after :build do |record|
67-
# record.find_section('navbar')['blocks'][0]['settings'][1]['value'] = {
68-
# link_type: 'page',
69-
# link_id: '42',
70-
# open_new_window: true,
71-
# href: '/path-to-something'
72-
# }
73-
# end
74-
# end
75-
76-
# trait :with_preset_navbar do
77-
# sections do
78-
# [
79-
# {
80-
# type: 'navbar',
81-
# settings: [
82-
# { id: :logo, value: 'mynewlogo.png' }
83-
# ],
84-
# blocks: [
85-
# {
86-
# type: 'menu_group',
87-
# settings: [],
88-
# children: [
89-
# {
90-
# type: 'menu_item',
91-
# settings: [
92-
# { id: 'label', value: 'Home' },
93-
# {
94-
# id: 'link',
95-
# value: {
96-
# link_type: 'url', open_new_window: true, href: 'https://www.nocoffee.fr'
97-
# }
98-
# }
99-
# ]
100-
# }
101-
# ]
102-
# }
103-
# ]
104-
# }
105-
# ]
106-
# end
107-
# end
10816
end
10917
end
11018

spec/models/maglev/page/path_spec.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,8 @@
2626
end
2727

2828
it "doesn't allow creating a page with a path which is not a valid path" do
29-
page = described_class.new(title: 'Hello world', path: 'foo bar')
29+
page = described_class.new(title: 'Hello world', path: 'foo bar', layout_id: 'default')
3030
expect(page).to be_invalid
31-
pp page.errors
3231
expect(page.errors.full_messages).to eq(['Path is not a valid path'])
3332
end
3433
end

spec/services/maglev/add_site_locale_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
it 'sets a default content to all the pages in the new locale' do
2828
subject
2929
Maglev::I18n.with_locale(:fr) do
30-
expect(page.reload.title).to eq 'Home'
30+
page = Maglev::Page.first
31+
expect(page.title).to eq 'Home'
3132
expect(page.path).to eq 'index'
3233
expect(Maglev::SectionsContentStore.first.sections).not_to be_empty
3334
end

spec/services/maglev/remove_section_type_spec.rb

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,21 @@
66
subject { described_class.call(site: site, type: type) }
77

88
let(:site) { create(:site, locales: [{ label: 'English', prefix: 'en' }, { label: 'French', prefix: 'fr' }]) }
9+
let(:global_store) { create(:sections_content_store) }
910
let(:type) { 'hero' }
1011

11-
describe 'when the site has sections of the specified type' do
12+
describe 'when the site has global sections of the specified type' do
1213
before do
13-
site.update!(sections_translations: {
14-
en: [
15-
{ type: 'hero', settings: [{ value: 'Hello' }] },
16-
{ type: 'footer', settings: [{ value: 'Footer' }] }
17-
],
18-
fr: [
19-
{ type: 'hero', settings: [{ value: 'Bonjour' }] },
20-
{ type: 'footer', settings: [{ value: 'Pied de page' }] }
21-
]
22-
})
14+
global_store.update!(sections_translations: {
15+
en: [
16+
{ type: 'hero', settings: [{ value: 'Hello' }] },
17+
{ type: 'footer', settings: [{ value: 'Footer' }] }
18+
],
19+
fr: [
20+
{ type: 'hero', settings: [{ value: 'Bonjour' }] },
21+
{ type: 'footer', settings: [{ value: 'Pied de page' }] }
22+
]
23+
})
2324
end
2425

2526
it 'removes the sections of the specified type in all locales and returns the count' do
@@ -28,15 +29,15 @@
2829

2930
# Check English locale
3031
Maglev::I18n.with_locale(:en) do
31-
sections = site.reload.sections
32+
sections = global_store.reload.sections
3233
expect(sections.length).to eq(1)
3334
expect(sections.first['type']).to eq('footer')
3435
expect(sections.first['settings'].first['value']).to eq('Footer')
3536
end
3637

3738
# Check French locale
3839
Maglev::I18n.with_locale(:fr) do
39-
sections = site.reload.sections
40+
sections = global_store.reload.sections
4041
expect(sections.length).to eq(1)
4142
expect(sections.first['type']).to eq('footer')
4243
expect(sections.first['settings'].first['value']).to eq('Pied de page')
@@ -46,26 +47,26 @@
4647

4748
describe 'when the site has no sections of the specified type' do
4849
before do
49-
site.update!(sections_translations: {
50-
en: [{ type: 'footer', settings: [{ value: 'Footer' }] }],
51-
fr: [{ type: 'footer', settings: [{ value: 'Pied de page' }] }]
52-
})
50+
global_store.update!(sections_translations: {
51+
en: [{ type: 'footer', settings: [{ value: 'Footer' }] }],
52+
fr: [{ type: 'footer', settings: [{ value: 'Pied de page' }] }]
53+
})
5354
end
5455

5556
it 'does not modify the sections and returns zero' do
5657
expect(subject).to eq(0)
5758

5859
# Check English locale
5960
Maglev::I18n.with_locale(:en) do
60-
sections = site.reload.sections
61+
sections = global_store.reload.sections
6162
expect(sections.length).to eq(1)
6263
expect(sections.first['type']).to eq('footer')
6364
expect(sections.first['settings'].first['value']).to eq('Footer')
6465
end
6566

6667
# Check French locale
6768
Maglev::I18n.with_locale(:fr) do
68-
sections = site.reload.sections
69+
sections = global_store.reload.sections
6970
expect(sections.length).to eq(1)
7071
expect(sections.first['type']).to eq('footer')
7172
expect(sections.first['settings'].first['value']).to eq('Pied de page')
@@ -74,19 +75,20 @@
7475
end
7576

7677
describe 'when removing all sections of a type' do
77-
let!(:page) { create(:page) }
78+
let(:page) { create(:page) }
79+
let(:page_store) { page.stores.first }
7880

7981
before do
8082
# Set up sections for both site and page
81-
site.update!(sections_translations: {
82-
en: [{ type: 'hero' }, { type: 'footer' }],
83-
fr: [{ type: 'hero' }, { type: 'footer' }]
84-
})
85-
86-
page.update!(sections_translations: {
87-
en: [{ type: 'hero' }, { type: 'banner' }],
88-
fr: [{ type: 'hero' }, { type: 'banner' }]
89-
})
83+
global_store.update!(sections_translations: {
84+
en: [{ type: 'hero' }, { type: 'footer' }],
85+
fr: [{ type: 'hero' }, { type: 'footer' }]
86+
})
87+
88+
page_store.update!(sections_translations: {
89+
en: [{ type: 'hero' }, { type: 'banner' }],
90+
fr: [{ type: 'hero' }, { type: 'banner' }]
91+
})
9092
end
9193

9294
it 'removes the sections from both site and pages and returns total count' do
@@ -95,12 +97,12 @@
9597

9698
# Check site sections
9799
Maglev::I18n.with_locale(:en) do
98-
expect(site.reload.sections.map { |s| s['type'] }).to eq(['footer'])
100+
expect(global_store.reload.sections.map { |s| s['type'] }).to eq(['footer'])
99101
end
100102

101103
# Check page sections
102104
Maglev::I18n.with_locale(:en) do
103-
expect(page.reload.sections.map { |s| s['type'] }).to eq(['banner'])
105+
expect(page_store.reload.sections.map { |s| s['type'] }).to eq(['banner'])
104106
end
105107
end
106108
end

0 commit comments

Comments
 (0)