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: discreet information #3592

Merged
merged 15 commits into from
Feb 28, 2025
Prev Previous commit
Next Next commit
wip
  • Loading branch information
adrianthedev committed Jan 17, 2025
commit 5b94e6c3466d805dc54e1b1d72ff936d67202ac6
10 changes: 6 additions & 4 deletions app/components/avo/discreet_information_component.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<% items.each do |item| %>
<%= content_tag :div, class: "text-gray-600 hover:text-gray-900 mt-1", title: item.tooltip, data: {tippy: :tooltip} do %>
<%= helpers.svg item.icon, class: "ml-2 text-2xl h-4" %>
<div class="flex gap-2 ml-2 mt-1">
<% items.each do |item| %>
<%= 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} do %>
<%= item.label if item.label.present? %> <%= helpers.svg item.icon, class: "text-2xl h-4" %>
<% end %>
<% end %>
<% end %>
</div>
16 changes: 16 additions & 0 deletions app/components/avo/discreet_information_component.rb
Original file line number Diff line number Diff line change
@@ -6,4 +6,20 @@ class Avo::DiscreetInformationComponent < Avo::BaseComponent
def items
@payload.items.compact
end

def element_tag(item)
if item.url.present?
:a
else
:div
end
end

def element_attributes(item)
if item.url.present?
{href: item.url, target: item.url_target}
else
{}
end
end
end
10 changes: 7 additions & 3 deletions lib/avo/discreet_information.rb
Original file line number Diff line number Diff line change
@@ -31,16 +31,20 @@ def timestamp_item(item)
def parse_payload(item)
if item.is_a?(Hash)
tooltip = item.delete(:tooltip)
icon = item.delete(:icon) || "heroicons/outline/clock"
icon = item.delete(:icon)
url = item.delete(:url)
url_target = item.delete(:url_target)
label = item.delete(:label)

DiscreetInformationItem.new(
tooltip: Avo::ExecutionContext.new(target: tooltip, record: record, resource: self, view: view).handle,
icon: Avo::ExecutionContext.new(target: icon, record: record, resource: self, view: view).handle,
url: Avo::ExecutionContext.new(target: url, record: record, resource: self, view: view).handle
url: Avo::ExecutionContext.new(target: url, record: record, resource: self, view: view).handle,
url_target: Avo::ExecutionContext.new(target: url_target, record: record, resource: self, view: view).handle,
label: Avo::ExecutionContext.new(target: label, record: record, resource: self, view: view).handle,
)
end
end

DiscreetInformationItem = Struct.new(:tooltip, :icon, :url, keyword_init: true) unless defined?(DiscreetInformationItem)
DiscreetInformationItem = Struct.new(:tooltip, :icon, :url, :url_target, :label, keyword_init: true) unless defined?(DiscreetInformationItem)
end
1 change: 1 addition & 0 deletions spec/dummy/app/avo/resources/event.rb
Original file line number Diff line number Diff line change
@@ -17,6 +17,7 @@ class Avo::Resources::Event < Avo::BaseResource
self.profile_photo = {
source: :profile_photo
}
self.discreet_information = :timestamps

def fields
field :name, as: :text, link_to_record: true, sortable: true, stacked: true
13 changes: 12 additions & 1 deletion spec/dummy/app/avo/resources/post.rb
Original file line number Diff line number Diff line change
@@ -33,7 +33,18 @@ class Avo::Resources::Post < Avo::BaseResource
main_app.post_path(record)
}

self.discreet_information = :timestamps
self.discreet_information = [
:timestamps,
{
tooltip: -> { sanitize("Product is <strong>#{record.published_at ? "published" : "draft"}</strong>", tags: %w[strong]) },
icon: -> { "heroicons/outline/#{record.published_at ? "eye" : "eye-slashed"}" }
},
{
label: -> { record.published_at ? "✅" : "🙄" },
url: -> { "https://avohq.io" },
url_target: :_blank
}
]

def fields
field :id, as: :id