Skip to content

Commit

Permalink
Improve BaseDatagrid with more examples
Browse files Browse the repository at this point in the history
  • Loading branch information
bogdan committed Jan 22, 2024
1 parent 4a338b5 commit 3cb4f5d
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions templates/base.rb.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 3cb4f5d

Please sign in to comment.