Skip to content

Commit aebeebf

Browse files
committed
chore: fix the AddSiteLocale service specs
1 parent 1368b6d commit aebeebf

File tree

6 files changed

+117
-115
lines changed

6 files changed

+117
-115
lines changed

app/models/maglev/page.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@ class Page < ApplicationRecord
66
include Maglev::Translatable
77
include Maglev::Page::PathConcern
88
include Maglev::Page::SearchConcern
9-
include Maglev::SectionsConcern # @deprecated Use SectionsContentStore instead
109

1110
## translations ##
1211
translates :title, presence: true
13-
translates :sections # @deprecated Use SectionsContentStore instead
1412
translates :seo_title, :meta_description
1513
translates :og_title, :og_description, :og_image_url
1614

app/models/maglev/site.rb

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ module Maglev
44
class Site < ApplicationRecord
55
## concerns ##
66
include Maglev::Site::LocalesConcern
7-
include Maglev::Translatable
8-
include Maglev::SectionsConcern # @deprecated Use SectionsContentStore instead
7+
# include Maglev::Translatable
8+
# include Maglev::SectionsConcern # @deprecated Use SectionsContentStore instead
99

1010
## translations ##
11-
translates :sections # @deprecated Use SectionsContentStore instead
11+
# translates :sections # @deprecated Use SectionsContentStore instead
1212

1313
## validations ##
1414
validates :name, presence: true
@@ -18,15 +18,15 @@ def api_attributes
1818
%i[id name]
1919
end
2020

21-
def find_section(type)
22-
ActiveSupport::Deprecation.warn('Not used anymore, replaced by SectionsContentStore')
23-
sections&.find { |section| section['type'] == type }
24-
end
21+
# def find_section(type)
22+
# ActiveSupport::Deprecation.warn('Not used anymore, replaced by SectionsContentStore')
23+
# sections&.find { |section| section['type'] == type }
24+
# end
2525

26-
def translate_in(locale, source_locale)
27-
ActiveSupport::Deprecation.warn('Not used anymore, replaced by SectionsContentStore')
28-
translate_attr_in(:sections, locale, source_locale)
29-
end
26+
# def translate_in(locale, source_locale)
27+
# ActiveSupport::Deprecation.warn('Not used anymore, replaced by SectionsContentStore')
28+
# translate_attr_in(:sections, locale, source_locale)
29+
# end
3030
end
3131
end
3232

app/services/maglev/add_site_locale.rb

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,16 @@ def call
1919
protected
2020

2121
def unsafe_call
22-
# Set a default content for site_scoped sections
23-
apply_to_site
24-
2522
site.save! # persist the new locale
2623

2724
# add a default content in the new locale to all the pages of the site
2825
# based on the default locale. Also take care of the path.
2926
apply_to_pages
3027

31-
true
32-
end
28+
# copy the content of the site to the new locale
29+
apply_to_sections_content_stores
3330

34-
def apply_to_site
35-
Maglev::I18n.with_locale(locale.prefix) do
36-
site.translate_in(locale.prefix, site.default_locale_prefix)
37-
end
31+
true
3832
end
3933

4034
def apply_to_pages
@@ -57,8 +51,21 @@ def default_page_path(page)
5751
page.paths.find_by(locale: site.default_locale.prefix)&.value
5852
end
5953

54+
def apply_to_sections_content_stores
55+
store_resources.find_each do |store|
56+
# set a default content which will be the same as in the default locale
57+
store.translate_in(locale.prefix, site.default_locale_prefix)
58+
59+
store.save!
60+
end
61+
end
62+
6063
def resources
6164
::Maglev::Page
6265
end
66+
67+
def store_resources
68+
::Maglev::SectionsContentStore
69+
end
6370
end
6471
end

spec/factories/maglev/sites.rb

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

8-
trait :empty do
9-
sections { [] }
10-
end
8+
# trait :empty do
9+
# sections { [] }
10+
# end
1111

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
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
4040

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
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
5555

5656
trait :with_style do
5757
style do
@@ -62,49 +62,49 @@
6262
end
6363
end
6464

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
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
7575

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
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
108108
end
109109
end
110110

spec/models/maglev/page/translatable_spec.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
page = create(:page, title: 'Translated page')
1818
Maglev::I18n.with_locale(:es) do
1919
expect(page.title).to be_blank
20-
expect(page.sections).to be_blank
2120
page.title = 'Mi página'
2221
page.path = 'mi-pagina'
2322
page.save!

spec/services/maglev/add_site_locale_spec.rb

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
describe Maglev::AddSiteLocale do
66
subject { service.call(site: site, locale: new_locale) }
77

8-
let(:site) { create(:site, :with_navbar, locales: [{ label: 'English', prefix: 'en' }]) }
8+
let(:site) { create(:site, locales: [{ label: 'English', prefix: 'en' }]) }
99
let(:service) { described_class.new }
1010

1111
describe 'no new locale is passed to the service' do
@@ -27,11 +27,9 @@
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(site.reload.sections).not_to eq nil
31-
page = Maglev::Page.first
32-
expect(page.title).to eq 'Home'
30+
expect(page.reload.title).to eq 'Home'
3331
expect(page.path).to eq 'index'
34-
expect(page.sections).not_to eq nil
32+
expect(Maglev::SectionsContentStore.first.sections).not_to be_empty
3533
end
3634
end
3735
end

0 commit comments

Comments
 (0)