Skip to content

Commit

Permalink
Merge pull request #2108 from tf/password-protected-translations
Browse files Browse the repository at this point in the history
Make filtering of entry translations menu less restrictive
  • Loading branch information
tf authored Jun 6, 2024
2 parents a1b25b1 + 3c26582 commit 5fbdcf8
Show file tree
Hide file tree
Showing 9 changed files with 168 additions and 14 deletions.
2 changes: 1 addition & 1 deletion app/models/pageflow/draft_entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def entry_title
entry.title
end

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

PublishedEntry.wrap_all_drafts(
Expand Down
15 changes: 15 additions & 0 deletions app/models/pageflow/entry_translation_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,26 @@ class EntryTranslationGroup < ApplicationRecord
foreign_key: 'translation_group_id',
dependent: :nullify

has_many :published_entries,
-> { published },
foreign_key: 'translation_group_id',
class_name: 'Entry'

has_many :publicly_visible_entries,
-> { published_without_password_protection.published_without_noindex },
foreign_key: 'translation_group_id',
class_name: 'Entry'

has_many :entries_published_without_password_protection,
-> { published_without_password_protection },
foreign_key: 'translation_group_id',
class_name: 'Entry'

has_many :entries_published_without_noindex,
-> { published_without_noindex },
foreign_key: 'translation_group_id',
class_name: 'Entry'

belongs_to :default_translation,
class_name: 'Entry',
optional: true
Expand Down
18 changes: 16 additions & 2 deletions app/models/pageflow/published_entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ def title
revision.title.presence || entry.title
end

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

if published_revision?
self.class.wrap_all(
entry.translation_group.publicly_visible_entries.instance_exec(&scope)
published_translation_entries(include_noindex:).instance_exec(&scope)
)
else
self.class.wrap_all_drafts(
Expand Down Expand Up @@ -99,6 +99,20 @@ def published_revision?

private

def published_translation_entries(include_noindex:)
translation_group = entry.translation_group

if password_protected? && include_noindex
translation_group.published_entries
elsif password_protected?
translation_group.entries_published_without_noindex
elsif include_noindex
translation_group.entries_published_without_password_protection
else
translation_group.publicly_visible_entries
end
end

def custom_revision?
@custom_revision
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def scrolled_entry_json_seed(json, scrolled_entry, options = {})
sections: main_storyline.sections,
content_elements: main_storyline.content_elements,
widgets: scrolled_entry.resolve_widgets(insert_point: :react),
options: options)
options:)
end
end
end
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
json.entry_translations do
json.array!(entry.translations(-> { preload(:site) })) do |translation|
json.array!(entry.translations(-> { preload(:site) }, include_noindex: true)) do |translation|
json.(translation, :id, :locale)
json.display_locale t('pageflow.public._language', locale: translation.locale)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -746,7 +746,7 @@ def render(helper, entry, options = {})
end

context 'entry translations' do
it 'renders links to published translations of published entry' do
it 'renders links to published translations of published entry including noindex entries' do
de_entry = create(
:published_entry,
type_name: 'scrolled',
Expand All @@ -755,6 +755,7 @@ def render(helper, entry, options = {})
)
en_entry = create(
:published_entry,
:published_with_noindex,
translation_of: de_entry,
type_name: 'scrolled',
title: 'entry-en',
Expand Down
26 changes: 18 additions & 8 deletions spec/factories/published_entries.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,29 @@ module Pageflow
without_feature { nil }

translation_of { nil }

entry_trait { :published }
end

trait :published_with_password do
entry_trait { :published_with_password }
end

trait :published_with_noindex do
entry_trait { :published_with_noindex }
end

initialize_with do
PublishedEntry.new(create(:entry,
:published,
title: title,
account: account,
site: site,
type_name: type_name,
entry_trait,
title:,
account:,
site:,
type_name:,
published_revision_attributes: revision_attributes,
permalink_attributes: permalink_attributes,
with_feature: with_feature,
without_feature: without_feature))
permalink_attributes:,
with_feature:,
without_feature:))
end

to_create { |published_entry| published_entry.entry.save! }
Expand Down
13 changes: 13 additions & 0 deletions spec/models/pageflow/draft_entry_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@ module Pageflow

expect(result[0].entry.association(:permalink).loaded?).to eq(true)
end

it 'ignores include_noindex argument' do
entry = create(:entry)
translation = create(:entry)
entry.mark_as_translation_of(translation)
draft_entry = DraftEntry.new(entry)

result = draft_entry.translations(include_noindex: true)

expect(result.length).to eq(2)
expect(result[0].title).to eq(entry.title)
expect(result[1].title).to eq(translation.title)
end
end

describe '#find_files' do
Expand Down
101 changes: 101 additions & 0 deletions spec/models/pageflow/published_entry_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,31 @@ module Pageflow
expect(result[0].title).to eq(entry.title)
end

it 'does not filter out password protected entries if entry is published with password' do
entry = create(:entry, :published_with_password)
translation = create(:entry, :published_with_password)
entry.mark_as_translation_of(translation)
published_entry = PublishedEntry.new(entry)

result = published_entry.translations

expect(result.length).to eq(2)
expect(result[0].title).to eq(entry.title)
expect(result[1].title).to eq(translation.title)
end

it 'filters out non-published entries if entry is published with password' do
entry = create(:entry, :published_with_password)
translation = create(:entry)
entry.mark_as_translation_of(translation)
published_entry = PublishedEntry.new(entry)

result = published_entry.translations

expect(result.length).to eq(1)
expect(result[0].title).to eq(entry.title)
end

it 'filters out noindex entries' do
entry = create(:entry, :published)
translation = create(:entry, :published_with_noindex)
Expand All @@ -265,6 +290,82 @@ module Pageflow
expect(result[0].title).to eq(entry.title)
end

it 'filters out noindex entries if entry is published with password' do
entry = create(:entry, :published_with_password)
translation = create(:entry, :published_with_noindex)
entry.mark_as_translation_of(translation)
published_entry = PublishedEntry.new(entry)

result = published_entry.translations

expect(result.length).to eq(1)
expect(result[0].title).to eq(entry.title)
end

describe 'with include_noindex' do
it 'includes noindex entries' do
entry = create(:entry, :published)
translation = create(:entry, :published_with_noindex)
entry.mark_as_translation_of(translation)
published_entry = PublishedEntry.new(entry)

result = published_entry.translations(include_noindex: true)

expect(result.length).to eq(2)
expect(result[0].title).to eq(entry.title)
expect(result[1].title).to eq(translation.title)
end

it 'filters out non-published entries' do
entry = create(:entry, :published)
translation = create(:entry)
entry.mark_as_translation_of(translation)
published_entry = PublishedEntry.new(entry)

result = published_entry.translations(include_noindex: true)

expect(result.length).to eq(1)
expect(result[0].title).to eq(entry.title)
end

it 'filters out password protected entries' do
entry = create(:entry, :published)
translation = create(:entry, :published_with_password)
entry.mark_as_translation_of(translation)
published_entry = PublishedEntry.new(entry)

result = published_entry.translations(include_noindex: true)

expect(result.length).to eq(1)
expect(result[0].title).to eq(entry.title)
end

it 'does not filter out password protected entries if entry is published with password' do
entry = create(:entry, :published_with_password)
translation = create(:entry, :published_with_password)
entry.mark_as_translation_of(translation)
published_entry = PublishedEntry.new(entry)

result = published_entry.translations(include_noindex: true)

expect(result.length).to eq(2)
expect(result[0].title).to eq(entry.title)
expect(result[1].title).to eq(translation.title)
end

it 'filters out non-published entries for password protected entry' do
entry = create(:entry, :published_with_password)
translation = create(:entry)
entry.mark_as_translation_of(translation)
published_entry = PublishedEntry.new(entry)

result = published_entry.translations(include_noindex: true)

expect(result.length).to eq(1)
expect(result[0].title).to eq(entry.title)
end
end

it 'supports using drafts' do
entry = create(:entry, :published)
published_translation = create(:entry, :published)
Expand Down

0 comments on commit 5fbdcf8

Please sign in to comment.