From 29795fcb178568c3a2fc0529f2632e43db70d5a3 Mon Sep 17 00:00:00 2001 From: Thiago Youssef <43591948+thiagoyoussef@users.noreply.github.com> Date: Mon, 4 Nov 2024 05:26:56 -0300 Subject: [PATCH] feature: callable filter `name` and `button_label` (#3382) * feature: callable filter `name` and `button_label` * Update lib/avo/filters/base_filter.rb --------- Co-authored-by: Paul Bob <69730720+Paul-Bob@users.noreply.github.com> --- lib/avo/filters/base_filter.rb | 10 +++++++++- spec/dummy/app/avo/filters/user_names_filter.rb | 4 ++-- spec/dummy/config/locales/avo.en.yml | 4 ++++ 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/lib/avo/filters/base_filter.rb b/lib/avo/filters/base_filter.rb index 5dc90aef6b..a2014b7edc 100644 --- a/lib/avo/filters/base_filter.rb +++ b/lib/avo/filters/base_filter.rb @@ -46,7 +46,7 @@ def apply_query(request, query, value) end def id - self.class.name.underscore.tr("/", "_") + name.underscore.tr("/", "_") end # Get the applied value this filter. @@ -88,6 +88,14 @@ def visible_in_view(resource: nil, parent_resource: nil) arguments: arguments ).handle end + + def name + Avo::ExecutionContext.new(target: self.class.name, arguments: @arguments).handle + end + + def button_label + Avo::ExecutionContext.new(target: self.class.button_label, arguments: @arguments).handle + end end end end diff --git a/spec/dummy/app/avo/filters/user_names_filter.rb b/spec/dummy/app/avo/filters/user_names_filter.rb index 56926e25b5..4e92a5b5cb 100644 --- a/spec/dummy/app/avo/filters/user_names_filter.rb +++ b/spec/dummy/app/avo/filters/user_names_filter.rb @@ -1,6 +1,6 @@ class Avo::Filters::UserNamesFilter < Avo::Filters::TextFilter - self.name = "User names filter" - self.button_label = "Filter by user names" + self.name = -> { I18n.t("avo.filter_translations.user_names_filter.name") } + self.button_label = -> { I18n.t("avo.filter_translations.user_names_filter.button_label") } self.empty_message = "Search by name" def apply(request, query, value) diff --git a/spec/dummy/config/locales/avo.en.yml b/spec/dummy/config/locales/avo.en.yml index ed04e5b7f2..9f2ce70e58 100644 --- a/spec/dummy/config/locales/avo.en.yml +++ b/spec/dummy/config/locales/avo.en.yml @@ -119,3 +119,7 @@ en: x_records_selected_from_page_html: %{selected} records selected on this page yes_confirm: Yes, I'm sure you_missed_something_check_form: You might have missed something. Please check the form. + filter_translations: + user_names_filter: + name: User names filter + button_label: Filter by user names