Skip to content

Commit

Permalink
Create configuration option for specifying a translator url
Browse files Browse the repository at this point in the history
Add a link to the specified url if present
  • Loading branch information
DanielKuhn committed Oct 2, 2024
1 parent 8dc1eac commit 1d4eb2c
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 0 deletions.
12 changes: 12 additions & 0 deletions app/views/components/pageflow/admin/entry_translations_tab.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def build(entry)
end

add_link(entry)
generate_link(entry)
end

private
Expand Down Expand Up @@ -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
1 change: 1 addition & 0 deletions config/locales/new/entry_translations.de.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ de:
translations: Übersetzungen
entry_translations:
add: Übersetzung zuordnen
generate: Übersetzung generieren
entry_id: Beitrag
id: Übersetzung(en)
new_hint_html: |-
Expand Down
1 change: 1 addition & 0 deletions config/locales/new/entry_translations.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ en:
translations: Translations
entry_translations:
add: Link translation
generate: Generate translation
entry_id: Entry
id: Translation(s)
new_hint_html: |-
Expand Down
3 changes: 3 additions & 0 deletions lib/pageflow/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'})
Expand Down Expand Up @@ -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'})
Expand Down

0 comments on commit 1d4eb2c

Please sign in to comment.