From 1d4eb2c2b80603bb2dd2ad877a52387bfdb94b0d Mon Sep 17 00:00:00 2001 From: Daniel Kuhn Date: Tue, 1 Oct 2024 16:38:20 +0200 Subject: [PATCH 1/3] Create configuration option for specifying a translator url Add a link to the specified url if present --- .../pageflow/admin/entry_translations_tab.rb | 12 ++++++ config/locales/new/entry_translations.de.yml | 1 + config/locales/new/entry_translations.en.yml | 1 + lib/pageflow/configuration.rb | 3 ++ .../admin/entry_translations_tab_spec.rb | 39 +++++++++++++++++++ 5 files changed, 56 insertions(+) diff --git a/app/views/components/pageflow/admin/entry_translations_tab.rb b/app/views/components/pageflow/admin/entry_translations_tab.rb index 47858e380..6455ddbdb 100644 --- a/app/views/components/pageflow/admin/entry_translations_tab.rb +++ b/app/views/components/pageflow/admin/entry_translations_tab.rb @@ -29,6 +29,7 @@ def build(entry) end add_link(entry) + generate_link(entry) end private @@ -84,6 +85,17 @@ def add_link(entry) new_admin_entry_translation_path(entry), class: 'button')) end + + def generate_link(entry) + return unless authorized?(:manage_translations, entry) + entry_translator_url = Pageflow.config_for(entry).entry_translator_url + return if entry_translator_url.blank? + + text_node(link_to(t('pageflow.admin.entry_translations.generate'), + entry_translator_url.call(entry), + class: 'button', + style: "margin-left: 8px")) + end end end end diff --git a/config/locales/new/entry_translations.de.yml b/config/locales/new/entry_translations.de.yml index 994ffc6e4..9e204a105 100644 --- a/config/locales/new/entry_translations.de.yml +++ b/config/locales/new/entry_translations.de.yml @@ -5,6 +5,7 @@ de: translations: Übersetzungen entry_translations: add: Übersetzung zuordnen + generate: Übersetzung generieren entry_id: Beitrag id: Übersetzung(en) new_hint_html: |- diff --git a/config/locales/new/entry_translations.en.yml b/config/locales/new/entry_translations.en.yml index d4092bf77..de6dd32c7 100644 --- a/config/locales/new/entry_translations.en.yml +++ b/config/locales/new/entry_translations.en.yml @@ -5,6 +5,7 @@ en: translations: Translations entry_translations: add: Link translation + generate: Generate translation entry_id: Entry id: Translation(s) new_hint_html: |- diff --git a/lib/pageflow/configuration.rb b/lib/pageflow/configuration.rb index 851730bfb..05af75a8b 100644 --- a/lib/pageflow/configuration.rb +++ b/lib/pageflow/configuration.rb @@ -425,6 +425,8 @@ class Configuration # @since 12.2 attr_accessor :news + attr_accessor :entry_translator_url + def initialize(target_type_name = nil) @target_type_name = target_type_name @@ -618,6 +620,7 @@ def enable_all_features delegate :public_entry_cache_control_header=, to: :config delegate :additional_public_entry_headers, to: :config delegate :cutoff_modes, to: :config + delegate :entry_translator_url=, to: :config delegate :for_entry_type, to: :config end diff --git a/spec/views/components/pageflow/admin/entry_translations_tab_spec.rb b/spec/views/components/pageflow/admin/entry_translations_tab_spec.rb index 905abff91..d146096c5 100644 --- a/spec/views/components/pageflow/admin/entry_translations_tab_spec.rb +++ b/spec/views/components/pageflow/admin/entry_translations_tab_spec.rb @@ -25,6 +25,23 @@ module Pageflow expect(rendered).to have_selector('a', text: 'Mark as default') end + it 'renders a link to entry translator url' do + pageflow_configure do |config| + config.entry_translator_url = lambda do |entry| + "http://example.com?entry_id=#{entry.id}" + end + end + + de_entry = create(:entry, draft_attributes: {locale: 'de'}) + + allow(helper).to receive(:authorized?).and_return(true) + + render(de_entry) + + expect(rendered).to have_selector('a', text: 'Generate translation') + expect(rendered).to have_selector("a[href='http://example.com?entry_id=#{de_entry.id}']") + end + it 'includes self in table without link' do de_entry = create(:entry, title: 'My story DE', draft_attributes: {locale: 'de'}) en_entry = create(:entry, title: 'My story EN', draft_attributes: {locale: 'en'}) @@ -67,6 +84,28 @@ module Pageflow expect(rendered).to have_selector('a', text: 'Mark as default') end + it 'hides generate link by default' do + de_entry = create(:entry, draft_attributes: {locale: 'de'}) + + allow(helper).to receive(:authorized?).and_return(true) + + render(de_entry) + + expect(rendered).not_to have_selector('a', text: 'Generate translation') + end + + it 'hides generate link if user cannot manage translations' do + de_entry = create(:entry, draft_attributes: {locale: 'de'}) + + allow(helper).to receive(:authorized?).and_return(true) + allow(helper) + .to receive(:authorized?).with(:manage_translations, de_entry).and_return(false) + + render(de_entry) + + expect(rendered).not_to have_selector('a', text: 'Generate translation') + end + it 'hides remove and add links if user cannot manage translations' do de_entry = create(:entry, draft_attributes: {locale: 'de'}) en_entry = create(:entry, title: 'My story EN', draft_attributes: {locale: 'en'}) From adaeb7025164719fcc34a51ce9dc275b75073da6 Mon Sep 17 00:00:00 2001 From: Daniel Kuhn Date: Mon, 7 Oct 2024 14:48:44 +0200 Subject: [PATCH 2/3] Add Docstring for entry_translator_url in configuration --- lib/pageflow/configuration.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lib/pageflow/configuration.rb b/lib/pageflow/configuration.rb index 05af75a8b..f78278051 100644 --- a/lib/pageflow/configuration.rb +++ b/lib/pageflow/configuration.rb @@ -425,6 +425,16 @@ class Configuration # @since 12.2 attr_accessor :news + # URL to a form that is used to translate an entry via an external service. + # + # @example + # + # config.features.register 'ai_translator' do |feature_config| + # feature_config.entry_translator_url = lambda do |entry| + # Rails.application.routes.url_helpers.new_admin_entry_ai_translation_path(entry) + # end + # end + # @since edge attr_accessor :entry_translator_url def initialize(target_type_name = nil) From f6d2969ab0e610f600d40d88fb3fc11f2869baa4 Mon Sep 17 00:00:00 2001 From: Tim Fischbach Date: Mon, 7 Oct 2024 15:38:33 +0200 Subject: [PATCH 3/3] Improve doc string Make example more generic. Keep example usage of host application route helpers --- lib/pageflow/configuration.rb | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/pageflow/configuration.rb b/lib/pageflow/configuration.rb index f78278051..e3e78828c 100644 --- a/lib/pageflow/configuration.rb +++ b/lib/pageflow/configuration.rb @@ -429,10 +429,8 @@ class Configuration # # @example # - # config.features.register 'ai_translator' do |feature_config| - # feature_config.entry_translator_url = lambda do |entry| - # Rails.application.routes.url_helpers.new_admin_entry_ai_translation_path(entry) - # end + # config.entry_translator_url = lambda do |entry| + # Rails.application.routes.url_helpers.some_custom_route(entry) # end # @since edge attr_accessor :entry_translator_url