Skip to content

Commit c5fc7b6

Browse files
committed
Add as_badge and :id type to Discreet Information
1 parent d5c12e3 commit c5fc7b6

File tree

4 files changed

+33
-5
lines changed

4 files changed

+33
-5
lines changed

app/components/avo/discreet_information_component.html.erb

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
<div class="flex gap-2 ml-2 mt-1">
1+
<div class="flex gap-2 ml-2 mt-1 items-center">
22
<% items.each do |item| %>
3-
<%= content_tag element_tag(item), **element_attributes(item), class: "flex gap-1 text-xs font-normal text-gray-600 hover:text-gray-900", title: item.tooltip, data: {tippy: :tooltip, **data(item)} do %>
3+
<%= content_tag element_tag(item), **element_attributes(item), class: element_classes(item), title: item.tooltip, data: {tippy: :tooltip, **data(item)} do %>
44
<%= item.label if item.label.present? %> <%= helpers.svg item.icon, class: "text-2xl h-4" %>
55
<% end %>
66
<% end %>

app/components/avo/discreet_information_component.rb

+8
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,13 @@ def element_attributes(item)
2323
end
2424
end
2525

26+
def element_classes(item)
27+
if item.as_badge
28+
%w(whitespace-nowrap rounded-md uppercase px-2 py-1 text-xs font-bold block text-center truncate bg-gray-500 text-white)
29+
else
30+
%w(flex gap-1 text-xs font-normal text-gray-600 hover:text-gray-900)
31+
end
32+
end
33+
2634
def data(item) = item.data || {}
2735
end

lib/avo/discreet_information.rb

+12-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ def items
1010
Array.wrap(resource.class.discreet_information).map do |item|
1111
if item == :timestamps
1212
timestamp_item(item)
13+
elsif item == :id
14+
id(item)
1315
else
1416
parse_payload(item)
1517
end
@@ -18,6 +20,13 @@ def items
1820

1921
private
2022

23+
def id(item)
24+
DiscreetInformationItem.new(
25+
label: "ID: #{record.id}",
26+
as_badge: true
27+
)
28+
end
29+
2130
def timestamp_item(item)
2231
return if record.created_at.blank? && record.updated_at.blank?
2332

@@ -35,7 +44,7 @@ def timestamp_item(item)
3544

3645
DiscreetInformationItem.new(
3746
tooltip: tag.div([created_at_tag, updated_at_tag].compact.join(tag.br), style: "text-align: right;"),
38-
icon: "heroicons/outline/clock"
47+
icon: "heroicons/outline/clock",
3948
)
4049
end
4150

@@ -54,9 +63,9 @@ def parse_payload(item)
5463
url: Avo::ExecutionContext.new(target: item[:url], **args).handle,
5564
url_target: Avo::ExecutionContext.new(target: item[:url_target], **args).handle,
5665
data: Avo::ExecutionContext.new(target: item[:data], **args).handle,
57-
label: Avo::ExecutionContext.new(target: item[:label], **args).handle
66+
label: Avo::ExecutionContext.new(target: item[:label], **args).handle,
5867
)
5968
end
6069

61-
DiscreetInformationItem = Struct.new(:tooltip, :icon, :url, :url_target, :data, :label, keyword_init: true) unless defined?(DiscreetInformationItem)
70+
DiscreetInformationItem = Struct.new(:tooltip, :icon, :url, :url_target, :data, :label, :as_badge, keyword_init: true) unless defined?(DiscreetInformationItem)
6271
end

spec/dummy/app/avo/resources/project.rb

+11
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,17 @@ class Avo::Resources::Project < Avo::BaseResource
1212
query.unscoped
1313
}
1414

15+
self.discreet_information = [
16+
:timestamps,
17+
:id,
18+
{
19+
tooltip: -> { sanitize("View <strong>#{record.name}</strong> on site", tags: %w[strong]) },
20+
icon: -> { "heroicons/outline/arrow-top-right-on-square" },
21+
url: -> { main_app.root_url },
22+
url_target: :_blank
23+
}
24+
]
25+
1526
def fields
1627
field :id, as: :id, link_to_record: true
1728
field :status,

0 commit comments

Comments
 (0)