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

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
5 changes: 5 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,10 @@ def resolve_component(original_component)
custom_components.dig(original_component.to_s)&.to_s&.safe_constantize || original_component
end

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

private

def flatten_keys(array)
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
Loading