From 3cb4f5dee71fa5b0af7ebbc6f74c69de4d119517 Mon Sep 17 00:00:00 2001 From: Bogdan Gusiev Date: Mon, 22 Jan 2024 13:58:59 +0100 Subject: [PATCH] Improve BaseDatagrid with more examples --- templates/base.rb.erb | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/templates/base.rb.erb b/templates/base.rb.erb index ddf2becb..ed4281ce 100644 --- a/templates/base.rb.erb +++ b/templates/base.rb.erb @@ -11,11 +11,35 @@ class BaseGrid # Enable forbidden attributes protection # self.forbidden_attributes_protection = true - def self.date_column(name, *args) + # Makes a date column + # @param name [Symbol] Column name + # @param args [Array] Other column helper arguments + # @example + # date_column(:created_at) + # date_column(:owner_registered_at) do |model| + # model.owner.registered_at + # end + def self.date_column(name, *args, &block) column(name, *args) do |model| - format(block_given? ? yield : model.send(name)) do |date| - date.strftime("%m/%d/%Y") + format(block ? block.call(model) : model.public_send(name)) do |date| + date&.strftime("%m/%d/%Y") || "—".html_safe end end end + + # Makes a boolean YES/NO column + # @param name [Symbol] Column name + # @param args [Array] Other column helper arguments + # @example + # boolean_column(:approved) + # boolean_column(:has_tasks, preload: :tasks) do |model| + # model.tasks.unfinished.any? + # end + def self.boolean_column(name, *args, &block) + column(name, *args) do |model| + value = block ? block.call(model) : model.public_send(name) + value ? "Yes" : "No" + end + end + end