Skip to content

Commit 5fbdcf8

Browse files
authored
Merge pull request #2108 from tf/password-protected-translations
Make filtering of entry translations menu less restrictive
2 parents a1b25b1 + 3c26582 commit 5fbdcf8

File tree

9 files changed

+168
-14
lines changed

9 files changed

+168
-14
lines changed

app/models/pageflow/draft_entry.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def entry_title
1414
entry.title
1515
end
1616

17-
def translations(scope = -> { self })
17+
def translations(scope = -> { self }, **)
1818
return [] unless entry.translation_group
1919

2020
PublishedEntry.wrap_all_drafts(

app/models/pageflow/entry_translation_group.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,26 @@ class EntryTranslationGroup < ApplicationRecord
66
foreign_key: 'translation_group_id',
77
dependent: :nullify
88

9+
has_many :published_entries,
10+
-> { published },
11+
foreign_key: 'translation_group_id',
12+
class_name: 'Entry'
13+
914
has_many :publicly_visible_entries,
1015
-> { published_without_password_protection.published_without_noindex },
1116
foreign_key: 'translation_group_id',
1217
class_name: 'Entry'
1318

19+
has_many :entries_published_without_password_protection,
20+
-> { published_without_password_protection },
21+
foreign_key: 'translation_group_id',
22+
class_name: 'Entry'
23+
24+
has_many :entries_published_without_noindex,
25+
-> { published_without_noindex },
26+
foreign_key: 'translation_group_id',
27+
class_name: 'Entry'
28+
1429
belongs_to :default_translation,
1530
class_name: 'Entry',
1631
optional: true

app/models/pageflow/published_entry.rb

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ def title
2020
revision.title.presence || entry.title
2121
end
2222

23-
def translations(scope = -> { self })
23+
def translations(scope = -> { self }, include_noindex: false)
2424
return [] unless entry.translation_group
2525

2626
if published_revision?
2727
self.class.wrap_all(
28-
entry.translation_group.publicly_visible_entries.instance_exec(&scope)
28+
published_translation_entries(include_noindex:).instance_exec(&scope)
2929
)
3030
else
3131
self.class.wrap_all_drafts(
@@ -99,6 +99,20 @@ def published_revision?
9999

100100
private
101101

102+
def published_translation_entries(include_noindex:)
103+
translation_group = entry.translation_group
104+
105+
if password_protected? && include_noindex
106+
translation_group.published_entries
107+
elsif password_protected?
108+
translation_group.entries_published_without_noindex
109+
elsif include_noindex
110+
translation_group.entries_published_without_password_protection
111+
else
112+
translation_group.publicly_visible_entries
113+
end
114+
end
115+
102116
def custom_revision?
103117
@custom_revision
104118
end

entry_types/scrolled/app/helpers/pageflow_scrolled/entry_json_seed_helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def scrolled_entry_json_seed(json, scrolled_entry, options = {})
3333
sections: main_storyline.sections,
3434
content_elements: main_storyline.content_elements,
3535
widgets: scrolled_entry.resolve_widgets(insert_point: :react),
36-
options: options)
36+
options:)
3737
end
3838
end
3939
end

entry_types/scrolled/app/views/pageflow_scrolled/entry_json_seed/_entry_translations.json.jbuilder

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
json.entry_translations do
2-
json.array!(entry.translations(-> { preload(:site) })) do |translation|
2+
json.array!(entry.translations(-> { preload(:site) }, include_noindex: true)) do |translation|
33
json.(translation, :id, :locale)
44
json.display_locale t('pageflow.public._language', locale: translation.locale)
55

entry_types/scrolled/spec/helpers/pageflow_scrolled/entry_json_seed_helper_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,7 @@ def render(helper, entry, options = {})
746746
end
747747

748748
context 'entry translations' do
749-
it 'renders links to published translations of published entry' do
749+
it 'renders links to published translations of published entry including noindex entries' do
750750
de_entry = create(
751751
:published_entry,
752752
type_name: 'scrolled',
@@ -755,6 +755,7 @@ def render(helper, entry, options = {})
755755
)
756756
en_entry = create(
757757
:published_entry,
758+
:published_with_noindex,
758759
translation_of: de_entry,
759760
type_name: 'scrolled',
760761
title: 'entry-en',

spec/factories/published_entries.rb

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,29 @@ module Pageflow
1313
without_feature { nil }
1414

1515
translation_of { nil }
16+
17+
entry_trait { :published }
18+
end
19+
20+
trait :published_with_password do
21+
entry_trait { :published_with_password }
22+
end
23+
24+
trait :published_with_noindex do
25+
entry_trait { :published_with_noindex }
1626
end
1727

1828
initialize_with do
1929
PublishedEntry.new(create(:entry,
20-
:published,
21-
title: title,
22-
account: account,
23-
site: site,
24-
type_name: type_name,
30+
entry_trait,
31+
title:,
32+
account:,
33+
site:,
34+
type_name:,
2535
published_revision_attributes: revision_attributes,
26-
permalink_attributes: permalink_attributes,
27-
with_feature: with_feature,
28-
without_feature: without_feature))
36+
permalink_attributes:,
37+
with_feature:,
38+
without_feature:))
2939
end
3040

3141
to_create { |published_entry| published_entry.entry.save! }

spec/models/pageflow/draft_entry_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,19 @@ module Pageflow
2828

2929
expect(result[0].entry.association(:permalink).loaded?).to eq(true)
3030
end
31+
32+
it 'ignores include_noindex argument' do
33+
entry = create(:entry)
34+
translation = create(:entry)
35+
entry.mark_as_translation_of(translation)
36+
draft_entry = DraftEntry.new(entry)
37+
38+
result = draft_entry.translations(include_noindex: true)
39+
40+
expect(result.length).to eq(2)
41+
expect(result[0].title).to eq(entry.title)
42+
expect(result[1].title).to eq(translation.title)
43+
end
3144
end
3245

3346
describe '#find_files' do

spec/models/pageflow/published_entry_spec.rb

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,31 @@ module Pageflow
253253
expect(result[0].title).to eq(entry.title)
254254
end
255255

256+
it 'does not filter out password protected entries if entry is published with password' do
257+
entry = create(:entry, :published_with_password)
258+
translation = create(:entry, :published_with_password)
259+
entry.mark_as_translation_of(translation)
260+
published_entry = PublishedEntry.new(entry)
261+
262+
result = published_entry.translations
263+
264+
expect(result.length).to eq(2)
265+
expect(result[0].title).to eq(entry.title)
266+
expect(result[1].title).to eq(translation.title)
267+
end
268+
269+
it 'filters out non-published entries if entry is published with password' do
270+
entry = create(:entry, :published_with_password)
271+
translation = create(:entry)
272+
entry.mark_as_translation_of(translation)
273+
published_entry = PublishedEntry.new(entry)
274+
275+
result = published_entry.translations
276+
277+
expect(result.length).to eq(1)
278+
expect(result[0].title).to eq(entry.title)
279+
end
280+
256281
it 'filters out noindex entries' do
257282
entry = create(:entry, :published)
258283
translation = create(:entry, :published_with_noindex)
@@ -265,6 +290,82 @@ module Pageflow
265290
expect(result[0].title).to eq(entry.title)
266291
end
267292

293+
it 'filters out noindex entries if entry is published with password' do
294+
entry = create(:entry, :published_with_password)
295+
translation = create(:entry, :published_with_noindex)
296+
entry.mark_as_translation_of(translation)
297+
published_entry = PublishedEntry.new(entry)
298+
299+
result = published_entry.translations
300+
301+
expect(result.length).to eq(1)
302+
expect(result[0].title).to eq(entry.title)
303+
end
304+
305+
describe 'with include_noindex' do
306+
it 'includes noindex entries' do
307+
entry = create(:entry, :published)
308+
translation = create(:entry, :published_with_noindex)
309+
entry.mark_as_translation_of(translation)
310+
published_entry = PublishedEntry.new(entry)
311+
312+
result = published_entry.translations(include_noindex: true)
313+
314+
expect(result.length).to eq(2)
315+
expect(result[0].title).to eq(entry.title)
316+
expect(result[1].title).to eq(translation.title)
317+
end
318+
319+
it 'filters out non-published entries' do
320+
entry = create(:entry, :published)
321+
translation = create(:entry)
322+
entry.mark_as_translation_of(translation)
323+
published_entry = PublishedEntry.new(entry)
324+
325+
result = published_entry.translations(include_noindex: true)
326+
327+
expect(result.length).to eq(1)
328+
expect(result[0].title).to eq(entry.title)
329+
end
330+
331+
it 'filters out password protected entries' do
332+
entry = create(:entry, :published)
333+
translation = create(:entry, :published_with_password)
334+
entry.mark_as_translation_of(translation)
335+
published_entry = PublishedEntry.new(entry)
336+
337+
result = published_entry.translations(include_noindex: true)
338+
339+
expect(result.length).to eq(1)
340+
expect(result[0].title).to eq(entry.title)
341+
end
342+
343+
it 'does not filter out password protected entries if entry is published with password' do
344+
entry = create(:entry, :published_with_password)
345+
translation = create(:entry, :published_with_password)
346+
entry.mark_as_translation_of(translation)
347+
published_entry = PublishedEntry.new(entry)
348+
349+
result = published_entry.translations(include_noindex: true)
350+
351+
expect(result.length).to eq(2)
352+
expect(result[0].title).to eq(entry.title)
353+
expect(result[1].title).to eq(translation.title)
354+
end
355+
356+
it 'filters out non-published entries for password protected entry' do
357+
entry = create(:entry, :published_with_password)
358+
translation = create(:entry)
359+
entry.mark_as_translation_of(translation)
360+
published_entry = PublishedEntry.new(entry)
361+
362+
result = published_entry.translations(include_noindex: true)
363+
364+
expect(result.length).to eq(1)
365+
expect(result[0].title).to eq(entry.title)
366+
end
367+
end
368+
268369
it 'supports using drafts' do
269370
entry = create(:entry, :published)
270371
published_translation = create(:entry, :published)

0 commit comments

Comments
 (0)