Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: external link on records #3501

Merged
merged 13 commits into from
Dec 20, 2024
Merged
3 changes: 2 additions & 1 deletion app/components/avo/items/panel_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ def args
index: 0,
data: {panel_id: "main"},
cover_photo: @resource.cover_photo,
profile_photo: @resource.profile_photo
profile_photo: @resource.profile_photo,
external_link: @resource.get_external_link
}
else
{name: @item.name, description: @item.description, index: @index}
Expand Down
3 changes: 2 additions & 1 deletion app/components/avo/panel_component.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
name: @name,
description: @description,
display_breadcrumbs: @display_breadcrumbs,
profile_photo: @profile_photo
profile_photo: @profile_photo,
external_link: @external_link
) do |header| %>
<% if name_slot.present? %>
<% header.with_name_slot do %>
Expand Down
1 change: 1 addition & 0 deletions app/components/avo/panel_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class Avo::PanelComponent < Avo::BaseComponent
def after_initialize
@name = @args.dig(:name) || @args.dig(:title)
end
prop :external_link

def classes
class_names(@classes, "has-cover-photo": @cover_photo.present?, "has-profile-photo": @profile_photo.present?)
Expand Down
10 changes: 9 additions & 1 deletion app/components/avo/panel_header_component.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,15 @@
<% if name_slot? %>
<%= name_slot %>
<% else %>
<%= render Avo::PanelNameComponent.new name: @name %>
<%= render Avo::PanelNameComponent.new name: @name do |panel_name_component| %>
<% panel_name_component.with_body do %>
<% if @external_link.present? %>
<%= link_to @external_link, class: "text-gray-600 hover:text-gray-900 mt-1", title: helpers.t("avo.visit_record_on_external_path"), data: {tippy: :tooltip}, target: :_blank do %>
<%= svg "heroicons/outline/arrow-top-right-on-square", class: "ml-2 text-2xl h-4" %>
<% end %>
<% end %>
<% end %>
<% end %>
<% end %>
<% if @description.present? %>
<div class="text-sm tracking-normal font-medium text-gray-600 text-center sm:text-left" data-target="description">
Expand Down
1 change: 1 addition & 0 deletions app/components/avo/panel_header_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class Avo::PanelHeaderComponent < Avo::BaseComponent
renders_one :tools

prop :name
prop :external_link
prop :description
prop :display_breadcrumbs, default: false
prop :profile_photo
Expand Down
7 changes: 7 additions & 0 deletions lib/avo/resources/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ def current_user
class_attribute :default_sort_column, default: :created_at
class_attribute :default_sort_direction, default: :desc
class_attribute :controls_placement, default: nil
class_attribute :external_link, default: nil

# EXTRACT:
class_attribute :ordering
Expand Down Expand Up @@ -644,6 +645,12 @@ def resolve_component(original_component)
custom_components.dig(original_component.to_s)&.to_s&.safe_constantize || original_component
end

def get_external_link
return unless record.persisted?

Avo::ExecutionContext.new(target: external_link, resource: self, record: record).handle
end

private

def flatten_keys(array)
Expand Down
1 change: 1 addition & 0 deletions lib/generators/avo/templates/locales/avo.ar.yml
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ ar:
undo: تراجع
view: عرض
view_item: عرض %{item}
visit_record_on_external_path: زيارة السجل على الرابط الخارجي
was_successfully_created: تم إنشاؤه بنجاح
was_successfully_updated: تم تحديثه بنجاح
x_items_more:
Expand Down
1 change: 1 addition & 0 deletions lib/generators/avo/templates/locales/avo.de.yml
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ de:
undo: Rückgängig machen
view: Anzeigen
view_item: "%{item} anzeigen"
visit_record_on_external_path: Datensatz über externen Link besuchen
was_successfully_created: wurde erfolgreich erstellt
was_successfully_updated: wurde erfolgreich aktualisiert
x_items_more:
Expand Down
1 change: 1 addition & 0 deletions lib/generators/avo/templates/locales/avo.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ en:
undo: undo
view: View
view_item: view %{item}
visit_record_on_external_path: Visit record on external path
was_successfully_created: was successfully created
was_successfully_updated: was successfully updated
x_items_more:
Expand Down
1 change: 1 addition & 0 deletions lib/generators/avo/templates/locales/avo.es.yml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ es:
undo: deshacer
view: Vista
view_item: ver %{item}
visit_record_on_external_path: Visitar registro en enlace externo
was_successfully_created: se ha creado con éxito
was_successfully_updated: se ha actualizado con éxito
x_items_more:
Expand Down
1 change: 1 addition & 0 deletions lib/generators/avo/templates/locales/avo.fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ fr:
undo: annuler
view: Vue
view_item: voir %{item}
visit_record_on_external_path: Consulter l'enregistrement via un lien externe
was_successfully_created: a été créé avec succès
was_successfully_updated: a été mis à jour avec succès
x_items_more:
Expand Down
1 change: 1 addition & 0 deletions lib/generators/avo/templates/locales/avo.it.yml
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ it:
undo: Annulla
view: Visualizza
view_item: Visualizza %{item}
visit_record_on_external_path: Visita il record tramite link esterno
was_successfully_created: è stato creato con successo
was_successfully_updated: è stato aggiornato con successo
x_items_more:
Expand Down
1 change: 1 addition & 0 deletions lib/generators/avo/templates/locales/avo.ja.yml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ ja:
undo: 元に戻す
view: ビュー
view_item: "%{item}を表示"
visit_record_on_external_path: 外部リンクで記録を確認する
was_successfully_created: は正常に作成されました
was_successfully_updated: は正常に更新されました
x_items_more:
Expand Down
1 change: 1 addition & 0 deletions lib/generators/avo/templates/locales/avo.nb.yml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ nb:
undo: angre
view: Vis
view_item: vis %{item}
visit_record_on_external_path: Besøk posten via en ekstern lenke
was_successfully_created: ble opprettet
was_successfully_updated: ble oppdatert
x_items_more:
Expand Down
1 change: 1 addition & 0 deletions lib/generators/avo/templates/locales/avo.nl.yml
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ nl:
undo: Ongedaan maken
view: Bekijken
view_item: "%{item} bekijken"
visit_record_on_external_path: Bezoek record via een externe link
was_successfully_created: is succesvol aangemaakt
was_successfully_updated: is succesvol bijgewerkt
x_items_more:
Expand Down
1 change: 1 addition & 0 deletions lib/generators/avo/templates/locales/avo.nn.yml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ nn:
undo: angre
view: Vis
view_item: vis %{item}
visit_record_on_external_path: Besøk posten via ein ekstern lenke
was_successfully_created: vart oppretta
was_successfully_updated: vart oppdatert
x_items_more:
Expand Down
1 change: 1 addition & 0 deletions lib/generators/avo/templates/locales/avo.pl.yml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ pl:
undo: Cofnij
view: Widok
view_item: Wyświetl %{item}
visit_record_on_external_path: Odwiedź rekord za pomocą zewnętrznego linku
was_successfully_created: został pomyślnie utworzony
was_successfully_updated: został pomyślnie zaktualizowany
x_items_more:
Expand Down
1 change: 1 addition & 0 deletions lib/generators/avo/templates/locales/avo.pt-BR.yml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ pt-BR:
undo: desfazer
view: Visualizar
view_item: visualizar %{item}
visit_record_on_external_path: Visitar registro através de link externo
was_successfully_created: foi criado com sucesso
was_successfully_updated: foi atualizado com sucesso
x_items_more:
Expand Down
1 change: 1 addition & 0 deletions lib/generators/avo/templates/locales/avo.pt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ pt:
undo: desfazer
view: Ver
view_item: ver %{item}
visit_record_on_external_path: Visitar registro através de link externo
was_successfully_created: foi criado com sucesso
was_successfully_updated: foi atualizado com sucesso
x_items_more:
Expand Down
1 change: 1 addition & 0 deletions lib/generators/avo/templates/locales/avo.ro.yml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ ro:
undo: Anulează
view: vezi
view_item: vezi %{item}
visit_record_on_external_path: Vizitați înregistrarea printr-un link extern
was_successfully_created: a fost creat
was_successfully_updated: a fost actualizat
x_items_more:
Expand Down
1 change: 1 addition & 0 deletions lib/generators/avo/templates/locales/avo.ru.yml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ ru:
undo: Отменить
view: Просмотр
view_item: Просмотр %{item}
visit_record_on_external_path: Перейти к записи через внешнюю ссылку
was_successfully_created: успешно создана
was_successfully_updated: успешно обновлена
x_items_more:
Expand Down
1 change: 1 addition & 0 deletions lib/generators/avo/templates/locales/avo.tr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ tr:
undo: geri al
view: Görünüm
view_item: "%{item} öğresini görüntüle"
visit_record_on_external_path: Harici bağlantı yoluyla kaydı ziyaret et
was_successfully_created: başarıyla oluşturuldu
was_successfully_updated: başarıyla güncellendi
x_items_more:
Expand Down
1 change: 1 addition & 0 deletions lib/generators/avo/templates/locales/avo.uk.yml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ uk:
undo: Скасувати
view: Перегляд
view_item: Перегляд %{item}
visit_record_on_external_path: Перейти до запису через зовнішнє посилання
was_successfully_created: успішно створено
was_successfully_updated: успішно оновлено
x_items_more:
Expand Down
1 change: 1 addition & 0 deletions lib/generators/avo/templates/locales/avo.zh.yml
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ zh:
undo: 撤销
view: 查看
view_item: 查看 %{item}
visit_record_on_external_path: 通过外部链接访问记录
was_successfully_created: 创建成功
was_successfully_updated: 更新成功
x_items_more:
Expand Down
4 changes: 4 additions & 0 deletions spec/dummy/app/avo/resources/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ class Avo::Resources::Post < Avo::BaseResource
end
}
self.view_types = [:grid, :table]
# Show a link to the post outside Avo
self.external_link = -> {
main_app.post_path(record)
}

def fields
field :id, as: :id
Expand Down
9 changes: 9 additions & 0 deletions spec/dummy/app/controllers/posts_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class PostsController < ApplicationController
def index
@posts = Post.all
end

def show
@post = Post.find(params[:id])
end
end
4 changes: 4 additions & 0 deletions spec/dummy/app/views/posts/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<h1>Posts#index</h1>
<p>Find me in app/views/posts/index.html.erb</p>

<%= @posts.count %>
4 changes: 4 additions & 0 deletions spec/dummy/app/views/posts/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<h1>Posts#show</h1>
<p>Find me in app/views/posts/show.html.erb</p>

<%= @post.inspect %>
1 change: 1 addition & 0 deletions spec/dummy/config/locales/avo.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ en:
undo: undo
view: View
view_item: view %{item}
visit_record_on_external_path: Visit record on external path
was_successfully_created: was successfully created
was_successfully_updated: was successfully updated
x_items_more:
Expand Down
2 changes: 2 additions & 0 deletions spec/dummy/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

get "hey", to: "home#index"

resources :posts

authenticate :user, ->(user) { user.is_admin? } do
scope :admin do
get "custom_tool", to: "avo/tools#custom_tool", as: :custom_tool
Expand Down
25 changes: 25 additions & 0 deletions spec/features/avo/external_link_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require "rails_helper"

RSpec.feature "external_link", type: :feature do
let!(:post) { create :post }

describe "external_link" do
it "on show" do
visit avo.resources_post_path(post)

find("[target='_blank'][href='/posts/#{post.to_param}']").click

expect(current_path).to eq "/posts/#{post.to_param}"
expect(page).to have_text("Find me in app/views/posts/show.html.erb")
end

it "on edit" do
visit avo.edit_resources_post_path(post)

find("[target='_blank'][href='/posts/#{post.to_param}']").click

expect(current_path).to eq "/posts/#{post.to_param}"
expect(page).to have_text("Find me in app/views/posts/show.html.erb")
end
end
end
Loading